抛出多次异常后,mp在使用mapper.selectOne后会出现错误?

开始的时候是查询列表方法用到了MP,在构建查询条件之后因为做了权限,如果没有权限直接抛出了自定义异常,然后前端测试了一下可行,但是多次抛出异常后就出现其它问题了,后面只要用到mapper.selectOne的方法都会报下面这个错,很奇怪不知道是不是因为这个原因导致的
为了更明显,然后我直接在这个方法抛异常,前端调了几次没问题,但是第9还是第10次的时候,只要后面走了mapper.selectOne方法都会报错,即使是第二个抛出异常参数设置是false也会抛出异常mapper.selectOne(xxx, false)


下面是测试代码,同样复现了,就简单的查询列表,执行几次抛出异常,然后传了74走到selectOne报错了,实体类字段就省略了

@Override
    public List<BaseMainFileVo> getFileList(BaseMainFileDto baseMainFileDto) {

        Long deptId = baseMainFileDto.getDeptId();
        if (deptId == 74) {
            LambdaQueryWrapper<BaseMainFile> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.eq(BaseMainFile::getFileId, 1);
            BaseMainFile file = baseMainFileMapper.selectOne(queryWrapper, false);
            System.err.println(file);
        } else {
            throw new CommonException(CommonExceptionTypeEnum.CODE_NO_DATA_FOUND);
        }
        List<BaseMainFileVo> collect = new ArrayList<>();
        return collect;
}
### Error querying database.  Cause: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
### The error may exist in com/xxx/BaseMainFileMapper.java (best guess)
### The error may involve com.xxx.BaseMainFileMapper.selectList_COUNT
### The error occurred while handling results
### SQL: SELECT count(0) FROM base_main_file WHERE (file_id = ?)
### Cause: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
### The error may exist in com/xxx/BaseMainFileMapper.java (best guess)
### The error may involve com.xxx.BaseMainFileMapper.selectList_COUNT
### The error occurred while handling results
### SQL: SELECT count(0) FROM base_main_file WHERE (file_id = ?)
### Cause: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = true)
@TableName(value="base_main_file")
public class BaseMainFile extends DataScopeBase implements Serializable {

    private static final long serialVersionUID = -1480328263191798648L;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class DataScopeBase implements Serializable {

    private static final long serialVersionUID = 4821439115640123L;
}
阅读 1.1k
2 个回答

看了你下面的描述,你前面用了pageHelper,即使后续跳过了sql执行,但是后面触发执行的第一条sql还是会做分页查询,这就出问题了,你可以抛出异常前手动清除Threadlocal中的数据就行了,
image.png
但是这种我个人觉得也太行,谁知道会不会影响到别的地方,新版的pageHepler已经支持在mapper层加入page参数,不需要以前那种startPage的处理了,那种就可以完美解决这个问题

@Override
public List<BaseMainFileVo> getFileList(BaseMainFileDto baseMainFileDto) {
    Long deptId = baseMainFileDto.getDeptId();
    if (deptId == 74) {
        LambdaQueryWrapper<BaseMainFile> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(BaseMainFile::getFileId, 1);
        BaseMainFile file = baseMainFileMapper.selectOne(queryWrapper, false);
        
        if (file == null) {
            System.err.println("No file found with the specified fileId.");
            // 处理未找到文件的情况
        } else {
            System.err.println(file);
            // 处理找到文件的情况
        }
    } else {
        throw new CommonException(CommonExceptionTypeEnum.CODE_NO_DATA_FOUND);
    }
    List<BaseMainFileVo> collect = new ArrayList<>();
    return collect;
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏