mybatis自动填充时间字段 - Go语言中文社区

mybatis自动填充时间字段


对于实体中的created_onupdated_on来说,它没有必要被开发人员去干预,因为它已经足够说明使用场景了,即在插入数据和更新数据时,记录当前时间,这对于mybatis来说,通过拦截器是可以实现的,记得之前说过在jpa中实现的方法,主要通过jpa的注解实现的,因为今天的mybatis需要用到java的拦截器。

定义两个注解

@Retention(RetentionPolicy.RUNTIME)
@Target( {ElementType.FIELD})
public @interface CreatedOnFuncation {

  String value() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@Target( {ElementType.FIELD})
public @interface UpdatedOnFuncation {

  String value() default "";
}

使用这两个注解

@Getter
@Builder(toBuilder = true)
@ToString
public class UserInfo {
  private Long id;
  private String name;
  private String email;

  @CreatedOnFuncation
  private LocalDateTime createdOn;
  @UpdatedOnFuncation
  private LocalDateTime updatedOn;
}

定义拦截器,重写赋值的语句

/**
 * 时间拦截器.
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Accessors(chain = true)
@Intercepts({@Signature(
    type = org.apache.ibatis.executor.Executor.class,
    method = "update",
    args = {MappedStatement.class, Object.class})})
public class CreateUpdateTimeInterceptor extends AbstractSqlParserHandler implements Interceptor {

  private Properties properties;

  @Override
  public Object intercept(Invocation invocation) throws Throwable {
    MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];

    // 获取 SQL 命令
    SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();

    // 获取参数
    Object parameter = invocation.getArgs()[1];

    // 获取私有成员变量
    Field[] declaredFields = parameter.getClass().getDeclaredFields();
    if (parameter.getClass().getSuperclass() != null) {
      Field[] superField = parameter.getClass().getSuperclass().getDeclaredFields();
      declaredFields = ArrayUtils.addAll(declaredFields, superField);
    }
    // 是否为mybatis plug
    boolean isPlugUpdate = parameter.getClass().getDeclaredFields().length == 1
        && parameter.getClass().getDeclaredFields()[0].getName().equals("serialVersionUID");

    //兼容mybatis plus的update
    if (isPlugUpdate) {
      Map<String, Object> updateParam = (Map<String, Object>) parameter;
      Class<?> updateParamType = updateParam.get("param1").getClass();
      declaredFields = updateParamType.getDeclaredFields();
      if (updateParamType.getSuperclass() != null) {
        Field[] superField = updateParamType.getSuperclass().getDeclaredFields();
        declaredFields = ArrayUtils.addAll(declaredFields, superField);
      }
    }
    for (Field field : declaredFields) {

      // insert
      if (field.getAnnotation(CreatedOnFuncation.class) != null) {
        if (SqlCommandType.INSERT.equals(sqlCommandType)) {
          field.setAccessible(true);
          field.set(parameter, new Timestamp(System.currentTimeMillis()));
        }
      }

      // update
      if (field.getAnnotation(UpdatedOnFuncation.class) != null) {
        if (SqlCommandType.INSERT.equals(sqlCommandType)
            || SqlCommandType.UPDATE.equals(sqlCommandType)) {
          field.setAccessible(true);

          //兼容mybatis plus的update
          if (isPlugUpdate) {
            Map<String, Object> updateParam = (Map<String, Object>) parameter;
            field.set(updateParam.get("param1"), new Timestamp(System.currentTimeMillis()));
          } else {
            field.set(parameter, new Timestamp(System.currentTimeMillis()));
          }
        }
      }
    }

    return invocation.proceed();
  }

  @Override
  public Object plugin(Object target) {
    if (target instanceof org.apache.ibatis.executor.Executor) {
      return Plugin.wrap(target, this);
    }
    return target;
  }

  @Override
  public void setProperties(Properties prop) {
    this.properties = prop;
  }
}

添加测试用例

  @Test
  public void insert() {
    UserInfo userInfo = UserInfo.builder()
        .name("lind")
        .email("test@sina.com")
        .build();
    userInfoMapper.insert(userInfo);
    System.out.println("userinfo:" + userInfo.toString());
  }

解决是我们所预想的,created_on和updated_on被自动赋上值了。

userinfo:UserInfo
(
id=1085780948955959297, 
name=lind, 
email=test@sina.com, 
createdOn=2019-01-17T14:08:45.665,
updatedOn=2019-01-17T14:08:45.665
)
版权声明:本文来源博客园,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://www.cnblogs.com/lori/p/10281976.html
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2019-11-16 23:11:13
  • 阅读 ( 1095 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

推荐文章

猜你喜欢