Error querying database. Cause: java.lang.IndexOutOfBoundsException: Index: 6, Size: 6
项目突然报这个错误,研究半天,找到解决办法。
项目请求获取token接口,如下提示。
查看后端报错详细信息。贴出一段比价有价值的报错信息
Caused by: org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.lang.IndexOutOfBoundsException: Index: 6, Size: 6
### The error may exist in file [D:\IDEAWORKSPACE\YJFDataPlatform\yjf-server\target\classes\mapper\TsysAccountMapper.xml]
### The error may involve com.cn.yjf.server.dao.TsysAccountMapper.selectByName
### The error occurred while handling results
### SQL: select account_id, user_id, username, password, status, active from t_sys_account WHERE username = ? limit 1
### Cause: java.lang.IndexOutOfBoundsException: Index: 6, Size: 6
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:153)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:145)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:76)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:427)
... 115 more
- 根据报错信息,仔细检查了下sql,发现sql没有问题。
- 在mapper中定义的返回结果,如果是具体的实体类,该实体类必须要包含一个无参构造器,如果自定义了构造函数,则必须加上无参构造函数。
- 对实体类加上无参构造函数,问题就解决了。
package com.cn.yjf.server.auth.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
/**
* 用户信息表实体
*/
@TableName("t_sys_account")
public class TsysAccount implements Serializable, UserDetails {
private Long accountId; //主键ID
private Long userId; //用户编号
private String username; //用户名称
private String password; //密码
private Integer status; //状态 1-正常
private Integer active; //1-正常;0-禁用
@TableField(exist = false)
List<TsysRole> roles; //角色集合
@TableField(exist = false)
private List<GrantedAuthority> authorities; //权限集合
public TsysAccount() {}
public TsysAccount(Long accountId, Long userId, String username, String password, Integer status, Integer active, List<TsysRole> roles, List<GrantedAuthority> authorities) {
this.accountId = accountId;
this.userId = userId;
this.username = username;
this.password = password;
this.status = status;
this.active = active;
this.roles = roles;
this.authorities = authorities;
}
public Long getAccountId() {
return accountId;
}
public void setAccountId(Long accountId) {
this.accountId = accountId;
}
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
if (this.status == null) {
return false;
} else {
return this.status.equals(1);
}
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
if(this.active==0){
return false;
}else{
return true;
}
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return this.authorities;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public Integer getActive() {
return active;
}
public void setActive(Integer active) {
this.active = active;
}
public List<TsysRole> getRoles() {
return roles;
}
public void setRoles(List<TsysRole> roles) {
this.roles = roles;
}
public void setAuthorities(List<GrantedAuthority> authorities) {
this.authorities = authorities;
}
}