|  |  | 
 |  |  | package com.dy.common.mybatis; | 
 |  |  |  | 
 |  |  |  | 
 |  |  | import com.alibaba.fastjson2.JSONObject; | 
 |  |  | import com.alibaba.fastjson2.JSONWriter; | 
 |  |  | import com.dy.common.po.BaseEntity; | 
 |  |  | import com.dy.common.util.IDLongGenerator; | 
 |  |  | 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; | 
 |  |  | import org.apache.ibatis.plugin.*; | 
 |  |  |  | 
 |  |  | import java.lang.reflect.Method; | 
 |  |  |  | 
 |  |  | 
 |  |  |  | 
 |  |  |     static int MAPPED_STATEMENT_INDEX = 0; | 
 |  |  |     static int PARAMETER_INDEX = 1; | 
 |  |  |     static String BASE_FIELD_SET_PRIMARY_KEY_FUNTION_NAME = "setId"; | 
 |  |  |     static String BASE_FIELD_SET_PRIMARY_KEY_FUNTION_SETID = "setId"; | 
 |  |  |     static String BASE_FIELD_SET_PRIMARY_KEY_FUNTION_SETUSERID = "setUserId"; | 
 |  |  |     static String BASE_FIELD_SET_PRIMARY_KEY_FUNTION_SETROLEID = "setRoleId"; | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 拦截逻辑实现 | 
 |  |  | 
 |  |  |             if (entity instanceof BaseEntity) { | 
 |  |  |                 //Class<? extends Object> entityClass = entity.getClass(); | 
 |  |  |                 Class<?> entityClass = entity.getClass(); | 
 |  |  |                 Method setMt = null ; | 
 |  |  |                 Method setId = null ; | 
 |  |  |                 Method setUserId = null ; | 
 |  |  |                 Method setRoleId = null ; | 
 |  |  |                 try{ | 
 |  |  |                     /* | 
 |  |  |                     // 添加用户时userId为自动生成主键。roleId存在时是为用户绑角色 | 
 |  |  |                     String jsonString = JSONObject.toJSONString(entity, JSONWriter.Feature.WriteMapNullValue); | 
 |  |  |                     JSONObject jsonObject = JSONObject.parseObject(jsonString); | 
 |  |  |                     if(jsonObject.containsKey("userId") && !jsonObject.containsKey("roleId")) { | 
 |  |  |                         setUserId = entityClass.getMethod(BASE_FIELD_SET_PRIMARY_KEY_FUNTION_SETUSERID, Long.class) ; | 
 |  |  |                         setRoleId = entityClass.getMethod(BASE_FIELD_SET_PRIMARY_KEY_FUNTION_SETROLEID, Long.class) ; | 
 |  |  |                     }else { | 
 |  |  |                         //有一些实体没有id,例如中间表 | 
 |  |  |                         setId = entityClass.getMethod(BASE_FIELD_SET_PRIMARY_KEY_FUNTION_SETID, Long.class) ; | 
 |  |  |                     } | 
 |  |  |                     */ | 
 |  |  |                     //有一些实体没有id,例如中间表 | 
 |  |  |                     setMt = entityClass.getMethod(BASE_FIELD_SET_PRIMARY_KEY_FUNTION_NAME, Long.class) ; | 
 |  |  |                     setId = entityClass.getMethod(BASE_FIELD_SET_PRIMARY_KEY_FUNTION_SETID, Long.class) ; | 
 |  |  |                     // BaUser实体中是setUserId, BaUserRole实体中包含setUserId,和setRoleId | 
 |  |  |                     setUserId = entityClass.getMethod(BASE_FIELD_SET_PRIMARY_KEY_FUNTION_SETUSERID, Long.class) ; | 
 |  |  |                     setRoleId = entityClass.getMethod(BASE_FIELD_SET_PRIMARY_KEY_FUNTION_SETROLEID, Long.class) ; | 
 |  |  |                 }catch (Exception e){ | 
 |  |  |                     //当entityClass没有setId方法时,会抛出异常 | 
 |  |  |                 } | 
 |  |  |                 if(setMt != null){ | 
 |  |  |                     setMt.invoke(entity, new IDLongGenerator().generate()); | 
 |  |  |                 if(setUserId != null && setRoleId == null){ | 
 |  |  |                     setUserId.invoke(entity, new IDLongGenerator().generate()); | 
 |  |  |                 }else if(setId != null){ | 
 |  |  |                     setId.invoke(entity, new IDLongGenerator().generate()); | 
 |  |  |                 } | 
 |  |  |  | 
 |  |  |                 invocation.getArgs()[PARAMETER_INDEX] = entity; | 
 |  |  |             } | 
 |  |  |         } |