• MyBatis:自定义分页插件


    一、mapper

        
    
    • 1
    • 2
    • 3

    二、接口:

        List selectCountry3(RowBounds rowBounds);
    
    • 1

    三、插件定义:

    package cn.edu.tju.config;
    
    import org.apache.ibatis.cache.CacheKey;
    import org.apache.ibatis.executor.Executor;
    import org.apache.ibatis.mapping.BoundSql;
    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.util.Properties;
    
    @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 MyPageInterceptor implements Interceptor {
        @Override
        public Object intercept(Invocation invocation) throws Throwable {
            Object[] args = invocation.getArgs();
    
            MappedStatement ms = (MappedStatement) args[0];
            BoundSql boundSql = ms.getBoundSql(args[1]);
            RowBounds rowBounds = (RowBounds) args[2];
    
            String sql = boundSql.getSql();
            sql += " limit ";
    
            int offset = rowBounds.getOffset();
            int limit = rowBounds.getLimit();
            sql += (offset-1)*limit;
            sql += ",";
            sql += limit;
            System.out.println(sql);
    
            BoundSql newBoundSql = new BoundSql(ms.getConfiguration(), sql,
                    boundSql.getParameterMappings(), boundSql.getParameterObject());
    
    
    
            Executor executor = (Executor) invocation.getTarget();
            return executor.query(ms, args[1], (RowBounds) args[2], (ResultHandler) args[3], null, newBoundSql);
        }
    
        @Override
        public Object plugin(Object target) {
            return Plugin.wrap(target,this);
        }
    
        @Override
        public void setProperties(Properties properties) {
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60

    四、插件注册:

        
    
            
        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    五、查询:

    package cn.edu.tju.test;
    
    import cn.edu.tju.domain.Country;
    import cn.edu.tju.mapper.PersonMapper;
    import com.github.pagehelper.PageHelper;
    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.RowBounds;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    
    import java.io.IOException;
    import java.util.List;
    
    public class MyBatisTest11 {
        public static void main(String[] args) throws IOException {
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(
    
                    Resources.getResourceAsStream("mybatis-config.xml"));
    
    
            SqlSession sqlSession = sqlSessionFactory.openSession();
            List countries = sqlSession.getMapper(PersonMapper.class)
                    .selectCountry3(new RowBounds(1, 2));
            System.out.println(countries.size());
            System.out.println(countries);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
  • 相关阅读:
    【送java面试题】掌握JVM诊断命令,稳定Java应用
    MySQL分区、主从复制,数据库优化
    RabbitMQ-08 不公平分发与预取值
    Java后端面试精选45题
    Matlab-resample
    java毕业设计恩施茶多芬网店系统设计与实现2021Mybatis+系统+数据库+调试部署
    【相机坐标系、ORB_SLAM2坐标系】
    程序员与产品之间应该如何配合,什么时候技术为重,什么时候产品为重?
    爬虫实训案例:中国大学排名
    高薪程序员&面试题精讲系列148之你熟悉哪些加密算法(下篇)?如何保证项目的安全性?
  • 原文地址:https://blog.csdn.net/amadeus_liu2/article/details/132819012