Changes between Version 5 and Version 6 of demo_04


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

--

Legend:

Unmodified
Added
Removed
Modified
  • demo_04

    v5 v6  
    3838 
    3939* 准备fields 
    40 fields是数据库表的元信息,元信息依赖SimpleJdbcTemplate对象根据表名获取,我们让测试类SqlHelperTest继承之AbstractTransactionalJUnit4SpringContextTests[[BR]] 
    41 以获得applicationContext对象的引用,然后注入SimpleJdbcTemplate对象. 
     40fields是数据库表的元信息,元信息我们可以模拟.由newFieldDescs方法返回 
    4241{{{ #!java 
    4342@RunWith(SpringJUnit4ClassRunner.class) 
     
    4847        @Autowired 
    4948        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        } 
    5063 
    5164        ...... 
     
    5467* 准备tablename,我们可以在数据库中创建一张表,用来测试 
    5568 
    56 {{{ #!java 
    57         private void createTesttable(String table) { 
    5869 
    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 }}} 
    8970* excludes用来指定,哪些字段不参与构建sql插入语句,这里可以设置null[[BR]] 
    9071测试准备工作,做好了,下面就可以编写测试了[[BR]] 
     
    10081        } 
    10182 
    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 
    11484}}} 
    11585