服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - MyBatis拦截器:给参数对象属性赋值的实例

MyBatis拦截器:给参数对象属性赋值的实例

2020-09-10 14:15Java之家 Java教程

下面小编就为大家带来一篇MyBatis拦截器:给参数对象属性赋值的实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

拦截器的作用:在进行增加、修改等操作时,给数据模型的一些通用操作属性(如:创建人、创建时间、修改人、修改时间等)自动赋值

该实现是在DAO层拦截,即存入DB前最后一层。后经分析,不是很合理,改为在service层拦截,用spring AOP来实现了,该代码遂弃用。不过已经测试可用,记录备忘。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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;
 
/**
 * 拦截器作用:给各实体对象在增加、修改时,自动添加操作属性信息。
 */
@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("-----------参数拦截---------------------------------------------------");
    System.out.println("02 当前线程ID:"+Thread.currentThread().getId());
    //遍历处理所有参数,update方法有两个参数,参见Executor类中的update()方法。
    for(int i=0;i<args.length;i++)
    {
      Object arg=args[i];
      String className=arg.getClass().getName();
      System.out.println(i + " 参数类型:"+className);
      
      //第一个参数处理。根据它判断是否给“操作属性”赋值。
      if(arg instanceof MappedStatement)
      {//如果是第一个参数 MappedStatement
        MappedStatement ms = (MappedStatement)arg;
        SqlCommandType sqlCommandType = ms.getSqlCommandType();
        System.out.println("操作类型:"+sqlCommandType);
        if(sqlCommandType == SqlCommandType.INSERT || sqlCommandType==SqlCommandType.UPDATE)
        {//如果是“增加”或“更新”操作,则继续进行默认操作信息赋值。否则,则退出
          continue;
        }
        else
        {
          break;
        }
      }
      
      //第二个参数处理。(只有第二个程序才能跑到这)
      if (arg instanceof Map)
      {//如果是map,有两种情况:(1)使用@Param多参数传入,由Mybatis包装成map。(2)原始传入Map
        System.out.println("这是一个包装过的类型!");
        Map map=(Map)arg;
        for (Object obj : map.values())
        {
          setProperty(obj);
        }
      }
      else
      {//原始参数传入
        setProperty(arg);
      }
      
    }
 
    return invocation.proceed();
 
  }
  
  /**
   * 为对象的操作属性赋值
   * @param obj
   */
  private void setProperty(Object obj)
  {
    try
    {
      //TODO: 根据需要,将相关属性赋上默认值
      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)
  {
 
  }
 
}

以上这篇MyBatis拦截器:给参数对象属性赋值的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

延伸 · 阅读

精彩推荐