springboot+mybatis自定义拦截器实现分页查询

本文介绍了如何在SpringBoot+Mybatis环境下通过自定义拦截器实现分页查询功能,详细讲解了自定义分页参数bean、Interceptor接口的实现以及在mybatis配置文件中的设置,并提供了测试案例。
摘要由CSDN通过智能技术生成

springboot+mybatis自定义拦截器实现分页查询

这篇文章主要介绍自定义拦截器实现分页查询,网上有很多类似的资料,但当自己实际参考使用的时候,遇到了很多问题,在此做个记录,希望可以帮到遇到同样问题的同学。
数据库:mysql-5.7.20
mybatis:2.0.0
springboot:1.5.9.RELEASE

自定义分页参数bean

public class PageBean<T> {
   

    /**
     * 当前页
     */
    private int currentPage = 1;

    /**
     * 每页条数
     */
    private int pageSize = 10;

    /**
     * 总页数
     */
    private int pageCount;

    /**
     * 总条数
     */
    private int totalCount;

    private List<T> data;
	
	public void setTotalCount(int totalCount) {
   
        this.totalCount = totalCount;
        this.calPageCount();
    }

    public void calPageCount() {
   
        this.pageCount = (int) Math.ceil((this.totalCount * 1.0) / this.pageSize);
    }
    //其他set/get

实现Interceptor接口

@Component
@Intercepts({
    @Signature(type = StatementHandler.class, 
						 method = "prepare", args = {
    Connection.class, Integer.class }) })
public class PageInterceptor implements Interceptor {
   

拦截的对象为StatementHandler类的prepare方法,方法的参数为:Connection、Integer

public interface StatementHandler {
   
    Statement prepare(Connection var1, Integer var2) throws SQLException;

实现Interceptor接口的intercept方法

@Override
    public Object 
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值