Mybatis PageHelper 异常 attempted to return null from a method with a primitive re

Mybatis PageHelper 异常 attempted to return null from a method with a primitive return type (long) 问题分析

问题背景

某天工程师群里发了个分页的问题,当时我手头没有急的工作,就帮忙看了下。一开始看到分页的字段使用的比较特殊,经过调整统一后,发现接口在第二页直接返回了错误:attempted to return null from a method with a primitive return type (long)。

问题分析

业务代码中主要逻辑就两个,一个是查询符合条件的记录数量 countByExample ,另一个是查询符合条件的记录 selectByExample 。这两个方法都是使用mybatis generator生成的代码,所以我就直接看了下生成的代码。 countByExample的返回值是long基本数据类型。但是错误提示返回了null。

经过排查后,发现Mybatis源码中PageHelper作为一个插件存在,是一个拦截器。会在每次query类型的方法执行时进行拦截,最后执行clear方法,清空分页。以下是部分关键源码:

java 复制代码
/**
 * Mybatis - 通用分页拦截器
 * <p>
 * GitHub: https://github.com/pagehelper/Mybatis-PageHelper
 * <p>
 * Gitee : https://gitee.com/free/Mybatis_PageHelper
 *
 * @author liuzh/abel533/isea533
 * @version 5.0.0
 */
@SuppressWarnings({"rawtypes", "unchecked"})
@Intercepts(
        {
                @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}),
                @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}),
        }
)
public class PageInterceptor implements Interceptor {
    private volatile Dialect dialect;
    private String countSuffix = "_COUNT";
    protected Cache<String, MappedStatement> msCountMap = null;
    private String default_dialect_class = "com.github.pagehelper.PageHelper";

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        try {
            Object[] args = invocation.getArgs();
            MappedStatement ms = (MappedStatement) args[0];
            Object parameter = args[1];
            RowBounds rowBounds = (RowBounds) args[2];
            ResultHandler resultHandler = (ResultHandler) args[3];
            Executor executor = (Executor) invocation.getTarget();
            CacheKey cacheKey;
            BoundSql boundSql;
            //由于逻辑关系,只会进入一次
            if (args.length == 4) {
                //4 个参数时
                boundSql = ms.getBoundSql(parameter);
                cacheKey = executor.createCacheKey(ms, parameter, rowBounds, boundSql);
            } else {
                //6 个参数时
                cacheKey = (CacheKey) args[4];
                boundSql = (BoundSql) args[5];
            }
            checkDialectExists();
            //对 boundSql 的拦截处理
            if (dialect instanceof BoundSqlInterceptor.Chain) {
                boundSql = ((BoundSqlInterceptor.Chain) dialect).doBoundSql(BoundSqlInterceptor.Type.ORIGINAL, boundSql, cacheKey);
            }
            List resultList;
            //调用方法判断是否需要进行分页,如果不需要,直接返回结果
            if (!dialect.skip(ms, parameter, rowBounds)) {
                //判断是否需要进行 count 查询
                if (dialect.beforeCount(ms, parameter, rowBounds)) {
                    //查询总数
                    Long count = count(executor, ms, parameter, rowBounds, null, boundSql);
                    //处理查询总数,返回 true 时继续分页查询,false 时直接返回
                    if (!dialect.afterCount(count, parameter, rowBounds)) {
                        //当查询总数为 0 时,直接返回空的结果
                        return dialect.afterPage(new ArrayList(), parameter, rowBounds);
                    }
                }
                resultList = ExecutorUtil.pageQuery(dialect, executor,
                        ms, parameter, rowBounds, resultHandler, boundSql, cacheKey);
            } else {
                //rowBounds用参数值,不使用分页插件处理时,仍然支持默认的内存分页
                resultList = executor.query(ms, parameter, rowBounds, resultHandler, cacheKey, boundSql);
            }
            return dialect.afterPage(resultList, parameter, rowBounds);
        } finally {
            if(dialect != null){
                dialect.afterAll();
            }
        }
    }
}

dialect.afterAll(); 会清空分页信息。

而在业务代码中,因为PageHelper下方调用的是countByExample,PageHelper已经clear了,所以在selectByExample执行时,PageHelper已经失效。但是这样的情况也只是会导致分页查询不生效,不会导致返回null的问题。

null的原因是countByExample被PageHelper拦截,但是无法被正常的分页,返回了null

解决方案

将Pagehelper紧跟在要分页的查询前,并且每次进行分页查询时都要重新设置PageHelper。避免出现PageHelper下方跟的是count方法。

正确示例

java 复制代码
long count = yourEntityMapper.countByExample(example);

PageHelper.startPage(pageNum, pageSize);
List<YourEntity> list = yourEntityMapper.selectByExample(example);

错误示例

java 复制代码
PageHelper.startPage(pageNum, pageSize);

long count = yourEntityMapper.countByExample(example);
List<YourEntity> list = yourEntityMapper.selectByExample(example);
相关推荐
xiaoye20181 小时前
Lettuce连接模型、命令执行、Pipeline 浅析
java
beata4 小时前
Java基础-18:Java开发中的常用设计模式:深入解析与实战应用
java·后端
Seven975 小时前
剑指offer-81、⼆叉搜索树的最近公共祖先
java
雨中飘荡的记忆1 天前
保证金系统入门到实战
java·后端
Nyarlathotep01131 天前
Java内存模型
java
暮色妖娆丶1 天前
不过是吃了几年互联网红利罢了,我高估了自己
java·后端·面试
NE_STOP1 天前
MyBatis-参数处理与查询结果映射
java
狂奔小菜鸡1 天前
Day40 | Java中的ReadWriteLock读写锁
java·后端·java ee
SimonKing1 天前
JetBrains 用户狂喜!这个 AI 插件让 IDE 原地进化成「智能编码助手」
java·后端·程序员