MybatisPlus 插入或更新數(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>
解決方案
1、 實體類
/**
* 基礎(chǔ)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;
}
2、攔截器MetaObjectHandler
/**
* @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;
}
}
3、測試
@RequiresPermissions("sys:dept:add")
@PostMapping("/add")
@ResponseBody
public R add(@RequestBody SysDeptEntity deptEntity) {
logger.info("添加信息={}", deptEntity);
sysDeptService.save(deptEntity); // 不再需要設(shè)置setCreateUser、setCreateTime、setUpdateUser、setUpdateTime操作,代碼更優(yōu)美
return R.ok();
}
參考文章
使用MyBatis Plus自動添加數(shù)據(jù)庫表中的創(chuàng)建時間、創(chuàng)建者、更新時間、更新者
使用mybatis plus插入或更新數(shù)據(jù)時mysql表的添加時間和更新時間字段沒有更新(mybatis plus坑之一)
到此這篇關(guān)于MybatisPlus 插入或更新數(shù)據(jù)時自動填充更新數(shù)據(jù)解決方案的文章就介紹到這了,更多相關(guān)MybatisPlus 插入或更新自動填充內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- mybatisplus?實現(xiàn)接口MetaObjectHandler自動填充字段值
- mybatisplus實現(xiàn)自動填充時間的項目實踐
- MybatisPlus自動填充創(chuàng)建(更新)時間問題
- MybatisPlus字段自動填充失效,填充值為null的解決方案
- mybatisplus自動填充屬性值的實現(xiàn)步驟
- mybatisPlus自動填充更新時間的示例代碼
- 在MyBatisPlus中使用@TableField完成字段自動填充的操作
- Mybatisplus自動填充實現(xiàn)方式及代碼示例
- MybatisPlus 自動填充的實現(xiàn)
- MyBatisPlus實現(xiàn)自動填充字段的實踐
相關(guān)文章
spring mvc 實現(xiàn)獲取后端傳遞的值操作示例
這篇文章主要介紹了spring mvc 實現(xiàn)獲取后端傳遞的值操作,結(jié)合實例形式詳細分析了spring mvc使用JSTL 方法獲取后端傳遞的值相關(guān)操作技巧2019-11-11
基于SpringMVC接受JSON參數(shù)詳解及常見錯誤總結(jié)
下面小編就為大家分享一篇基于SpringMVC接受JSON參數(shù)詳解及常見錯誤總結(jié),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03
SpringBoot集成minio實現(xiàn)文件上傳和刪除功能
這篇文章主要介紹了SpringBoot集成minio實現(xiàn)文件上傳和刪除功能,詳細介紹每個功能的實現(xiàn)步驟和代碼示例,具有一定的參考價值,感興趣的可以了解一下2023-11-11
解決springboot MultipartFile文件上傳遇到的問題
本文給大家?guī)砹私鉀Qspringboot MultipartFile文件上傳遇到的問題,解決方法超簡單,感興趣的朋友參考下本文2018-08-08
Java實現(xiàn)經(jīng)典游戲俄羅斯方塊(升級版)的示例代碼
俄羅斯方塊是一款風(fēng)靡全球,從一開始到現(xiàn)在都一直經(jīng)久不衰的電腦、手機、掌上游戲機產(chǎn)品,是一款游戲規(guī)則簡單,但又不缺乏樂趣的簡單經(jīng)典小游戲。本文將用Java語言實現(xiàn)這一經(jīng)典游戲,需要的可以參考一下2022-09-09
詳解SpringBoot和Mybatis配置多數(shù)據(jù)源
本篇文章主要介紹了詳解SpringBoot和Mybatis配置多數(shù)據(jù)源,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05
Java基于servlet監(jiān)聽器實現(xiàn)在線人數(shù)監(jiān)控功能的方法
這篇文章主要介紹了Java基于servlet監(jiān)聽器實現(xiàn)在線人數(shù)監(jiān)控功能的方法,結(jié)合實例形式分析了ServletContextListener監(jiān)聽功能的相關(guān)使用步驟與操作技巧,需要的朋友可以參考下2018-01-01

