调用 jdbcTemplate.queryForList 时出现错误 spring-org.springframework.jdbc.IncorrectResultSetColumnCountException

查找到解决方法: http://www.cnblogs.com/wumian/articles/2012-10-25-2056.html

调用spring jdbc接口:

User user = (User) this.jdbcTemplate.queryForObject(
                "SELECT * FROM USER WHERE ID = 1", User.class);

 报错如下:

Exception in thread "main" org.springframework.jdbc.IncorrectResultSetColumnCountException: Incorrect column count: expected 1, actual 4
    at org.springframework.jdbc.core.SingleColumnRowMapper.mapRow(SingleColumnRowMapper.java:87)
    at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:92)
    at org.springframework.jdbc.core.RowMapperResultSetExtractor.extractData(RowMapperResultSetExtractor.java:1)
    at org.springframework.jdbc.core.JdbcTemplate$1QueryStatementCallback.doInStatement(JdbcTemplate.java:445)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:395)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:455)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:463)
    at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:471)
    at org.springframework.jdbc.core.JdbcTemplate.queryForObject(JdbcTemplate.java:476)
    at com.ylp.dao.UserDao.getById(UserDao.java:45)
    at com.ylp.dao.Test.main(Test.java:17)

 大概意思是:查出的数据有4列,它把jdbc.queryForObject("SELECT * FROM USER WHERE ID = 1", User.class);中的第一列转换成User.class,所以 expected 1, actual 6,预期1列,但查出来的数据有4列

修改:

RowMapper<User> rm = ParameterizedBeanPropertyRowMapper.newInstance(User.class);
        User user = (User) this.jdbcTemplate.queryForObject(
                "SELECT * FROM USER WHERE ID = 1", rm);

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。