1.实体类上的注解
@NoArgsConstructor
@AllArgsConstructor
@Data
public class User {
@TableId(type = IdType.AUTO)
private Long id;
private String name;
private Integer age;
private String email;
@TableField(fill = FieldFill.INSERT)
private Date createTime;
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
}
2.对注解的处理
@Component
@Slf4j
public class MyDetaObjectHander implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
log.info("come to insert fill .........");
//setFieldValByName(String fieldName, Object fieldVal, MetaObject metaObject)
this.setFieldValByName("createTime",new Date(),metaObject);
this.setFieldValByName("updateTime",new Date(),metaObject);
}
@Override
public void updateFill(MetaObject metaObject) {
log.info("come to update fill .......");
this.setFieldValByName("updateTime",new Date(),metaObject);
}
}
3.进行添加和修改测试
//添加用户
@Test
public void addUser(){
User user = new User();
user.setAge(23);
user.setEmail("168@qq.com");
user.setName("gwq");
int insert = userDao.insert(user);
System.out.println(insert);
}
@Test
public void update(){
User user = new User();
user.setAge(23);
user.setEmail("168@qq.com");
user.setName("gwq");
user.setId(1299641789988229125L);
int i = userDao.updateById(user);
System.out.println(i);
}
到此这篇关于mybatisPlus自动填充更新时间的文章就介绍到这了,更多相关mybatisPlus自动填充更新时间内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!