MybatisPlus出現Error querying database. Cause: java.lang.IndexOutOfBoundsException: Index: 7, Size: 7

問題

今天在項目中遇到一個問題:項目中使用了mybatisplus和lombok,在執行查詢操作時,後臺報錯:

2020-03-20 19:48:26.232 ERROR 11612 --- [nio-8000-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.lang.IndexOutOfBoundsException: Index: 7, Size: 7
### The error may exist in com/foxconn/mapper/UserMapper.java (best guess)
### The error may involve com.foxconn.mapper.UserMapper.selectOne
### The error occurred while handling results
### SQL: SELECT   openid,nick_name,real_name,identity_card,email,birth,mobile   FROM user     WHERE (openid = ? AND status = ?)
### Cause: java.lang.IndexOutOfBoundsException: Index: 7, Size: 7] with root cause

分析

從上面可以看打印的sql日誌,發現mybatisplus生成的sql是沒問題的,於是懷疑是參數傳遞出錯,分析了一下程序:

 QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.select("openid", "nick_name", "real_name", "identity_card", "email", "birth", "mobile")
        .eq("openid", openid).eq("status", "0");
        User user = userService.getOne(wrapper);

深思熟慮後這段代碼是沒問題的,最後只能懷疑到實體類上了,查看實體類User,我這裏使用了lombok插件,檢查了idea安裝了插件,maven也導入了依賴包。
在這裏插入圖片描述
最終測試發現實體類並沒有提供構造函數,@Data並不能自動生成構造函數,查了資料,在類似添加了兩個構造函數註解,問題解決。

@Data
@Builder
@AllArgsConstructor //全參構造函數
@NoArgsConstructor  //無參構造函數
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章