使用JdbcTemplate报 Incorrect column count: expected 1, actual 5错误解决

Incorrect column count: expected 1, actual 5

在使用jdbc的querForObject queryForList的时候,出现Incorrect column count: expected 1, actual 5

比如 

1
2
String sql = "select * from sysuser where id = 3";
SysUser s = this.jdbcTemplate.queryForObject(sql, SysUser.class);

其实这样是不对的,

应该为

1
2
3
String sql = "select name from sysuser where id = 3";
String s = this.jdbcTemplate.queryForObject(sql, String.class);
System.out.println(s);

这个 jdbcTemplate.queryForObject(sql, requiredType) 中的requiredType应该为基础类型,和String类型。

如果想查真正的object应该为

1
2
3
4
List<SysUser> userList = jdbcTemplate.query(sql, new Object[]{}, new BeanPropertyRowMapper<SysUser>(SysUser.class));
if(null!=userList&&userList.size()>0){
    SysUser user = userList.get(0);
}
posted @ 2018-04-03 08:56  人艰不拆_zmc  阅读(876)  评论(0编辑  收藏  举报