裴翠翠怎么死的,巅峰公子,又名怀化站长网
作用
官方说明:
mybatis 允许你在已映射语句执行过程中的某一点进行拦截调用。
什么意思呢?就是你可以对执行某些方法之前进行拦截,做自己的一些操作,如:
1.记录所有执行的sql(通过对 mybatis org.apache.ibatis.executor.statement.statementhandler 中的prepare 方法进行拦截)
2.修改sql(org.apache.ibatis.executor.executor中query方法进行拦截)等。
但拦截的方法调用有限制,mybatis 允许使用插件来拦截的方法调用包括:
实现
使用插件是非常简单的,只需实现 interceptor 接口,并指定想要拦截的方法签名即可。
// exampleplugin.java @intercepts({@signature( type= executor.class, method = "update", args = {mappedstatement.class,object.class}, @signature( type = executor.class, //必须为上面所支持的类 method = "query", //类中支持的方法,可从源码中查看支持哪些方法 args = {mappedstatement.class, object.class , rowbounds.class, resulthandler.class})}) //对应的参数class,也可从源码中查看 public class exampleplugin implements interceptor { public object intercept(invocation invocation) throws throwable { object[] queryargs = invocation.getargs(); mappedstatement mappedstatement = (mappedstatement) queryargs[0]; object parameter = queryargs[1]; boundsql boundsql = mappedstatement.getboundsql(parameter); string sql = boundsql.getsql();//获取到sql ,可以进行调整 string name = invocation.getmethod().getname(); queryargs[1] = 2; //可以修改参数内容 system.err.println("拦截的方法名是:" + name); return invocation.proceed(); } public object plugin(object target) { return plugin.wrap(target, this); } public void setproperties(properties properties) { } }
在配置文件中注册插件
<!-- mybatis-config.xml --> <plugins> <plugin interceptor="org.mybatis.example.exampleplugin"> <property name="someproperty" value="100"/> </plugin> </plugins>
当我们调用query方法时,匹配拦截器的方法, 所以会执行拦截器下intercept方法,做自己的处理。
参考资料,官网
总结
以上所述是小编给大家介绍的mybatis自定义plugin插件,希望对大家有所帮助
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
浅析我对 String、StringBuilder、StringBuffer 的理解
使用IDEA搭建SSM框架的详细教程(spring + springMVC +MyBatis)
Springboot整合freemarker 404问题解决方案
引入mybatis-plus报 Invalid bound statement错误问题的解决方法
网友评论