【Spring】queryForXXX()方法使用的一些细节问题

来源:互联网 发布:网络推广外包收费 编辑:程序博客网 时间:2024/05/14 07:01
关于使用Spring的JdbcTemplate简化jdbc开发:spring 3.2.2版本之后,jdbctemplate中的queryForInt已经被取消了!
queryForXXX()方法的使用:
queryForObject代替queryForInt方法。
queryForObject方法详解,查看源代码,queryForObject(String sql, Object[] args, Class<T> requiredType){}

例如:spring 3.2.2之前的queryForInt()方法
JdbcTemplate jdbcTemplate = new JdbcTemplate();String sql = "select count(*) from user";//调用queryForInt()方法,可以直接得到返回的int类型!int count = jdbcTemplate.queryForInt(sql);

例如:queryForObject()方法
JdbcTemplate jdbcTemplate = new JdbcTemplate();String sql = "select count(*) from user";//queryForObject()方法中,如果需要返回的是int类型,就写Integer.class,需要返回long类型就写long.class.int count = jdbcTemplate.queryForObject(sql,Interger.Class);

注意:当需要返回是什么类型,那么就在第三个参数指定什么类型,例如:需要返回int类型,就写Integer.class,需要返回long类型就写long.class.
      这一点和spring 3.2.2之前的版本jdbctemplate中的queryForInt()方法有所区别,但本质还是一样的,所不同的是,需要我们额外指定返回的参数类型。

总结:queryForObject()代替queryForInt()方法使用需要开发者额外指定返回的类型。
0 0
原创粉丝点击