1、新建插件
import org.apache.ibatis.executor.CachingExecutor;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.executor.resultset.ResultSetHandler;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.*;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import java.util.Properties;
@Intercepts({@Signature(
type= Executor.class,
method = "query",
args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})})
public class PagePlugin implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
Object[] args = invocation.getArgs();
System.out.println("-->intercept method: "+invocation.getMethod().getDeclaringClass().getCanonicalName());
System.out.println("-->intercept target: "+invocation.getTarget().getClass().getCanonicalName());
if(args != null){
for(int i=0;i<args.length;i++){
System.out.println("-->intercept args: "+args[i]);
}
}
int INDEX_MS = 0;
MappedStatement ms = (MappedStatement)args[INDEX_MS];
StatementType statementType = ms.getStatementType();
System.out.println("-->intercept statementType: "+statementType );
SqlCommandType sqlCommandType = ms.getSqlCommandType();
System.out.println("-->intercept sqlCommandType: "+sqlCommandType);
SqlSource sqlSource = ms.getSqlSource();
int INDEX_PARAM = 1;
BoundSql boundSql = sqlSource.getBoundSql(args[INDEX_PARAM]);
String sql = boundSql.getSql();
if(sql.contains("where")){
sql=sql.replaceFirst("where","where version=1 and ");
}else if(sql.contains("WHERE")){
sql=sql.replaceFirst("WHERE","where version=1 and ");
}
// 重新new一个查询语句对像
BoundSql newBoundSql = new BoundSql(ms.getConfiguration(), sql, boundSql.getParameterMappings(), boundSql.getParameterObject());
// 把新的查询放到statement里
MappedStatement newMs = copyFromMappedStatement(ms, new BoundSqlSqlSource(newBoundSql));
for (ParameterMapping mapping : boundSql.getParameterMappings()) {
String prop = mapping.getProperty();
if (boundSql.hasAdditionalParameter(prop)) {
newBoundSql.setAdditionalParameter(prop, boundSql.getAdditionalParameter(prop));
}
}
args[INDEX_MS] = newMs;
return invocation.proceed();
}
private MappedStatement copyFromMappedStatement(MappedStatement ms, SqlSource newSqlSource) {
MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), ms.getId(), newSqlSource, ms.getSqlCommandType());
builder.resource(ms.getResource());
builder.fetchSize(ms.getFetchSize());
builder.statementType(ms.getStatementType());
builder.keyGenerator(ms.getKeyGenerator());
if (ms.getKeyProperties() != null && ms.getKeyProperties().length > 0) {
builder.keyProperty(ms.getKeyProperties()[0]);
}
builder.timeout(ms.getTimeout());
builder.parameterMap(ms.getParameterMap());
builder.resultMaps(ms.getResultMaps());
builder.resultSetType(ms.getResultSetType());
builder.cache(ms.getCache());
builder.flushCacheRequired(ms.isFlushCacheRequired());
builder.useCache(ms.isUseCache());
return builder.build();
}
public static class BoundSqlSqlSource implements SqlSource {
private BoundSql boundSql;
public BoundSqlSqlSource(BoundSql boundSql) {
this.boundSql = boundSql;
}
public BoundSql getBoundSql(Object parameterObject) {
return boundSql;
}
}
@Override
public Object plugin(Object target) {
System.out.println("+++++++++++++++++++++++++++++++++++++="+target.getClass().getCanonicalName());
if(target instanceof CachingExecutor){
Executor executor = (Executor) target;
Plugin.wrap(target,this);
}else if(target instanceof ParameterHandler){
ParameterHandler parameterHandler=(ParameterHandler) target;
}else if(target instanceof ResultSetHandler){
ResultSetHandler resultSetHandler = (ResultSetHandler)target;
}else if(target instanceof StatementHandler){
StatementHandler statementHandler = (StatementHandler)target;
BoundSql boundSql = statementHandler.getBoundSql();
}
return Plugin.wrap(target,this);
}
@Override
public void setProperties(Properties properties) {
}
}
2、配置插件
<plugins>
<plugin interceptor="com.jcc.code.read.plugin.PagePlugin"/>
</plugins>