最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2021年09月24日 16:17:58   作者:Starzkg  
本文主要介紹了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、 實體類

/**
 * 基礎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); // 不再需要設置setCreateUser、setCreateTime、setUpdateUser、setUpdateTime操作,代碼更優(yōu)美
    return R.ok();
}

參考文章

使用MyBatis Plus自動添加數(shù)據(jù)庫表中的創(chuàng)建時間、創(chuàng)建者、更新時間、更新者

mybatisPlus自動填充更新時間

mybatis-plus實戰(zhàn)-時間字段自動更新

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

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

相關文章

最新評論

石台县| 长海县| 安龙县| 海晏县| 商城县| 伊吾县| 义乌市| 西平县| 基隆市| 巴彦淖尔市| 舟山市| 确山县| 南通市| 木兰县| 山西省| 周至县| 乌鲁木齐市| 南通市| 苏州市| 临江市| 渭源县| 绵阳市| 德化县| 礼泉县| 安溪县| 吉林省| 观塘区| 谷城县| 乌兰浩特市| 台安县| 伊宁市| 黄山市| 赫章县| 青田县| 兴海县| 寿光市| 乌兰县| 友谊县| 潼南县| 繁峙县| 三穗县|