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 ); } |