MyBatis interceptor: An instance that assigns a property to a parameter object

  • 2020-06-23 00:23:49
  • OfStack

The purpose of the interceptor is to automatically assign some common operation attributes (such as creator, creation time, modifier, modification time, etc.) to the data model when adding, modifying, etc.

This implementation intercepts at the DAO layer, the last layer before storing DB. After analysis, is not very reasonable, instead in service layer interception, spring AOP to achieve, the code was abandoned. But tests are available and notes are recorded.


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;

/**
 *  Interceptor role: to each entity object in the addition, modification, automatically add operation attribute information. 
 */
@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("----------- Parameters to intercept ---------------------------------------------------");
    System.out.println("02  The current thread ID:"+Thread.currentThread().getId());
    // Iterate through all the parameters, update The method takes two arguments, see Executor In the class update() Methods. 
    for(int i=0;i<args.length;i++)
    {
      Object arg=args[i];
      String className=arg.getClass().getName();
      System.out.println(i + "  Parameter type: "+className);
      
      // The first 1 Parameter processing. It determines whether or not to assign an action attribute. 
      if(arg instanceof MappedStatement)
      {// If it is the first 1 A parameter  MappedStatement
        MappedStatement ms = (MappedStatement)arg;
        SqlCommandType sqlCommandType = ms.getSqlCommandType();
        System.out.println(" Operation type: "+sqlCommandType);
        if(sqlCommandType == SqlCommandType.INSERT || sqlCommandType==SqlCommandType.UPDATE)
        {// If it is an Add or Update operation, the default operation information assignment continues. Otherwise, exit 
          continue;
        }
        else
        {
          break;
        }
      }
      
      // The first 2 Parameter processing. (only the first 2 To get here.) 
      if (arg instanceof Map) 
      {// If it is map There are two cases :( 1 ) to use @Param Multiple parameters passed in by Mybatis Packaged in map . ( 2 ) Original incoming Map
        System.out.println(" This is a 1 Ten packaged types !");
        Map map=(Map)arg;
        for (Object obj : map.values()) 
        { 
          setProperty(obj);
        } 
      }
      else
      {// Original parameter passed in 
        setProperty(arg);
      }
      
    }

    return invocation.proceed();

  }
  
  /**
   *  Assigns an action attribute to an object 
   * @param obj
   */
  private void setProperty(Object obj)
  {
    try
    {
      //TODO:  Assign relevant properties to default values as needed 
      BeanUtils.setProperty(obj, "createrUsername", " zhang 3");
      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)
  {

  }

}

Related articles: