Changes between Version 5 and Version 6 of demo_04
- Timestamp:
- 04/05/2012 02:42:16 PM (14 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
demo_04
v5 v6 38 38 39 39 * 准备fields 40 fields是数据库表的元信息,元信息依赖SimpleJdbcTemplate对象根据表名获取,我们让测试类SqlHelperTest继承之AbstractTransactionalJUnit4SpringContextTests[[BR]] 41 以获得applicationContext对象的引用,然后注入SimpleJdbcTemplate对象. 40 fields是数据库表的元信息,元信息我们可以模拟.由newFieldDescs方法返回 42 41 {{{ #!java 43 42 @RunWith(SpringJUnit4ClassRunner.class) … … 48 47 @Autowired 49 48 SimpleJdbcTemplate simpleJdbcTemplate; 49 50 51 private List<FieldDesc> newFieldDescs() { 52 List<FieldDesc> list = new ArrayList<FieldDesc>(); 53 FieldDesc id = new FieldDesc(); 54 id.setField("gameId"); 55 id.setType("int(11)"); 56 FieldDesc createAt = new FieldDesc(); 57 createAt.setField("createAt"); 58 createAt.setType("timestamp"); 59 list.add(id); 60 list.add(createAt); 61 return list; 62 } 50 63 51 64 ...... … … 54 67 * 准备tablename,我们可以在数据库中创建一张表,用来测试 55 68 56 {{{ #!java57 private void createTesttable(String table) {58 69 59 simpleJdbcTemplate.update("DROP TABLE IF EXISTS `" + table + "`");60 61 simpleJdbcTemplate62 .update("CREATE TABLE `" + table + "` ("63 + "`id` bigint(20) NOT NULL,"64 + "`createAt` timestamp NULL DEFAULT NULL,"65 + "`name` varchar(50) DEFAULT NULL,"66 + "`age` int(11) DEFAULT NULL,"67 + "`info` text,"68 + "`rate` float DEFAULT NULL,"69 + "PRIMARY KEY (`id`)"70 + ") ENGINE=InnoDB DEFAULT CHARSET=gbk;");71 72 Calendar calendar = Calendar.getInstance();73 calendar.set(Calendar.YEAR, 2011);74 calendar.set(Calendar.MONTH, 10);75 calendar.set(Calendar.DAY_OF_MONTH, 10);76 calendar.set(Calendar.HOUR_OF_DAY, 23);77 calendar.set(Calendar.MINUTE, 23);78 calendar.set(Calendar.SECOND, 23);79 80 int rows = simpleJdbcTemplate.update(81 "INSERT INTO test_table VALUES(?,?,?,?,?,?)", new Object[] {82 393648562, calendar.getTime(), "chenyang", 25,83 "速度第一,完美第二",0.35 });84 85 Assert.assertEquals(1, rows);86 87 }88 }}}89 70 * excludes用来指定,哪些字段不参与构建sql插入语句,这里可以设置null[[BR]] 90 71 测试准备工作,做好了,下面就可以编写测试了[[BR]] … … 100 81 } 101 82 102 private List<FieldDesc> newFieldDescs() { 103 List<FieldDesc> list = new ArrayList<FieldDesc>(); 104 FieldDesc id = new FieldDesc(); 105 id.setField("gameId"); 106 id.setType("int(11)"); 107 FieldDesc createAt = new FieldDesc(); 108 createAt.setField("createAt"); 109 createAt.setType("timestamp"); 110 list.add(id); 111 list.add(createAt); 112 return list; 113 } 83 114 84 }}} 115 85
![(please configure the [header_logo] section in trac.ini)](http://www1.pconline.com.cn/hr/2009/global/images/logo.gif)