nested exception is org.apache.ibatis.exceptions.TooManyResultsException: Expected one result

文章目录

      • 背景
      • 解决

背景

mybaitsPlus this.baseMapper.selectOne(wrapper); 查出多个结果,抛出了异常

解决

getOne(wrapper, false); ServiceImpl.getOne会在查出多个结果中选择一个

    public T getOne(Wrapper<T> queryWrapper, boolean throwEx) {
        return throwEx ? this.baseMapper.selectOne(queryWrapper) : SqlHelper.getObject(this.log, this.baseMapper.selectList(queryWrapper));
    }
        public static <E> E getObject(Log log, List<E> list) {
        return getObject(() -> {
            return log;
        }, list);
    }

    public static <E> E getObject(Supplier<Log> supplier, List<E> list) {
        if (CollectionUtils.isNotEmpty(list)) {
            int size = list.size();
            if (size > 1) {
                Log log = (Log)supplier.get();
                log.warn(String.format("Warn: execute Method There are  %s results.", size));
            }

            return list.get(0);
        } else {
            return null;
        }
    }

你可能感兴趣的:(java,开发语言)