mybatis配置自动填充时间拦截器

1. Annotation:

  ① FillOnInsert

  import java.lang.annotation.ElementType;
  import java.lang.annotation.Retention;
  import java.lang.annotation.RetentionPolicy;
  import java.lang.annotation.Target;

  @Retention(RetentionPolicy.RUNTIME)
  @Target({ElementType.FIELD})
  public @interface FillOnInsert {
  Class<?> value();
  }

  ② FillOnUpdate

  import java.lang.annotation.ElementType;
  import java.lang.annotation.Retention;
  import java.lang.annotation.RetentionPolicy;
  import java.lang.annotation.Target;

  @Retention(RetentionPolicy.RUNTIME)
  @Target({ElementType.FIELD})
  public @interface FillOnUpdate {
  Class<?> value();
  }

2. handler(class):

  import com.embracesource.common.core.utils.StringUtils;
  import com.jichu.shebei.annotation.FillOnInsert;
  import com.jichu.shebei.annotation.FillOnUpdate;
  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.Signature;
  import org.springframework.stereotype.Component;

  import java.lang.reflect.Field;
  import java.util.Date;
  import java.util.Properties;

  /**
   * @description: 填充时间字段处理器
   **/
  @Component
  @Intercepts({@Signature(type = Executor.class,
method = "update",
args = {MappedStatement.class, Object.class})})
  public class FillTimeFieldHandler implements Interceptor {

@Override
public Object intercept(Invocation invocation) throws Throwable {
MappedStatement statement = (MappedStatement) invocation.getArgs()[0];
SqlCommandType sqlCommandType = statement.getSqlCommandType();
Object param = invocation.getArgs()[1];
Class<?> aClass = param.getClass();
Class<?> superclass = aClass.getSuperclass();
// 判断是否是BaseEntity,并将填充其时间字段
if (StringUtils.equals(superclass.getName(),"com.embracesource.common.core.web.domain.BaseEntity")) {
if (SqlCommandType.INSERT.equals(sqlCommandType)) {
superclass.getMethod("setCreateTime", Date.class).invoke(param, new Date());
superclass.getMethod("setUpdateTime", Date.class).invoke(param, new Date());
}
if (SqlCommandType.UPDATE.equals(sqlCommandType)) {
superclass.getMethod("setUpdateTime", Date.class).invoke(param, new Date());
}
}
// 根据注解填充
for (Field field : aClass.getDeclaredFields()) {
if (field.isAnnotationPresent(FillOnInsert.class) && SqlCommandType.INSERT.equals(sqlCommandType)) {
field.setAccessible(true);
Class<?> fillValueClass = field.getAnnotationsByType(FillOnInsert.class)[0].value();
field.set(param, fillValueClass.getDeclaredConstructor().newInstance());
}
if (field.isAnnotationPresent(FillOnUpdate.class) && SqlCommandType.UPDATE.equals(sqlCommandType)) {
field.setAccessible(true);
Class<?> fillValueClass = field.getAnnotationsByType(FillOnUpdate.class)[0].value();
field.set(param, fillValueClass.getDeclaredConstructor().newInstance());
}
}
return invocation.proceed();
}

@Override
public Object plugin(Object target) {
return Interceptor.super.plugin(target);
}

@Override
public void setProperties(Properties properties) {
Interceptor.super.setProperties(properties);
}
}

 

posted @ 2024-08-02 11:07  sensen~||^_^|||&  阅读(2)  评论(0编辑  收藏  举报