1.实体类上的注解
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
@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.对注解的处理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
@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.进行添加和修改测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
//添加用户 @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自动填充更新时间内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://www.cnblogs.com/ghwq/p/13583110.html