1. 程式人生 > >Mybatis Plugin(攔截器)的開發

Mybatis Plugin(攔截器)的開發

nsa commit 步驟 ring rac oot div eth 我們

1.Plugin MyBatis 允許使用插件來攔截的方法調用包括:
• Executor (update, query, flushStatements, commit, rollback,
getTransaction, close, isClosed)
• ParameterHandler (getParameterObject, setParameters)
• ResultSetHandler (handleResultSets, handleOutputParameters)
• StatementHandler (prepare, parameterize, batch, update, query)
註意;可以通過插件攔截到這四個對象,修改參數等操作: 你必須要知道的類:
  1. org.apache.ibatis.plugin.Plugin
  2. org.apache.ibatis.reflection.SystemMetaObject
2.使用步驟
    1. 實現 Interceptor 接口
      1. 三個方法執行順序
        1. setProperties()
        2. plugin()
        3. intercept()
 1
FirstIntercepter=====>setProperties 2 FirstIntercepter====>pluginorg.apache.ibatis.executor.CachingExecutor@64485a47 3 FirstIntercepter====>pluginorg.apache.ibatis.scripting.defaults.DefaultParameterHandler@2f0a87b3 4 FirstIntercepter====>pluginorg.apache.ibatis.executor.resultset.DefaultResultSetHandler@4fcd19b3
5 FirstIntercepter====>pluginorg.apache.ibatis.executor.statement.RoutingStatementHandler@2fd66ad3 6 DEBUG 09-05 11:56:24,696 ==> Preparing: select * from employee where id=? (BaseJdbcLogger.java:159) 7 FirstIntercepter:===>intercept 8 DEBUG 09-05 11:56:24,722 ==> Parameters: 1(Integer) (BaseJdbcLogger.java:159) 9 DEBUG 09-05 11:56:24,739 <== Total: 1 (BaseJdbcLogger.java:159) 10 Employee [id=1, lastName=tom, gender=1, email[email protected], depid=null]

    1. 給你的攔截器簽名:
 1 /**
 2 * 完成插件簽名:
 3 * 告訴MyBatis當前插件用來攔截哪個對象的哪個方法
 4 * type:要攔截的四大類型
 5 * method:攔截那個方法
 6 * args:這個方法的入參
 7 * */
 8 @Intercepts({
 9 @Signature(type=StatementHandler.class,
10 method="parameterize",
11 args=java.sql.Statement.class
12 )
13 })
14 public class FirstIntercepter implements Interceptor

    1. mybatis-cfg.xml中配置插件
      1. 這裏註意配置plugins的標簽順序,以免出錯,在environments上面
<!-- plugins 插件的配置 實際上是使用:intercepter原理代理的 -->
<plugins>
<plugin interceptor="mybatis.intercepter.FirstIntercepter">
<property name="param1" value="root"/>
<property name="param2" value="root"/>
</plugin>
</plugins>

3.多個插件的執行
  1. 、多個插件依次生成目標對象的代理對象,層層包裹,先聲明的先包裹;形成代理鏈
  2. 、可以理解為:初始化執行
  3. 執行log
  4. FirstIntercepter=====>setProperties
 1 MySecondIntercepter====>setProperties:{param1=root}
 2 FirstIntercepter====>pluginorg.apache.ibatis.executor.CachingExecutor@64485a47
 3 MySecondIntercepter====>plugin:org.apache.ibatis.executor.CachingExecutor@64485a47
 4 FirstIntercepter====>pluginorg.apache.ibatis.scripting.defaults.DefaultParameterHandler@2f0a87b3
 5 MySecondIntercepter====>plugin:org.apache.ibatis.scripting.defaults.DefaultParameterHandler@2f0a87b3
 6 FirstIntercepter====>pluginorg.apache.ibatis.executor.resultset.DefaultResultSetHandler@4fcd19b3
 7 MySecondIntercepter====>plugin:org.apache.ibatis.executor.resultset.DefaultResultSetHandler@4fcd19b3
 8 FirstIntercepter====>pluginorg.apache.ibatis.executor.statement.RoutingStatementHandler@2fd66ad3
 9 MySecondIntercepter====>plugin:org.apache.ibatis.executor.statement.RoutingStatementHandler@2fd66ad3
10 DEBUG 09-05 12:07:01,928 ==> Preparing: select * from employee where id=? (BaseJdbcLogger.java:159)
11 MySecondIntercepter====>intercept:public abstract void org.apache.ibatis.executor.statement.StatementHandler.parameterize(java.sql.Statement) throws java.sql.SQLException
12 FirstIntercepter:===>intercept
13 DEBUG 09-05 12:07:01,954 ==> Parameters: 1(Integer) (BaseJdbcLogger.java:159)
14 DEBUG 09-05 12:07:01,968 <== Total: 1 (BaseJdbcLogger.java:159)
15 Employee [id=1, lastName=tom, gender=1, email[email protected], depid=null]

4.實現攔截修改參數
  • sql
<!-- Employee getSelectEmp(Integer id); -->
<select id="getSelectEmp" parameterType="java.lang.Integer"
resultType="mybatis.bean.Employee">
select * from employee where id=#{id}
</select>

  • 這裏我們攔截id:
由於ibatis中參數的聲明存在與 StatementHandler中所以註意簽名
  • 1 @Intercepts({
    2 @Signature(type=StatementHandler.class,
    3 method="parameterize",
    4 args=java.sql.Statement.class
    5 )
    6 })

    業務邏輯intercept方法中
 1 /**
 2 * 1:業務邏輯處理的方法:
 3 */
 4 @Override
 5 public Object intercept(Invocation invocation) throws Throwable {
 6 //在這裏可以進行業務邏輯修改
 7 System.out.println("FirstIntercepter:===>intercept"+invocation.getMethod());
 8  
 9 MetaObject metaObject = SystemMetaObject.forObject(invocation.getTarget());
10 //拿到target的元數據 StatementHandler==>ParameterHandler===>
11 //DefaultParameterHandler==>>parameterObject
12 Object value = metaObject.getValue("parameterHandler.parameterObject");
13 System.out.println("sql "+value.toString());
14 //修改完sql語句要用的參數
15 metaObject.setValue("parameterHandler.parameterObject", 2);
16 Object object = invocation.proceed();
17 return object;
18 }

  • 打印log,
    • 可以看到原來入參為1,現在經過攔截器修改入參為2
1 DEBUG 09-05 12:36:23,387 ==> Preparing: select * from employee where id=? (BaseJdbcLogger.java:159)
2 MySecondIntercepter====>intercept:public abstract void org.apache.ibatis.executor.statement.StatementHandler.parameterize(java.sql.Statement) throws java.sql.SQLException
3 FirstIntercepter:===>interceptpublic abstract void org.apache.ibatis.executor.statement.StatementHandler.parameterize(java.sql.Statement) throws java.sql.SQLException
4 sql 1
5 DEBUG 09-05 12:36:23,418 ==> Parameters: 2(Integer) (BaseJdbcLogger.java:159)
6 DEBUG 09-05 12:36:23,432 <== Total: 1 (BaseJdbcLogger.java:159)
7 Employee [id=2, lastName=cat, gender=0, email[email protected], depid=null]

Mybatis Plugin(攔截器)的開發