Mybatis自定义插件-Interceptor
MyBatis 允许你在映射语句执行过程中的某一点进行拦截调用。默认情况下,MyBatis 允许使用插件来拦截的方法调用包括:
Executor (update, query, flushStatements, commit, rollback, getTransaction, close, isClosed)
ParameterHandler (getParameterObject, setParameters)
ResultSetHandler (handleResultSets, handleOutputParameters)
StatementHandler (prepare, parameterize, batch, update, query)
这些类中方法的细节可以通过查看每个方法的签名来发现,或者直接查看 MyBatis 发行包中的源代码。 如果你想做的不仅仅是监控方法的调用,那么你最好相当了解要重写的方法的行为。 因为在试图修改或重写已有方法的行为时,很可能会破坏 MyBatis 的核心模块。 这些都是更底层的类和方法,所以使用插件的时候要特别当心。
通过 MyBatis 提供的强大机制,使用插件是非常简单的,只需实现 Interceptor 接口,并指定想要拦截的方法签名即.
@Intercepts(value = {@Signature(
type = Executor.class,
method = "query",
args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})})
public class MyInterceptor implements Interceptor {
// @Autowired
private Logger log = new LoggerStudoImpl();
@Override
public Object intercept(Invocation invocation) throws Throwable {
// 该方法写入自己的逻辑
Object[] queryArgs = invocation.getArgs();
MappedStatement ms = (MappedStatement) queryArgs[0];
BoundSql boundSql = ms.getBoundSql(queryArgs[1]);
String sql = boundSql.getSql();
String SQL = new StringBuilder(sql).append(" ").append("and deleted = '0'").toString();
StaticSqlSource rawSqlSource = new StaticSqlSource(ms.getConfiguration(), SQL, boundSql.getParameterMappings());
MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), ms.getId(), rawSqlSource, 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());
MappedStatement statement = builder.build();
queryArgs[0] = statement;
log.error(SQL);
return invocation.proceed();
}
@Override
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
@Override
public void setProperties(Properties properties) {
}
}
然后将自定义插件添加到Mybatis配置文件中,该插件就会生效。
<plugins>
<plugin interceptor="com.example.shiro.config.MyInterceptor"></plugin>
</plugins>
自定义插件拦截的是Excutor中的query方法,也就是说只要是Mybatis执行查询,那么该插件就会拦截查询的SQL语句,将查询的SQL添加上and deleted = ‘0’,这样每次查询就无需加上deleted='0’了。
比如一些分页插件就是拦截的ReultSetHandle进而将查询的结果进行分页处理。