欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MybatisPlus 插入或更新數(shù)據(jù)時(shí)自動(dòng)填充更新數(shù)據(jù)解決方案

 更新時(shí)間:2021年09月24日 16:17:58   作者:Starzkg  
本文主要介紹了MybatisPlus 插入或更新數(shù)據(jù)時(shí)自動(dòng)填充更新數(shù)據(jù)解決方案,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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、 實(shí)體類

/**
 * 基礎(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é)點(diǎn)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 {
 
    /**
     * 自動(dòng)填充功能
     * @return
     */
    @Bean
    public GlobalConfig globalConfig() {
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setMetaObjectHandler(new MetaHandler());
        return globalConfig;
    }
 
}

 3、測(cè)試

@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自動(dòng)添加數(shù)據(jù)庫(kù)表中的創(chuàng)建時(shí)間、創(chuàng)建者、更新時(shí)間、更新者

mybatisPlus自動(dòng)填充更新時(shí)間

mybatis-plus實(shí)戰(zhàn)-時(shí)間字段自動(dòng)更新

使用mybatis plus插入或更新數(shù)據(jù)時(shí)mysql表的添加時(shí)間和更新時(shí)間字段沒有更新(mybatis plus坑之一)

到此這篇關(guān)于MybatisPlus 插入或更新數(shù)據(jù)時(shí)自動(dòng)填充更新數(shù)據(jù)解決方案的文章就介紹到這了,更多相關(guān)MybatisPlus 插入或更新自動(dòng)填充內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot獲取配置文件的簡(jiǎn)單實(shí)現(xiàn)方法

    SpringBoot獲取配置文件的簡(jiǎn)單實(shí)現(xiàn)方法

    這篇文章主要給大家介紹了關(guān)于SpringBoot如何獲取配置文件的簡(jiǎn)單實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • mybatis接口綁定失效的解決

    mybatis接口綁定失效的解決

    這篇文章主要介紹了mybatis接口綁定失效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • checkpoint 機(jī)制具體實(shí)現(xiàn)示例詳解

    checkpoint 機(jī)制具體實(shí)現(xiàn)示例詳解

    這篇文章主要為大家介紹了checkpoint 機(jī)制具體實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • java報(bào)錯(cuò):“錯(cuò)誤:編碼GBK?的不可映射字符”解決辦法

    java報(bào)錯(cuò):“錯(cuò)誤:編碼GBK?的不可映射字符”解決辦法

    當(dāng)Java源代碼中包含中文字符時(shí),我們?cè)谟胘avac編譯時(shí)會(huì)出現(xiàn)“錯(cuò)誤:編碼GBK的不可映射字符”,這篇文章主要給大家介紹了關(guān)于java報(bào)錯(cuò):“錯(cuò)誤:編碼GBK?的不可映射字符”的解決辦法,需要的朋友可以參考下
    2024-08-08
  • 詳解JAVA設(shè)計(jì)模式之適配器模式

    詳解JAVA設(shè)計(jì)模式之適配器模式

    這篇文章主要介紹了JAVA設(shè)計(jì)模式之適配器模式的的相關(guān)資料,文中示例代碼非常詳細(xì),供大家參考和學(xué)習(xí),感興趣的朋友可以了解
    2020-06-06
  • 詳解Java中的實(shí)例初始化塊(IIB)

    詳解Java中的實(shí)例初始化塊(IIB)

    在本篇文章里我們針對(duì)Java中的實(shí)例初始化塊(IIB)做想詳細(xì)分析,有需要的朋友們可以跟著學(xué)習(xí)參考下。
    2018-10-10
  • 深入淺析Java中Static Class及靜態(tài)內(nèi)部類和非靜態(tài)內(nèi)部類的不同

    深入淺析Java中Static Class及靜態(tài)內(nèi)部類和非靜態(tài)內(nèi)部類的不同

    上次有朋友問我,java中的類可以是static嗎?我給他肯定的回答是可以的,在java中我們可以有靜態(tài)實(shí)例變量、靜態(tài)方法、靜態(tài)塊。當(dāng)然類也可以是靜態(tài)的,下面小編整理了些關(guān)于java中的static class相關(guān)資料分享在腳本之家平臺(tái)供大家參考
    2015-11-11
  • Java?中的?clone(?)?和?new哪個(gè)效率更高

    Java?中的?clone(?)?和?new哪個(gè)效率更高

    很多朋友不太清楚clone()和new那個(gè)更快?針對(duì)這個(gè)問題我百度了好多資料,最終小編總結(jié)下關(guān)于Java?中的?clone(?)?和?new哪個(gè)效率更高的問題,感興趣的朋友跟隨小編一起看看吧
    2021-12-12
  • @RequestBody注解Ajax post json List集合數(shù)據(jù)請(qǐng)求400/415的處理

    @RequestBody注解Ajax post json List集合數(shù)據(jù)請(qǐng)求400/41

    這篇文章主要介紹了@RequestBody注解Ajax post json List集合數(shù)據(jù)請(qǐng)求400/415的處理方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • springboot3.2整合mybatis-plus詳細(xì)代碼示例

    springboot3.2整合mybatis-plus詳細(xì)代碼示例

    這篇文章主要給大家介紹了關(guān)于springboot3.2整合mybatis-plus的相關(guān)資料,Spring Boot是一個(gè)非常流行的Java Web框架,可以快速地搭建Web應(yīng)用程序,需要的朋友可以參考下
    2023-12-12

最新評(píng)論