MybatisPlus|MybatisPlus 插入或更新数据时自动填充更新数据解决方案

目录

  • 解决方案
    • 1、 实体类
    • 2、拦截器MetaObjectHandler
    • 3、测试
  • 参考文章
    Maven

    org.springframework.bootspring-boot-starter-parent2.2.6.RELEASE com.baomidoumybatis-plus-boot-starter3.1.0


    解决方案

    1、 实体类
    /** * 基础Bean */@Datapublic class BaseEntity implements Serializable { @TableField(value = "https://www.it610.com/article/create_user", fill = FieldFill.INSERT) // 新增执行private String createUser; @TableField(value = "https://www.it610.com/article/create_time", fill = FieldFill.INSERT)private LocalDateTime createTime; @TableField(value = "https://www.it610.com/article/update_user", fill = FieldFill.INSERT_UPDATE) // 新增和更新执行private String updateUser; @TableField(value = "https://www.it610.com/article/update_time", fill = FieldFill.INSERT_UPDATE)private LocalDateTime updateTime; @TableField(value = "https://www.it610.com/article/remark")private String remark; }

    @Data@TableName("sys_dept")public class SysDeptEntity extends BaseEntity { private static final long serialVersionUID = 1L; /*** 部门ID**/@TableIdprivate Long deptId; /*** 部门父节点ID**/private Long parentId; /*** 部门名称**/private String deptName; /*** 显示顺序**/private Integer orderNum; /*** 用户状态(0:正常 1:禁用)**/private Integer status; @TableField(exist = false)private List children; }


    2、拦截器MetaObjectHandler
    /** * @author ShenTuZhiGang * @version 1.0.0 * @date 2020-11-26 15:52 */@Slf4j@Componentpublic class CustomMetaObjectHandler implements MetaObjectHandler {@Autowiredprivate AuthenticationTrustResolver authenticationTrustResolver; @Overridepublic 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); } } @Overridepublic 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); }}}

    不需要以下代码:
    @Configurationpublic class MyBatisPlusConfig { /*** 自动填充功能* @return*/@Beanpublic GlobalConfig globalConfig() {GlobalConfig globalConfig = new GlobalConfig(); globalConfig.setMetaObjectHandler(new MetaHandler()); return globalConfig; } }


    3、测试
    @RequiresPermissions("sys:dept:add")@PostMapping("/add")@ResponseBodypublic R add(@RequestBody SysDeptEntity deptEntity) {logger.info("添加信息={}", deptEntity); sysDeptService.save(deptEntity); // 不再需要设置setCreateUser、setCreateTime、setUpdateUser、setUpdateTime操作,代码更优美return R.ok(); }


    参考文章
    使用MyBatis Plus自动添加数据库表中的创建时间、创建者、更新时间、更新者
    mybatisPlus自动填充更新时间
    mybatis-plus实战-时间字段自动更新
    【MybatisPlus|MybatisPlus 插入或更新数据时自动填充更新数据解决方案】使用mybatis plus插入或更新数据时mysql表的添加时间和更新时间字段没有更新(mybatis plus坑之一)

    到此这篇关于MybatisPlus 插入或更新数据时自动填充更新数据解决方案的文章就介绍到这了,更多相关MybatisPlus 插入或更新自动填充内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

      推荐阅读