Incorrect column count: expected 1, actual 5,JdbcTemplate queryForList 出錯


spring JdbcTemplate  queryForList 出錯

Incorrect column count: expected 1, actual 5

 

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

©Copyright 蕃薯耀 2017年7月10日

http://www.cnblogs.com/fanshuyao/

 

一、問題描述:

 

查詢時使用JdbcTemplate 中的queryForList發生錯誤,如下:

 

查詢方法如下:

Java代碼   收藏代碼
  1. jdbcTemplate.queryForList(selectSql.toString(), entityClass)  

 

查詢sql如下:

Sql代碼   收藏代碼
  1. select * from test where 1=1 order by create_time desc limit 0,10  

 

錯誤如下:

Java代碼   收藏代碼
  1. Incorrect column count: expected 1, actual 5  

 

 

二、解決方案:

1、上面錯誤的原因是,查詢返回的結果列期望為1,但實際返回的是5列,因為test表中有5個字段,故返回5列。而這個方法參數的解釋是這樣的:

Java代碼   收藏代碼
  1. Parameters:  
  2. sql SQL query to execute  
  3. elementType the required type of element in the result list (for example, Integer.class)  

 就是第2個參數在網上說只能是簡單類型String或Integer。

 

2、使用query查詢

Java代碼   收藏代碼
  1. jdbcTemplate.query(selectSql.toString(), rowMapper)  

 但多了一個參數rowMapper,這個參數需要定義為:

Java代碼   收藏代碼
  1. @SuppressWarnings("unused")  
  2.     private BeanPropertyRowMapper<T> rowMapper = new BeanPropertyRowMapper<T>(entityClass){    
  3.         @Override    
  4.         protected void initBeanWrapper(BeanWrapper bw) {    
  5.             super.initBeanWrapper(bw);    
  6.         }    
  7.     };   

具體的作用就是進入查詢結果轉換成實體。

 

到這步也就解決問題了。

 

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

©Copyright 蕃薯耀 2017年7月10日

http://www.cnblogs.com/fanshuyao/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM