How MybatisPlus can automatically populate and update data when inserting or updating data
This article mainly introduces MybatisPlus how to insert or update data automatically fill update data, the article is very detailed, has a certain reference value, interested friends must read it!
Maven
Org.springframework.boot spring-boot-starter-parent 2.2.6.RELEASE com.baomidou mybatis-plus-boot-starter 3.1.0 solution
1. Entity class / * * basic Bean * / @ Datapublic class BaseEntity implements Serializable {@ TableField (value = "create_user", fill = FieldFill.INSERT) / / add execution private String createUser; @ TableField (value = "create_time", fill = FieldFill.INSERT) private LocalDateTime createTime; @ TableField (value = "update_user", fill = FieldFill.INSERT_UPDATE) / / add and update execution private String updateUser @ TableField (value = "update_time", fill = FieldFill.INSERT_UPDATE) private LocalDateTime updateTime; @ TableField (value = "remark") private String remark;} @ Data@TableName ("sys_dept") public class SysDeptEntity extends BaseEntity {private static final long serialVersionUID = 1L; / * * departmental ID * / @ TableId private Long deptId; / * * departmental parent ID * / private Long parentId / * Department name * * / private String deptName; / * * display order * * / private Integer orderNum; / * user status (0: normal 1: disabled) * * / private Integer status; @ TableField (exist = false) private List children 2. Interceptor MetaObjectHandler/** * @ author ShenTuZhiGang * @ version 1.0.0 * @ date 2020-11-26 15:52 * / @ Slf4j@Componentpublic class CustomMetaObjectHandler implements MetaObjectHandler {@ Autowired private AuthenticationTrustResolver authenticationTrustResolver; @ Override public void insertFill (MetaObject metaObject) {log.info ("come to insert fill."); this.setFieldValByName ("createTime", LocalDateTime.now (), metaObject) This.setFieldValByName ("updateTime", LocalDateTime.now (), metaObject); Authentication authentication = SecurityContextHolder.getContext (). GetAuthentication (); if (! authenticationTrustResolver.isAnonymous (authentication) & & authenticationauthentication null) {AuthenticationUser user = (AuthenticationUser) authentication.getPrincipal (); this.setFieldValByName ("createUser", user.getUsername (), metaObject); this.setFieldValByName ("updateUser", user.getUsername (), metaObject) } else {this.setFieldValByName ("createUser", "unknown", metaObject); this.setFieldValByName ("updateUser", "unknown", metaObject);} @ Override public void updateFill (MetaObject metaObject) {log.info ("come to update fill."); this.setFieldValByName ("update_time", LocalDateTime.now (), metaObject) Authentication authentication = SecurityContextHolder.getContext (). GetAuthentication (); if (! authenticationTrustResolver.isAnonymous (authentication) & & authenticationauthentication null) {AuthenticationUser user = (AuthenticationUser) authentication.getPrincipal (); this.setFieldValByName ("updateUser", user.getUsername (), metaObject);} else {this.setFieldValByName ("updateUser", "unknown", metaObject);}
The following code is not required:
@ Configurationpublic class MyBatisPlusConfig {/ * Auto fill function * @ return * / @ Bean public GlobalConfig globalConfig () {GlobalConfig globalConfig = new GlobalConfig (); globalConfig.setMetaObjectHandler (new MetaHandler ()); return globalConfig }} 3. Test @ RequiresPermissions ("sys:dept:add") @ PostMapping ("/ add") @ ResponseBodypublic R add (@ RequestBody SysDeptEntity deptEntity) {logger.info ("add information = {}", deptEntity); sysDeptService.save (deptEntity); / / No longer need to set setCreateUser, setCreateTime, setUpdateUser, setUpdateTime operations, the code is more beautiful return R.ok () } these are all the contents of the article "how to automatically fill in and update data when MybatisPlus inserts or updates data". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!