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

2023-05-16

文章目录

      • 背景
      • 解决

背景

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;
        }
    }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

nested exception is org.apache.ibatis.exceptions.TooManyResultsException: Expected one result 的相关文章

随机推荐