mybatisPlus自動填充更新時間
阿新 • • 發佈:2020-08-29
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",newDate(),metaObject); } }
3.進行新增和修改測試
//新增使用者 @Test public void addUser(){ User user = new User(); user.setAge(23); user.setEmail("[email protected]"); 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("[email protected]"); user.setName("gwq"); user.setId(1299641789988229125L); int i = userDao.updateById(user); System.out.println(i); }