MyBatis 分页插件 pagehelper 及 动态接口插件开发

119 阅读1分钟

分页插件pagehelper使用

1、pom.xml添加依赖

<dependencies>
    <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper</artifactId>
        <version>5.3.2</version>
    </dependency>

</dependencies>

2、mybatis 配置文件配置插件

<!--        注册分页插件-->
        <plugin interceptor="com.github.pagehelper.PageInterceptor">

        </plugin>
    </plugins>
    <environments default="development">

3、使用插件

@Test
public void Test011(){
    EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);

    PageHelper.startPage(1,5);

    List<Emp11> emp11s = mapper.queryEmp();
    System.out.println(emp11s);
    System.out.println("#####################################");
    PageInfo<Emp11>  emp11PageInfo = new PageInfo<>(emp11s,10);
    System.out.println(emp11PageInfo);

    System.out.println("√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√√");
    System.out.println("当前页:"+emp11PageInfo.getPageNum());
    System.out.println("上一页:"+emp11PageInfo.getPrePage());
    System.out.println("下一页:"+emp11PageInfo.getNextPage());
    System.out.println("总条数:"+emp11PageInfo.getTotal());
    //System.out.println(emp11PageInfo.getSize());
    System.out.println("是否最后一页"+emp11PageInfo.isIsLastPage());
    System.out.println("页码:"+Arrays.toString(emp11PageInfo.getNavigatepageNums()));
    for(int i : emp11PageInfo.getNavigatepageNums()){
        System.out.println("第"+i+"页");
    }
}

4 扩展插件

package com.kdy.interceptor;

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.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

@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();
//        System.out.println(args);
//
//        for(Object obj : args){
//            System.out.println(obj);
//        }

        MappedStatement ms = (MappedStatement)args[0];
        Object parameterObject = args[1];
        BoundSql boundSql = ms.getBoundSql(args[1]);
        RowBounds rowBounds = (RowBounds)args[2];

        String sql = boundSql.getSql();
        String limitsql =sql+  " limit " + rowBounds.getOffset()+","+rowBounds.getLimit();
        BoundSql boundSql1 = new BoundSql(ms.getConfiguration(), limitsql, boundSql.getParameterMappings(), parameterObject);
        Executor target = (Executor)invocation.getTarget();
        CacheKey cacheKey = target.createCacheKey(ms, parameterObject, rowBounds, boundSql1);
        return target.query(ms,parameterObject,rowBounds,(ResultHandler) args[3],cacheKey,boundSql1);
        //return invocation.proceed();
    }
}