解決Mybatis出現(xiàn)報(bào)錯(cuò)Error querying database.Cause: java.lang.IndexOutOfBoundsException: Index 9 out of
錯(cuò)誤現(xiàn)象
確認(rèn)SQL沒有任何問題,但是在執(zhí)行時(shí)仍然出現(xiàn)以下報(bào)錯(cuò),報(bào)錯(cuò)下標(biāo)越界:IndexOutOfBoundsException: Index 9 out of bounds for length 9
18:02:19.449 [http-nio-8007-exec-10] DEBUG c.t.c.c.d.GyUserWithdrawDao.listWihdraw - ==> Preparing: SELECT gyw.*, ui.realname realname FROM `gy_user_withdraw` gyw inner join user_info ui on gyw.user_id = ui.userid LIMIT ?
18:02:19.450 [http-nio-8007-exec-10] DEBUG c.t.c.c.d.GyUserWithdrawDao.listWihdraw - ==> Parameters: 10(Integer)
18:02:19.466 [http-nio-8007-exec-10] ERROR c.t.c.exception.ExceptionHandlerAdvice - 運(yùn)行異常
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.lang.IndexOutOfBoundsException: Index 9 out of bounds for length 9
### The error may exist in file [D:\CIMS\gyyh_2.3.3\cims_commons_db\target\classes\mybatis\mysql\GyUserWithdrawMapper.xml]
### The error may involve com.trusfort.cims.commons.dao.GyUserWithdrawDao.listWihdraw
### The error occurred while handling results
### SQL: SELECT gyw.*, ui.realname realname FROM `gy_user_withdraw` gyw inner join user_info ui on gyw.user_id = ui.userid LIMIT ?
### Cause: java.lang.IndexOutOfBoundsException: Index 9 out of bounds for length 9
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:92)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:440)
at com.sun.proxy.$Proxy172.selectList(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:223)
at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:147)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:80)
at org.apache.ibatis.binding.MapperProxy$PlainMethodInvoker.invoke(MapperProxy.java:144)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:85)
at com.sun.proxy.$Proxy369.listWihdraw(Unknown Source)
錯(cuò)誤原因
很明顯不是SQL的問題,從日志來看SQL明顯沒有問題,而且我們的Mapper.xml中配置也沒有問題
<select id="listWihdraw" resultType="GyUserWithdraw"> SELECT gyw.*, ui.realname realname FROM `gy_user_withdraw` gyw inner join user_info ui on gyw.user_id = ui.userid </select>
那就只能從Mybatis執(zhí)行SQL后查詢結(jié)果到實(shí)體映射的角度出發(fā)來考慮了。
我們看一下我們的實(shí)體類源碼
@Data @Builder(toBuilder = true) public class GyUserWithdraw implements Serializable { private String id; private String userId; private String employeenum; private String statusBefore; private String statusAfter; private String syncResult; private Date createTime; private Date updateTime; }
看著貌似是沒什么毛病,但鑒于我們此處偷懶使用了Lombok的插件,還是仔細(xì)考慮回想一下
我們一般寫數(shù)據(jù)庫實(shí)體類的時(shí)候,只要有字段定義和對應(yīng)的getter、setter方法就可以了
為何用了Lombok的@Data注解之后有問題了呢?
我們看一下編譯后的實(shí)體類,用IDEA查看一下class反編譯的結(jié)果
重點(diǎn)來了
我們會(huì)發(fā)現(xiàn)與原本不使用Lombok的實(shí)體類相比,該類只有一個(gè)全參數(shù)的構(gòu)造方法,而原本我們手寫實(shí)體類時(shí)只寫了getter和setter方法,所以編譯后會(huì)默認(rèn)帶一個(gè)無參構(gòu)造。
而上述使用Lombok的@Builder
注解后,編譯后實(shí)體有全參數(shù)的構(gòu)造方法,那么編譯器就不會(huì)再默認(rèn)提供一個(gè)默認(rèn)的無參構(gòu)造。
所以問題可能就出現(xiàn)在這個(gè)無參構(gòu)造方法缺失上,mybatis在將查詢結(jié)果映射成實(shí)體時(shí)是通過無參構(gòu)造獲得一個(gè)實(shí)例,然后調(diào)用setter方法將值存入相應(yīng)字段的。
解決方案
我們可以使用Lombok注解@NoArgsConstructor
和@AllArgsConstructor
,使編譯器編譯的時(shí)候提供出無參構(gòu)造。
在原實(shí)體上增加兩個(gè)注解即可,我們再重試就會(huì)發(fā)現(xiàn)SQL執(zhí)行正常了。
@Data @Builder(toBuilder = true) @NoArgsConstructor @AllArgsConstructor
注意:
因?yàn)槲覀兪褂?code>@Builder注解,所以光添加一個(gè)@NoArgsConstructor
,程序會(huì)編譯報(bào)錯(cuò)的,builder需要一個(gè)全參構(gòu)造,所以我們還得加上@AllArgsConstructor
。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- 解決Mybatis報(bào)錯(cuò):org.apache.ibatis.reflection.ReflectionException: There is no getter for property named問題
- 解決mybatis generator MySQL自增ID出現(xiàn)重復(fù)問題MySQLIntegrityConstraintViolationException
- MyBatis嵌套查詢collection報(bào)錯(cuò):org.apache.ibatis.exceptions.TooManyResultsException
- 解決springboot3:mybatis-plus依賴錯(cuò)誤:org.springframework.beans.factory.UnsatisfiedDependencyException
- 解決mybatis plus報(bào)錯(cuò)com.microsoft.sqlserver.jdbc.SQLServerException:必須執(zhí)行該語句才能獲得結(jié)果
- 關(guān)于MyBatisSystemException異常產(chǎn)生的原因及解決過程
相關(guān)文章
Java使用枚舉實(shí)現(xiàn)狀態(tài)機(jī)的方法詳解
這篇文章主要介紹了Java使用枚舉實(shí)現(xiàn)狀態(tài)機(jī)的方法詳解,枚舉類型很適合用來實(shí)現(xiàn)狀態(tài)機(jī),狀態(tài)機(jī)可以處于有限數(shù)量的特定狀態(tài),它們通常根據(jù)輸入,從一個(gè)狀態(tài)移動(dòng)到下一個(gè)狀態(tài),但同時(shí)也會(huì)存在瞬態(tài),需要的朋友可以參考下2023-11-11SpringBoot使用Redis緩存的實(shí)現(xiàn)方法
這篇文章主要介紹了SpringBoot使用Redis緩存的實(shí)現(xiàn)方法,需要的朋友可以參考下2018-02-02SpringBoot注入靜態(tài)屬性或靜態(tài)對象的方法
我們在使用SpringBoot為一些靜態(tài)屬性或者靜態(tài)對象注入時(shí)會(huì)發(fā)現(xiàn)注入不成功,我們可以以下這幾種方式把需要注入的值注入到靜態(tài)屬性中,感興趣的朋友一起看下2024-12-12Java實(shí)現(xiàn)批量化操作Excel文件的示例代碼
在操作Excel的場景中,通常會(huì)有一些針對Excel的批量操作,這篇文章主要為大家詳細(xì)介紹了如何使用GcExcel實(shí)現(xiàn)批量化操作Excel,感興趣的可以了解一下2024-12-12Java開發(fā)druid數(shù)據(jù)連接池maven方式簡易配置流程示例
本篇文章主要為大家介紹了java開發(fā)中druid數(shù)據(jù)連接池maven方式的簡易配置流程示例,文中附含詳細(xì)的代碼示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-10-10Java獲取Excel中圖片所在的行和列坐標(biāo)位置
這篇文章主要介紹了Java獲取Excel中圖片所在的行和列坐標(biāo)位置,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-08-08