您好,登錄后才能下訂單哦!
這篇文章主要介紹MybatisPlus如何實現(xiàn)插入或更新數(shù)據(jù)時自動填充更新數(shù)據(jù),文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
Maven
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.1.0</version> </dependency>
/** * 基礎Bean */ @Data public class BaseEntity implements Serializable { @TableField(value = "create_user", fill = FieldFill.INSERT) // 新增執(zhí)行 private String createUser; @TableField(value = "create_time", fill = FieldFill.INSERT) private LocalDateTime createTime; @TableField(value = "update_user", fill = FieldFill.INSERT_UPDATE) // 新增和更新執(zhí)行 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; /** * 部門ID **/ @TableId private Long deptId; /** * 部門父節(jié)點ID **/ private Long parentId; /** * 部門名稱 **/ private String deptName; /** * 顯示順序 **/ private Integer orderNum; /** * 用戶狀態(tài)(0:正常 1:禁用) **/ private Integer status; @TableField(exist = false) private List<SysDeptEntity> children; }
/** * @author ShenTuZhiGang * @version 1.0.0 * @date 2020-11-26 15:52 */ @Slf4j @Component public 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) && authentication!=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) && authentication!=null){ AuthenticationUser user = (AuthenticationUser) authentication.getPrincipal(); this.setFieldValByName("updateUser", user.getUsername(), metaObject); }else{ this.setFieldValByName("updateUser", "unknown", metaObject); } } }
不需要以下代碼:
@Configuration public class MyBatisPlusConfig { /** * 自動填充功能 * @return */ @Bean public GlobalConfig globalConfig() { GlobalConfig globalConfig = new GlobalConfig(); globalConfig.setMetaObjectHandler(new MetaHandler()); return globalConfig; } }
@RequiresPermissions("sys:dept:add") @PostMapping("/add") @ResponseBody public R add(@RequestBody SysDeptEntity deptEntity) { logger.info("添加信息={}", deptEntity); sysDeptService.save(deptEntity); // 不再需要設置setCreateUser、setCreateTime、setUpdateUser、setUpdateTime操作,代碼更優(yōu)美 return R.ok(); }
以上是“MybatisPlus如何實現(xiàn)插入或更新數(shù)據(jù)時自動填充更新數(shù)據(jù)”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關知識,歡迎關注億速云行業(yè)資訊頻道!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。