解決Mybatis出現(xiàn)報錯Error querying database.Cause: java.lang.IndexOutOfBoundsException: Index 9 out of
錯誤現(xiàn)象
確認SQL沒有任何問題,但是在執(zhí)行時仍然出現(xiàn)以下報錯,報錯下標越界: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 - 運行異常
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)
錯誤原因
很明顯不是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é)果到實體映射的角度出發(fā)來考慮了。
我們看一下我們的實體類源碼
@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的插件,還是仔細考慮回想一下
我們一般寫數(shù)據(jù)庫實體類的時候,只要有字段定義和對應的getter、setter方法就可以了
為何用了Lombok的@Data注解之后有問題了呢?
我們看一下編譯后的實體類,用IDEA查看一下class反編譯的結(jié)果

重點來了
我們會發(fā)現(xiàn)與原本不使用Lombok的實體類相比,該類只有一個全參數(shù)的構(gòu)造方法,而原本我們手寫實體類時只寫了getter和setter方法,所以編譯后會默認帶一個無參構(gòu)造。
而上述使用Lombok的@Builder注解后,編譯后實體有全參數(shù)的構(gòu)造方法,那么編譯器就不會再默認提供一個默認的無參構(gòu)造。
所以問題可能就出現(xiàn)在這個無參構(gòu)造方法缺失上,mybatis在將查詢結(jié)果映射成實體時是通過無參構(gòu)造獲得一個實例,然后調(diào)用setter方法將值存入相應字段的。
解決方案
我們可以使用Lombok注解@NoArgsConstructor和@AllArgsConstructor,使編譯器編譯的時候提供出無參構(gòu)造。
在原實體上增加兩個注解即可,我們再重試就會發(fā)現(xiàn)SQL執(zhí)行正常了。
@Data @Builder(toBuilder = true) @NoArgsConstructor @AllArgsConstructor
注意:
因為我們使用@Builder注解,所以光添加一個@NoArgsConstructor,程序會編譯報錯的,builder需要一個全參構(gòu)造,所以我們還得加上@AllArgsConstructor。
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- 解決Mybatis報錯:org.apache.ibatis.reflection.ReflectionException: There is no getter for property named問題
- 解決mybatis generator MySQL自增ID出現(xiàn)重復問題MySQLIntegrityConstraintViolationException
- MyBatis嵌套查詢collection報錯:org.apache.ibatis.exceptions.TooManyResultsException
- 解決springboot3:mybatis-plus依賴錯誤:org.springframework.beans.factory.UnsatisfiedDependencyException
- 解決mybatis plus報錯com.microsoft.sqlserver.jdbc.SQLServerException:必須執(zhí)行該語句才能獲得結(jié)果
- 關(guān)于MyBatisSystemException異常產(chǎn)生的原因及解決過程
相關(guān)文章
SpringBoot使用Redis緩存的實現(xiàn)方法
這篇文章主要介紹了SpringBoot使用Redis緩存的實現(xiàn)方法,需要的朋友可以參考下2018-02-02
SpringBoot注入靜態(tài)屬性或靜態(tài)對象的方法
我們在使用SpringBoot為一些靜態(tài)屬性或者靜態(tài)對象注入時會發(fā)現(xiàn)注入不成功,我們可以以下這幾種方式把需要注入的值注入到靜態(tài)屬性中,感興趣的朋友一起看下2024-12-12
Java開發(fā)druid數(shù)據(jù)連接池maven方式簡易配置流程示例
本篇文章主要為大家介紹了java開發(fā)中druid數(shù)據(jù)連接池maven方式的簡易配置流程示例,文中附含詳細的代碼示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-10-10

