Mybatis-plus: MP加了二级缓存后,分页失效问题的建议

Created on 24 Jul 2019  ·  2Comments  ·  Source: baomidou/mybatis-plus

当前使用版本(必须填写清楚,否则不予处理)

mybatis-plus 3.1.2

该问题是怎么引起的?(最新版上已修复的会直接close掉)

MP加上redis二级缓存后,当有缓存时,分页功能失效,返回的总数Total都是0.

建议

既然这个问题暂时处理不了,是否可以用分页查询时,强制不用缓存。
在MybatisMapperMethod类的executeForIPage(SqlSession sqlSession, Object[] args)这个方法里,是否能加上useCache=false的配置。
image

Most helpful comment

自己加了个拦截器解决了,有分页参数时不使用缓存。

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.binding.MapperMethod;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @author huangqinghe
 * @date 2019/7/24.
 */
@Intercepts({@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class,
        RowBounds.class, ResultHandler.class})})
public class PageSettingCacheInterceptor implements Interceptor {

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        Object[] args = invocation.getArgs();
        MappedStatement ms = (MappedStatement) args[0];
        if (args.length > 1 & args[1] instanceof Map) {
            HashMap<String, Object> paramMap = (HashMap<String, Object>) args[1];
            boolean matches = paramMap.entrySet().stream().anyMatch(entry -> entry.getValue() instanceof Page);
            if (matches) {
                Class<?> clazz = ms.getClass();
                Field useCache = clazz.getDeclaredField("useCache");
                useCache.setAccessible(true);
                useCache.set(ms, false);
            }
        }
        return invocation.proceed();
    }

    @Override
    public Object plugin(Object target) {
        if (target instanceof Executor) {
            return Plugin.wrap(target, this);
        }
        return target;
    }

    @Override
    public void setProperties(Properties properties) {
        //do nothing
    }

}

然后在config类注入Bean

    @Bean
    public PageSettingCacheInterceptor pageSettingCacheInterceptor() {
        return new PageSettingCacheInterceptor();
    }

All 2 comments

自己加了个拦截器解决了,有分页参数时不使用缓存。

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.binding.MapperMethod;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @author huangqinghe
 * @date 2019/7/24.
 */
@Intercepts({@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class,
        RowBounds.class, ResultHandler.class})})
public class PageSettingCacheInterceptor implements Interceptor {

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        Object[] args = invocation.getArgs();
        MappedStatement ms = (MappedStatement) args[0];
        if (args.length > 1 & args[1] instanceof Map) {
            HashMap<String, Object> paramMap = (HashMap<String, Object>) args[1];
            boolean matches = paramMap.entrySet().stream().anyMatch(entry -> entry.getValue() instanceof Page);
            if (matches) {
                Class<?> clazz = ms.getClass();
                Field useCache = clazz.getDeclaredField("useCache");
                useCache.setAccessible(true);
                useCache.set(ms, false);
            }
        }
        return invocation.proceed();
    }

    @Override
    public Object plugin(Object target) {
        if (target instanceof Executor) {
            return Plugin.wrap(target, this);
        }
        return target;
    }

    @Override
    public void setProperties(Properties properties) {
        //do nothing
    }

}

然后在config类注入Bean

    @Bean
    public PageSettingCacheInterceptor pageSettingCacheInterceptor() {
        return new PageSettingCacheInterceptor();
    }

试用 3.2.0

Was this page helpful?
0 / 5 - 0 ratings