1 Mybatis允许在已映射语句执行过程中的某一点进行拦截调用.默认情况下,MyBatis允许插件拦截的接口和方法包括以下几个:
-
Executor(update,query,flushStatements,commit,rollback,getTransaction,close,isClosed)
-
ParameterHandler(getParameterObject,setParameters)
-
ResultSetHandler(handleResultSets,handleCursorResultSets,handleOutputParameters)
-
StatmentHandler(prepare,parameterize,batch,update,query)
2 插件开发步骤
1)实现Interceptor(org.apache.ibatis.plugin.Interceptor)拦截器接口,接口如下
public interface Interceptor{
Object intercept(Invocation invocation) throws Throwable;
Object plugin(Object target);
void setProperties(Properties properties);
}
2)配置拦截器
xml方式,在mybatis-config.xml增加如下配置:
<plugins>
<plugin interceptor="com.musi.interceptor.MyBatisInterceptor">
<property name="prop1" value="value1"/ >
<property name="prop2" value="value2" />
</plugin>
</plugins>
java方式配置:
3 Interceptor详解
setProperties:这个方法用来传递插件的参数,拦截器在初始化时会将配置中的参数通过setProperties传递给拦截器,在拦截器中可以很方便的通过Properties取得配置的值.
plugin:这个方法的参数target就是拦截器拦截的对象,该方法会在创建被拦截的接口实现类时被调用.该方法的实现很简单,只需要调用MyBatis提供的Plugin(org.apache.ibatis.plugin.Plugin)类的wrap静态方法就可以通过Java的动态代理拦截对象.这个接口方法通常的实现代码如下:
@Override
public Object plugin(Object target) {
return Plugin.wrap(target,this);
}
intercept(Invocation invocation):MyBatis运行时要执行的方法.通过该方法的参数invocation可以得到很多有用的信息,该参数常用的参数如下:
@Override
public Object intercept(Invocation invocation) throws Throwable {
Object target = invocation.getTarget();
Method method = invocation.getMethod();
Object[] args = invocation.getArgs();
Object result = invocation.proceed();
return result;
}
当配置多个拦截器时,Mybatis会遍历所有拦截器,按顺序执行拦截器的plugin方法,被拦截的对象会被层层代理.
4 拦截器签名
除了需要实现拦截器接口外,还需要给实现类配置以下的注解
@Intercepts({@Signature(type = ResultSetHandler.class, method = "handleResults", args = {Statement.class})})
public class MyBatisInterceptor implements Interceptor {
@Intercepts(org.apache.ibatis.plugin.Intercepts)的属性是一个@Signature数组,可以在同一个拦截器中同时拦截不同的方法和接口
@Signature注解包含以下三个属性:
type:设置拦截的接口,可选值是前面提到的四个接口
method:设置拦截接口中的方法名
args:设置拦截方法的参数类型数组,通过方法名和参数类型可以确定唯一一个方法
5 Executor接口
int update(MappedStatement ms,Object parameter) throws SQLException
该方法会在所有的INSERT,UPDATE,DELETE执行时被调用,因此如果想拦截这3类操作,可以拦截该方法.接口方法对应的签名如下:
@Signature(type=Executor.class, method=“update”, args={MappedStatement.class,Object.class})
<E> List<E> query(MappedStatement ms,Object parameter, RowBounds
rowBounds, ResultHandler resultHandler) throws SQLException
该方法会在所有SELECT查询方法执行时被调用.需要注意的是,接口中还有一个参数更多的同名接口,但是由于MyBatis的设计原因,这个参数多的的接口不能被拦截.接口方法对应的签名如下:
@Signature(type=Executor.class,method=“query”,args={MappedStatement.class,Object class,RowBounds.class,ResultHandler.class})
<E> Cursor<E> queryCursor(MappedStatement ms,Object parameter, RowBounds rowBounds) throws SQLException;//该方法只有在查询的返回值类型为Cursor时被调用
List<BatchResult> flushStatements() throws SQLException //该方法只在通过sqlSession方法调用flushStatments方法或执行的接口方法中带有@Flush注解时才被调用
void commit(boolean required) throws SQLException//该方法只在通过SQLSession方法调用commit方法时才被调用.
void rollback(boolean required) throws SQLException;//该方法只在通过sqlSession方法调用rollback方法时才被调用.
Transaction getTransaction() //该方法只在通过sqlSession方法获取数据库连接时才被调用.
void close(boolean forceRollBack);//该方法只在延迟加载获取新的Executor后才会被执行
boolean close();//该方法只在延迟记载执行查询方法前被执行.
6 ParameterHandler接口
Object getParameterObject();//该方法只在执行存储过程处理出参的时候被调用.
void setParameters(PreparedStatement ps) throws SQLException;//该方法在所有数据库执行方法设置SQL参数时被调用.
7 ResultSetHandler接口
<E> List<E> handleResultSets(Statement stmt) throws SQLException;//该方法会在除存储过程及返回值类型为Cursor<T>(org.apache.ibatis.cursor.Cursor<T>)以外的查询方法中调用.
<E> Cursor<E> handleCursorResultSets(Statement stmt) throws SQLException;//该方法只会在返回值类型为Cursor<T>的查询方法中被调用
void handleOutputParameters(CallableStatement cs) throws SQLException;//该方法只在使用存储过程处理出参时被调用.
8 StatementHandler接口
Statement prepare(Connection connection,Integer transactionTimeout) throws Exception;//该方法会在数据库执行前被调用,优先于当前接口中的其他方法而被执行.
void parameterize(Statement statement) throws SQLException;//该方法在prepare方法之后执行,用于处理参数信息.
int batch(Statement statement) throws SQLException;//在全局设置配置defaultExecutorType="BATCH"时,执行数据操作才会调用该方法.
<E> List<E> query(Statement statement, ResultHandler resultHandler) throw SQLException;//执行SELECT方法时被调用
<E> Cursor<E> queryCursor(Statement statement) throws SQLException;//只在返回值类型为Cursor<T>的查询中被调用.