mybatis的插件分析
mybatis的插件分析
mybatis插件回在解析配置是通過pluginAll方法將插件添加到插件鏈中,然后會在sqlSessionfactory.openSession()方法中將插件鏈綁到executor上,在執行sql的時候回攔截具體方法后,通過代理類來進行具體處理。
?
官方文檔:http://www.mybatis.org/mybatis-3/zh/configuration.html#plugins
?
通過 MyBatis 提供的強大機制,使用插件是非常簡單的,只需實現 Interceptor 接口,并指定想要攔截的方法簽名即可
官方例子
// ExamplePlugin.java @Intercepts({@Signature(type= Executor.class,method = "update",args = {MappedStatement.class,Object.class})}) public class ExamplePlugin implements Interceptor {public Object intercept(Invocation invocation) throws Throwable {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>上面的插件將會攔截在 Executor 實例中所有的 “update” 方法調用, 這里的 Executor 是負責執行低層映射語句的內部對象
步驟:
1、實現interceptor接口
2、在自定義plugin上配置注解@Intercepts
注解配置:@Intercepts({ @Signature(type = Executor.class, method = "query", args = { MappedStatement.class, Object.class,RowBounds.class, ResultHandler.class }) })
type:Executor、StatementHandler、ParameterHandler、ResultsetHandler四個接口
method:以上四個接口中的方法
args:以上方法中的參數
3、mybatis.xml中配置標簽<plugin><>
?
?
原理思想分析可參考:https://blog.csdn.net/reliveit/article/details/50289395
?
posted @ 2018-06-07 12:18 犇犇丶 閱讀(...) 評論(...) 編輯 收藏總結
以上是生活随笔為你收集整理的mybatis的插件分析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: web.xml中的ContextLoad
- 下一篇: mybatis初始化过程