- 在Spring框架中使用JdbcTemplate.queryForList返回自定义对象
org.springframework.jdbc.IncorrectResultSetColumnCountException: Incorrect column count: expected 1, actual 2 at org.springframework.jdbc.core.SingleColumnRowMapper.mapRow(SingleColumnRowMapper.java:108) at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:94)
- 错误代码:
String sql = "select id,name from user"; List分析list = jdbcTemplate.queryForList(sql, Lizz.class);
错误提示只能赋值一个字段,但是返回结果有2个字段,因为queryForList方法只支持元素类型。
调整方案List queryForList(String sql, Class elementType) throws DataAccessException;
使用jdbcTemplate.query方法查询
// 查询数据集合 List相似异常:Spring JdbcTemplate异常:EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0_进击的小白-CSDN博客list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Lizz.class)); // 查询一个对象 Lizz list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Lizz.class)); // 返回一个对象 Lizz lizz = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Lizz.class));



