在设计数据库表的封装类时,在某字段上加
上注解@TableField(exist = false) 表示表中不包含这个字段,但是在做CRUD时,却出现了角标越界异常IndexOutOfBoundsException,原代码如下:
//封装数据库数据。 @AllArgsConstructor @Data @TableName("userlogin") public class UserLogin { @TableId @TableField(value = "uid") @ApiModelProperty("ID") private Integer uid; @ApiModelProperty("用户名") private String username; @ApiModelProperty("密码") private String password; @TableField(exist = false) private String token; }
==> Preparing: SELECT uid,username,password FROM userlogin WHERE (username = ? AND password = ?)
==> Parameters: zhangsan(String), 123456(String)
<== Columns: uid, username, password
<== Row: 2, zhangsan, 123456
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1c848110]
2022-07-21 21:09:55.378 ERROR 9188 --- [nio-8080-exec-1] 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: 3, Size: 3
### The error may exist in com/xhcoder/mapper/UserLoginMapper.java (best guess)
### The error may involve com.xhcoder.mapper.UserLoginMapper.selectOne
### The error occurred while handling results
### SQL: SELECT uid,username,password FROM userlogin WHERE (username = ? AND password = ?)
### Cause: java.lang.IndexOutOfBoundsException: Index: 3, Size: 3] with root cause
经过检查,发现是因为标注了 @AllargsConstructor。注释掉就可以了。
JDBC Connection [HikariProxyConnection@1420380651 wrapping com.mysql.jdbc.JDBC4Connection@7f5d6cce] will not be managed by Spring
==> Preparing: SELECT uid,username,password FROM userlogin WHERE (username = ? AND password = ?)
==> Parameters: zhangsan(String), 123456(String)
<== Columns: uid, username, password
<== Row: 2, zhangsan, 123456
<== Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1ca8a8d7]