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

简介: 国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私募机构九鼎控股打造,九鼎投资是在全国股份转让系统挂牌的公众公司,股票代码为430719,为“中国PE第一股”,市值超1000亿元。

国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html
内部邀请码:C8E245J (不写邀请码,没有现金送)
国内私募机构九鼎控股打造,九鼎投资是在全国股份转让系统挂牌的公众公司,股票代码为430719,为中国PE第一股,市值超1000亿元。 

------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

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

【推荐看看】 http://my.oschina.net/u/218421/blog/38598

调用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);


提示:以上结论在 spring 4.x 上行不通,主要是 queryForObject 模板方法针对的结果集是“一列”的时候,并且支持的是常见类型(Integer.class)等

 再次查找资料,在官方的 api 中,仅仅提到:

elementType - the required type of element in the result list (for example, Integer.class)

哪如果我想要返回一个 List<T> 的列表对象不行吗?查找资料,发现如下文章将的比较好: 

【推荐看看】 http://my.oschina.net/u/218421/blog/38598

改用如下代码实现即可:

return (new DBUtility()).getJdbcDao().query(sql, new RowMapper<Task>() {
            public Task mapRow(ResultSet arg0, int arg1) throws SQLException {
                Task t = new Task();
                t.setTaskId(arg0.getLong("taskId"));
                t.setTaskName(arg0.getString("taskName"));
                t.setTaskDescription(arg0.getString("taskDescription"));
                t.setTaskPriority(arg0.getString("taskPriority"));
                t.setTaskStatus(arg0.getString("taskStatus"));
                return t;
            }
        });

 

OK,此时才算解决问题。

spring jdbc 版本: 4.1.1.RELEASE

目录
相关文章
|
Java
SpringBoot整合Mybatis-Plus报错:org.springframework.core.NestedIOException
SpringBoot整合Mybatis-Plus报错:org.springframework.core.NestedIOException
1699 0
SpringBoot整合Mybatis-Plus报错:org.springframework.core.NestedIOException
|
5月前
|
Java Spring
nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException(Spring循环依赖问题)
nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException(Spring循环依赖问题)
123 0
|
7月前
|
SQL Java 数据库连接
jpa、hibernate、spring-data-jpa、jdbcTemplate
jpa、hibernate、spring-data-jpa、jdbcTemplate
|
Java 微服务 Spring
【Java异常】Spring boot启动失败@org.springframework.beans.factory.annotation.Autowired(required=true)
【Java异常】Spring boot启动失败@org.springframework.beans.factory.annotation.Autowired(required=true)
311 0
|
Java 数据库连接 mybatis
flowable项目启动项目报错:Consider defining a bean of type ‘org.mybatis.spring.SqlSessionTemplate‘ in ……
flowable项目启动项目报错:Consider defining a bean of type ‘org.mybatis.spring.SqlSessionTemplate‘ in ……
329 0
|
7月前
|
Java Apache vr&ar
springmvc报错 nested exception is org.mybatis.spring.MyBatisSystemException:
springmvc报错 nested exception is org.mybatis.spring.MyBatisSystemException:
|
7月前
|
Java Windows Perl
mybatis+spring报错PropertyAccessException 1: org.springframework.beans.MethodInvocationException
mybatis+spring报错PropertyAccessException 1: org.springframework.beans.MethodInvocationException
springboot: Failed to bind properties under ‘spring.datasource’ to javax.sql.DataSource
springboot: Failed to bind properties under ‘spring.datasource’ to javax.sql.DataSource
springboot: Failed to bind properties under ‘spring.datasource’ to javax.sql.DataSource
|
Java 数据库连接 mybatis
Spring boot 整合mybatis报错ClassNotFoundException: org.mybatis.logging.LoggerFactory
Spring boot 整合mybatis报错ClassNotFoundException: org.mybatis.logging.LoggerFactory
826 0
Spring boot 整合mybatis报错ClassNotFoundException: org.mybatis.logging.LoggerFactory
|
Java 数据库连接 mybatis
springboot集成mybatis报错:Invalid default: public abstract java.lang.Class org.mybatis.spring.annotation
springboot集成mybatis报错:Invalid default: public abstract java.lang.Class org.mybatis.spring.annotation
321 0