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

发布时间:2020-07-14 02:44:48 作者:邹君安
来源:网络 阅读:1067

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

  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 }

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


推荐阅读:
  1. 用v-for给src属性赋值的方法
  2. 解决vue A对象赋值给B对象,修改B属性会影响到A的问题

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

拦截器 package import

上一篇:AGG第三十四课 stroke_aa和outline_aa渲染线段效率对比

下一篇:6种微服务RPC框架,你知道几个?

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》