/** * Copyright © 2015-2020 JeePlus All rights reserved. */ package com.jeeplus.common.persistence.interceptor; import org.apache.ibatis.executor.statement.BaseStatementHandler; import org.apache.ibatis.executor.statement.RoutingStatementHandler; import org.apache.ibatis.executor.statement.StatementHandler; import org.apache.ibatis.mapping.BoundSql; import org.apache.ibatis.mapping.MappedStatement; 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 com.jeeplus.common.persistence.Page; import com.jeeplus.common.utils.Reflections; import java.sql.Connection; import java.util.Properties; /** * Mybatis数据库分页插件,拦截StatementHandler的prepare方法 * @author poplar.yfyang / thinkgem * @version 2013-8-28 */ @Intercepts({ @Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class}) }) public class PreparePaginationInterceptor extends BaseInterceptor { private static final long serialVersionUID = 1L; public PreparePaginationInterceptor() { super(); } @Override public Object intercept(Invocation ivk) throws Throwable { if (ivk.getTarget().getClass().isAssignableFrom(RoutingStatementHandler.class)) { final RoutingStatementHandler statementHandler = (RoutingStatementHandler) ivk.getTarget(); final BaseStatementHandler delegate = (BaseStatementHandler) Reflections.getFieldValue(statementHandler, DELEGATE); final MappedStatement mappedStatement = (MappedStatement) Reflections.getFieldValue(delegate, MAPPED_STATEMENT); // //拦截需要分页的SQL //// if (mappedStatement.getId().matches(_SQL_PATTERN)) { // if (StringUtils.indexOfIgnoreCase(mappedStatement.getId(), _SQL_PATTERN) != -1) { BoundSql boundSql = delegate.getBoundSql(); //分页SQL