In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.