MyBatis拦截器实现分页功能的实现方法
前言:
首先说下实现原理。使用拦截器拦截原始的sql,然后加上分页查询的关键字和属性,拼装成新的sql语句再交给mybatis去执行。
除了业务代码之外,需要写的东西不多,提几个关键的:
1、分页对象Page类。给该对象设置一个当前页数(前端给)、总记录数(拦截器内赋值)2个参数,他就能帮你计算出分页sql语句用的2个参数。
/** * 分页对应的实体类 */public class Page { /** * 总条数 */ private int totalNumber; /** * 当前第几页 */ private int currentPage; /** * 总页数 */ private int totalPage; /** * 每页显示条数 */ private int pageNumber = 5; /** * 数据库中limit的参数,从第几条开始取 */ private int dbIndex; /** * 数据库中limit的参数,一共取多少条 */ private int dbNumber; /** * 根据当前对象中属性值计算并设置相关属性值 */ public void count() { // 计算总页数 int totalPageTemp = this.totalNumber / this.pageNumber; int plus = (this.totalNumber % this.pageNumber) == 0 ? 0 : 1; totalPageTemp = totalPageTemp + plus; if(totalPageTemp <= 0) { totalPageTemp = 1; } this.totalPage = totalPageTemp; // 设置当前页数 // 总页数小于当前页数,应将当前页数设置为总页数 if(this.totalPage < this.currentPage) { this.currentPage = this.totalPage; } // 当前页数小于1设置为1 if(this.currentPage < 1) { this.currentPage = 1; } // 设置limit的参数 this.dbIndex = (this.currentPage - 1) * this.pageNumber; this.dbNumber = this.pageNumber; } public int getTotalNumber() { return totalNumber; } public void setTotalNumber(int totalNumber) { this.totalNumber = totalNumber; this.count(); } public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } public int getTotalPage() { return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } public int getPageNumber() { return pageNumber; } public void setPageNumber(int pageNumber) { this.pageNumber = pageNumber; this.count(); } public int getDbIndex() { return dbIndex; } public void setDbIndex(int dbIndex) { this.dbIndex = dbIndex; } public int getDbNumber() { return dbNumber; } public void setDbNumber(int dbNumber) { this.dbNumber = dbNumber; }} |
2、关键的拦截器实现
package com.imooc.interceptor;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.util.Map;import java.util.Properties;import org.apache.ibatis.executor.parameter.ParameterHandler;import org.apache.ibatis.executor.statement.StatementHandler;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.Plugin;import org.apache.ibatis.plugin.Signature;import org.apache.ibatis.reflection.DefaultReflectorFactory;import org.apache.ibatis.reflection.MetaObject;import org.apache.ibatis.reflection.SystemMetaObject;import com.imooc.entity.Page;/** * 分页拦截器 * * @author Skye * */@Intercepts({ @Signature(type = StatementHandler.class, method = "prepare", args = { Connection.class, Integer.class }) })public class PageInterceptor implements Interceptor { public Object intercept(Invocation invocation) throws Throwable { StatementHandler statementHandler = (StatementHandler) invocation.getTarget(); MetaObject metaObject = MetaObject.forObject(statementHandler, SystemMetaObject.DEFAULT_OBJECT_FACTORY, SystemMetaObject.DEFAULT_OBJECT_WRAPPER_FACTORY, new DefaultReflectorFactory()); MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement"); //通过MetaObject元数据取得方法名id:com.XXX.queryMessageListByPage String id = mappedStatement.getId(); //匹配在mybatis中定义的与分页有关的查询id if (id.matches(".+ByPage$")) { //BoundSql中有原始的sql语句和对应的查询参数 BoundSql boundSql = statementHandler.getBoundSql(); Map<String, Object> params = (Map<String, Object>) boundSql.getParameterObject(); Page page = (Page) params.get("page"); String sql = boundSql.getSql(); String countSql = "select count(*)from (" + sql + ")a"; Connection connection = (Connection) invocation.getArgs()[0]; PreparedStatement countStatement = connection.prepareStatement(countSql); ParameterHandler parameterHandler = (ParameterHandler) metaObject.getValue("delegate.parameterHandler"); parameterHandler.setParameters(countStatement); ResultSet rs = countStatement.executeQuery(); if (rs.next()) { //为什么是getInt(1)? 因为数据表的列是从1开始计数 page.setTotalNumber(rs.getInt(1)); System.out.println("拦截器得知page的记录总数为:" + page.getTotalNumber()); } String pageSql = sql + " limit " + page.getDbIndex() + "," + page.getDbNumber(); metaObject.setValue("delegate.boundSql.sql", pageSql); } return invocation.proceed(); } /** * @param target * 被拦截的对象 */ public Object plugin(Object target) { // 如果将拦截器类比喻为代购票的公司,那this就是代购业务员(进入方法前是无代理购票能力业务员,进入后成为有代理能力的业务员) // 通过注解获取拦截目标的信息,如果不符合拦截要求就返回原目标,如果符合则使用动态代理生成代理对象 return Plugin.wrap(target, this); } public void setProperties(Properties properties) { // TODO Auto-generated method stub }} |