MyBatis攔截器:給參數對象屬性賦值


  該攔截器的作用:在進行增加、修改等操作時,給數據模型的一些通用操作屬性(如:創建人、創建時間、修改人、修改時間等)自動賦值。

  該實現是在DAO層攔截,即存入DB前最后一層。后經分析,不是很合理,改為在service層攔截,用spring AOP來實現了,該代碼遂棄用。不過已經測試可用,記錄備忘。

  1 package com.development;
  2 
  3 import java.lang.reflect.InvocationTargetException;
  4 import java.util.Date;
  5 import java.util.Map;
  6 import java.util.Properties;
  7 
  8 import org.apache.commons.beanutils.BeanUtils;
  9 import org.apache.ibatis.executor.Executor;
 10 import org.apache.ibatis.mapping.MappedStatement;
 11 import org.apache.ibatis.mapping.SqlCommandType;
 12 import org.apache.ibatis.plugin.Interceptor;
 13 import org.apache.ibatis.plugin.Intercepts;
 14 import org.apache.ibatis.plugin.Invocation;
 15 import org.apache.ibatis.plugin.Plugin;
 16 import org.apache.ibatis.plugin.Signature;
 17 
 18 /**
 19  * 攔截器作用:給各實體對象在增加、修改時,自動添加操作屬性信息。
 20  */
 21 @Intercepts({@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class }) })
 22 public class OpeInfoInterceptor implements Interceptor
 23 {
 24 
 25     public Object intercept(Invocation invocation) throws Throwable
 26     {
 27         Object[] args = invocation.getArgs();
 28 
 29         System.out.println("-----------參數攔截---------------------------------------------------");
 30         System.out.println("02 當前線程ID:"+Thread.currentThread().getId());
 31         //遍歷處理所有參數,update方法有兩個參數,參見Executor類中的update()方法。
 32         for(int i=0;i<args.length;i++)
 33         {
 34             Object arg=args[i];
 35             String className=arg.getClass().getName();
 36             System.out.println(i + " 參數類型:"+className);
 37             
 38             //第一個參數處理。根據它判斷是否給“操作屬性”賦值。
 39             if(arg instanceof MappedStatement)
 40             {//如果是第一個參數 MappedStatement
 41                 MappedStatement ms = (MappedStatement)arg;
 42                 SqlCommandType sqlCommandType = ms.getSqlCommandType();
 43                 System.out.println("操作類型:"+sqlCommandType);
 44                 if(sqlCommandType == SqlCommandType.INSERT || sqlCommandType==SqlCommandType.UPDATE)
 45                 {//如果是“增加”或“更新”操作,則繼續進行默認操作信息賦值。否則,則退出
 46                     continue;
 47                 }
 48                 else
 49                 {
 50                     break;
 51                 }
 52             }
 53             
 54             //第二個參數處理。(只有第二個程序才能跑到這)
 55             if (arg instanceof Map) 
 56             {//如果是map,有兩種情況:(1)使用@Param多參數傳入,由Mybatis包裝成map。(2)原始傳入Map
 57                 System.out.println("這是一個包裝過的類型!");
 58                 Map map=(Map)arg;
 59                 for (Object obj : map.values()) 
 60                 {  
 61                     setProperty(obj);
 62                 } 
 63             }
 64             else
 65             {//原始參數傳入
 66                 setProperty(arg);
 67             }
 68             
 69         }
 70 
 71         return invocation.proceed();
 72 
 73     }
 74     
 75     /**
 76      * 為對象的操作屬性賦值
 77      * @param obj
 78      */
 79     private void setProperty(Object obj)
 80     {
 81         try
 82         {
 83             //TODO: 根據需要,將相關屬性賦上默認值
 84             BeanUtils.setProperty(obj, "createrUsername", "張三");
 85             BeanUtils.setProperty(obj, "createDT", new Date());
 86         }
 87         catch (IllegalAccessException e)
 88         {
 89             e.printStackTrace();
 90         }
 91         catch (InvocationTargetException e)
 92         {
 93             e.printStackTrace();
 94         }
 95     }
 96 
 97     public Object plugin(Object target)
 98     {
 99         return Plugin.wrap(target, this);
100     }
101 
102     public void setProperties(Properties properties)
103     {
104 
105     }
106 
107 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM