`

jdbctemplate在spring2,5中得以简化

阅读更多
http://www.iteye.com/topic/190083
在spring2.5以前时使用JdbcTemplate总会被RowMapper所烦恼,比如
private class JdbcPetRowMapper implements ParameterizedRowMapper<JdbcPet> {   
   public JdbcPet mapRow(ResultSet rs, int rownum) throws SQLException {   
            JdbcPet pet = new JdbcPet();   
            pet.setId(rs.getInt("id"));   
            pet.setName(rs.getString("name"));   
            pet.setBirthDate(rs.getDate("birth_date"));   
            pet.setTypeId(rs.getInt("type_id"));   
            pet.setOwnerId(rs.getInt("owner_id"));   
            return pet;   
        }   
    }  


现在方便多了,变成这样:
this.simpleJdbcTemplate.queryForObject(   
                    "SELECT id, first_name, last_name, address, city, telephone FROM owners WHERE id=?",   
                    ParameterizedBeanPropertyRowMapper.newInstance(Owner.class),   
                    id)  

依靠反射漂亮的完成了前段中的bean set
分享到:
评论
1 楼 chenlb 2008-06-30  
不错,RowMapper是比较烦琐

相关推荐

Global site tag (gtag.js) - Google Analytics