天天看點

踩坑系列—mybatis查詢報錯java.lang.IndexOutOfBoundsException

sql單獨執行正确,但是mybatis查詢報錯,部分錯誤資訊如下:

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:

###Error querying database. Cause: java.lang.IndexOutOfBoundsException: Index: 6, Size: 6

###The error may involve com.xxx.persistence.mapper.XXXMapper.countxxxQty

###The error occurred while handling results

###SQL: SELECT D.WAREHOUSE_ID, D.SKU_ID, D.EXPECTED_QTY_NUM, D.NAME, D.OPER,D.OPER_TIME FROM do_header D WHERE D .do_type = 100 and D.sku_id in ( ? , ? , ? , ? )

###Cause:

java.lang.IndexOutOfBoundsException: Index: 6, Size: 6

at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:92)

at org.mybatis.spring.SqlSessionTemplate S q l S e s s i o n I n t e r c e p t o r . i n v o k e ( S q l S e s s i o n T e m p l a t e . j a v a : 440 ) a t c o m . s u n . p r o x y . SqlSessionInterceptor.invoke(SqlSessionTemplate.java:440) at com.sun.proxy. SqlSessionInterceptor.invoke(SqlSessionTemplate.java:440)atcom.sun.proxy.Proxy106.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.invoke(MapperProxy.java:93)

at com.sun.proxy.$Proxy241.countSkuQtyForCalculate(Unknown Source)

原因:

mapper中查詢結果resultMap對應的DoxxxDto使用了

lombok注解,使用有誤導緻的

。報錯時的注解:

@Data

@Builder

public class DoxxxDto{

private xxx1;

private xxx2;

private xxx3;

private xxx4;

private xxx5;

private xxx6;

private xxx7;

}

沒有構造函數,将查詢結果轉換成javebean的時候出了問題,報錯中的6是因為查詢了6列。加上構造函數的注解即可。如下:

@Data

@Builder

@NoArgsConstructor

@AllArgsConstructor

public class DoxxxDto{

private xxx1;

private xxx2;

private xxx3;

private xxx4;

private xxx5;

private xxx6;

private xxx7;

}

參考部落格位址:https://blog.csdn.net/qq_37186947/article/details/102160260

繼續閱讀