Changes between Version 3 and Version 4 of demo_04


Ignore:
Timestamp:
04/05/2012 02:30:47 PM (14 years ago)
Author:
chenyang
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • demo_04

    v3 v4  
    3636如何单元测试SqlHelper.buildInsert?[[BR]] 
    3737测试SqlHelper.buildInsert(List<FieldDesc> fields, String tablename,String[] excludes);方法,需要准备fields,tablename,excludes这三个参数[[BR]] 
     38 
     39* 准备fields 
    3840fields是数据库表的元信息,元信息依赖SimpleJdbcTemplate对象根据表名获取,我们让测试类SqlHelperTest继承之AbstractTransactionalJUnit4SpringContextTests[[BR]] 
    3941以获得applicationContext对象的引用,然后注入SimpleJdbcTemplate对象. 
     
    5052} 
    5153}}} 
     54* 准备tablename,我们可以在数据库中创建一张表,用来测试 
     55 
     56{{{ #!java 
     57        private void createTesttable(String table) { 
     58 
     59                simpleJdbcTemplate.update("DROP TABLE IF EXISTS `" + table + "`"); 
     60 
     61                simpleJdbcTemplate 
     62                                .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}}}