国内私募机构九鼎控股打造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