Spring JdbcTemplate queryforlist Error
Incorrect column count:expected 1, actual 5
>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>
? Copyright Sweet Potato Yiu July 10, 2017
http://www.cnblogs.com/fanshuyao/
First, the problem description:
An error occurred when querying with queryForList in JdbcTemplate, as follows:
The Query method is as follows:
Java code
Jdbctemplate.queryforlist (Selectsql.tostring (), Entityclass)
Query SQL as follows:
SQL code
SELECT * FROM test where 1=1 order by create_time desc limit 0,10
The error is as follows:
Java code
Incorrect column count:expected 1, actual 5
Second, the solution:
1, the reason for the above error is that the query returns a result column expected to be 1, but the actual return is 5 columns, because there are 5 fields in the test table, it returns 5 columns. The explanation for this method parameter is this:
Java code
Parameters:
SQL SQL query to execute
ElementType the required type of element in the result list (for example, Integer. Class)
The 2nd argument on the net is that it can only be a simple type string or integer.
2. Use Query queries
Java code
Jdbctemplate.query (Selectsql.tostring (), RowMapper)
But with one more parameter RowMapper, this parameter needs to be defined as:
Java code
- @SuppressWarnings ("unused")
- private Beanpropertyrowmapper<t> RowMapper = new Beanpropertyrowmapper<t> (entityclass) {
- @Override
- protected void Initbeanwrapper (Beanwrapper bw) {
- super.initbeanwrapper (BW);
- }
- };
The specific function is to enter the query results into the entity.
To this point will solve the problem.
>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>
? Copyright Sweet Potato Yiu July 10, 2017
http://www.cnblogs.com/fanshuyao/
Incorrect column count:expected 1, actual 5,jdbctemplate queryforlist error