问题
今天在项目中遇到一个问题:项目中使用了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 //无参构造函数