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
pub