技術(shù)員聯(lián)盟提供win764位系統(tǒng)下載,win10,win7,xp,裝機(jī)純凈版,64位旗艦版,綠色軟件,免費(fèi)軟件下載基地!

當(dāng)前位置:主頁(yè) > 教程 > 服務(wù)器類 >

MyBatis攔截器:給參數(shù)對(duì)象屬性賦值的實(shí)例

來(lái)源:技術(shù)員聯(lián)盟┆發(fā)布時(shí)間:2017-11-03 12:14┆點(diǎn)擊:

  該攔截器的作用: 在進(jìn)行增加、修改等操作時(shí),給數(shù)據(jù)模型的一些通用操作屬性(如:創(chuàng)建人、創(chuàng)建時(shí)間、修改人、修改時(shí)間等)自動(dòng)賦值。

  該實(shí)現(xiàn)是在DAO層攔截,即存入DB前最后一層。后經(jīng)分析,不是很合理,改為在service層攔截,用spring AOP來(lái)實(shí)現(xiàn)了,該代碼遂棄用。不過(guò)已經(jīng)測(cè)試可用,記錄備忘。

  package com.development;

  import java.lang.reflect.InvocationTargetException;

  import java.util.Date;

  import java.util.Map;

  import java.util.Properties;

  import org.apache.commons.beanutils.BeanUtils;

  import org.apache.ibatis.executor.Executor;

  import org.apache.ibatis.mapping.MappedStatement;

  import org.apache.ibatis.mapping.SqlCommandType;

  import org.apache.ibatis.plugin.Interceptor;

  import org.apache.ibatis.plugin.Intercepts;

  import org.apache.ibatis.plugin.Invocation;

  import org.apache.ibatis.plugin.Plugin;

  import org.apache.ibatis.plugin.Signature;

  /**

  * 攔截器作用:給各實(shí)體對(duì)象在增加、修改時(shí),自動(dòng)添加操作屬性信息。

  */

  @Intercepts({@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class }) })

  public class OpeInfoInterceptor implements Interceptor

  {

  public Object intercept(Invocation invocation) throws Throwable

  {

  Object[] args = invocation.getArgs();

  System.out.println("-----------參數(shù)攔截---------------------------------------------------");

  System.out.println("02 當(dāng)前線程ID:"+Thread.currentThread().getId());

  //遍歷處理所有參數(shù),update方法有兩個(gè)參數(shù),參見(jiàn)Executor類中的update()方法。

  for(int i=0;i

  {

  Object arg=args[i];

  String className=arg.getClass().getName();

  System.out.println(i + " 參數(shù)類型:"+className);

  //第一個(gè)參數(shù)處理。根據(jù)它判斷是否給“操作屬性”賦值。

  if(arg instanceof MappedStatement)

  {//如果是第一個(gè)參數(shù) MappedStatement

  MappedStatement ms = (MappedStatement)arg;

  SqlCommandType sqlCommandType = ms.getSqlCommandType();

  System.out.println("操作類型:"+sqlCommandType);

  if(sqlCommandType == SqlCommandType.INSERT || sqlCommandType==SqlCommandType.UPDATE)

  {//如果是“增加”或“更新”操作,則繼續(xù)進(jìn)行默認(rèn)操作信息賦值。否則,則退出

  continue;

  }

  else

  {

  break;

  }

  }

  //第二個(gè)參數(shù)處理。(只有第二個(gè)程序才能跑到這)

  if (arg instanceof Map)

  {//如果是map,有兩種情況:(1)使用@Param多參數(shù)傳入,由Mybatis包裝成map。(2)原始傳入Map

  System.out.println("這是一個(gè)包裝過(guò)的類型!");

  Map map=(Map)arg;

  for (Object obj : map.values())

  {

  setProperty(obj);

  }

  }

  else

  {//原始參數(shù)傳入

  setProperty(arg);

  }

  }

  return invocation.proceed();

  }

  /**

  * 為對(duì)象的操作屬性賦值

  * @param obj

  */

  private void setProperty(Object obj)

  {

  try

  {

  //TODO: 根據(jù)需要,將相關(guān)屬性賦上默認(rèn)值

  BeanUtils.setProperty(obj, "createrUsername", "張三");

  BeanUtils.setProperty(obj, "createDT", new Date());

  }

  catch (IllegalAccessException e)

  {

  e.printStackTrace();

  }

  catch (InvocationTargetException e)

  {

  e.printStackTrace();

  }

  }

  public Object plugin(Object target)

  {

  return Plugin.wrap(target, this);

  }

  public void setProperties(Properties properties)