欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java 使用JdbcTemplate 中的queryForList發(fā)生錯(cuò)誤解決辦法

 更新時(shí)間:2017年07月11日 10:47:00   作者:蕃薯耀  
這篇文章主要介紹了Java 使用JdbcTemplate 中的queryForList發(fā)生錯(cuò)誤解決辦法的相關(guān)資料,需要的朋友可以參考下

Java 使用JdbcTemplate 中的queryForList發(fā)生錯(cuò)誤解決辦法

         在開(kāi)發(fā)項(xiàng)目中遇到JdbcTemplate 中的queryForList發(fā)生錯(cuò)誤,很是頭疼,在網(wǎng)上找了相關(guān)資料,可以幫忙解決,這里記錄下,

一、問(wèn)題描述: 

查詢時(shí)使用JdbcTemplate 中的queryForList發(fā)生錯(cuò)誤,如下: 

查詢方法如下:

jdbcTemplate.queryForList(selectSql.toString(), entityClass) 

查詢sql如下:

select * from test where 1=1 order by create_time desc limit 0,10 

錯(cuò)誤如下:

Incorrect column count: expected 1, actual 5 

二、解決方案:

1、上面錯(cuò)誤的原因是,查詢返回的結(jié)果列期望為1,但實(shí)際返回的是5列,因?yàn)閠est表中有5個(gè)字段,故返回5列。而這個(gè)方法參數(shù)的解釋是這樣的:

Parameters: 
sql SQL query to execute 
elementType the required type of element in the result list (for example, Integer.class) 

 就是第2個(gè)參數(shù)在網(wǎng)上說(shuō)只能是簡(jiǎn)單類型String或Integer。 

2、使用query查詢

jdbcTemplate.query(selectSql.toString(), rowMapper) 

 但多了一個(gè)參數(shù)rowMapper,這個(gè)參數(shù)需要定義為:

@SuppressWarnings("unused") 
  private BeanPropertyRowMapper<T> rowMapper = new BeanPropertyRowMapper<T>(entityClass){  
    @Override  
    protected void initBeanWrapper(BeanWrapper bw) {  
      super.initBeanWrapper(bw);  
    }  
  };  

具體的作用就是進(jìn)入查詢結(jié)果轉(zhuǎn)換成實(shí)體。 

到這步也就解決問(wèn)題了。

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

最新評(píng)論