Interceptor
是MyBatis提供的一个插件(plugin扩展)。代表拦截器,可以拦截代码中的数据库访问操作,即Statement操作
拦截后,可以去修改正在执行的SQL语句,可以额外访问数据库,可以实现若干数据的计算和处理。
使用场景并不多,针对某类型的SQL实现拦截工具。粒度太粗糙,影响执行效率。
具体代码
自定义拦截器(需实现Interceptor接口,借助注解实现)
//声明拦截器
@Intercepts(value = {
/**
* 要拦截的具体类型
* method:query对应statement中的executeQuery(),update 对应statement中的executeUpdate()
* type:拦截的驱动的类型,Executor是MyBatis提供的底层访问数据库时的封装接口,类似于JDBC的Statement
* args:通知MyBatis框架,当前拦截器中拦截方法intercept参数invocation需要提供多少个参数
*/
@Signature(method = "query", type=Executor.class,args= {MappedStatement.class,Object.class,RowBounds.class,ResultHandler.class})
}
)
public class PageInterceptor implements Interceptor{
@Override
public Object interce