阅读 247

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

本文主要介绍了MybatisPlus 插入或更新数据时自动填充更新数据解决方案,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

目录
  • 解决方案

    • 1、 实体类

    •  2、拦截器MetaObjectHandler

    •  3、测试

  • 参考文章

    Maven

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <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、 实体类

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    /**
     * 基础Bean
     */
    @Data
    public class BaseEntity implements Serializable {
      
        @TableField(value = "create_user", fill = FieldFill.INSERT) // 新增执行
        private String createUser;
      
        @TableField(value = "create_time", fill = FieldFill.INSERT)
        private LocalDateTime createTime;
      
        @TableField(value = "update_user", fill = FieldFill.INSERT_UPDATE) // 新增和更新执行
        private String updateUser;
      
        @TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
        private LocalDateTime updateTime;
      
        @TableField(value = "remark")
        private String remark;
      
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    @Data
    @TableName("sys_dept")
    public class SysDeptEntity extends BaseEntity {
      
        private static final long serialVersionUID = 1L;
      
        /**
         * 部门ID
         **/
        @TableId
        private Long deptId;
      
        /**
         * 部门父节点ID
         **/
        private Long parentId;
      
        /**
         * 部门名称
         **/
        private String deptName;
      
        /**
         * 显示顺序
         **/
        private Integer orderNum;
      
        /**
         * 用户状态(0:正常 1:禁用)
         **/
        private Integer status;
      
        @TableField(exist = false)
        private List<SysDeptEntity> children;
      
    }

     2、拦截器MetaObjectHandler

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    /**
     * @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);
            }
        }
    }

    不需要以下代码:


    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    @Configuration
    public class MyBatisPlusConfig {
      
        /**
         * 自动填充功能
         * @return
         */
        @Bean
        public GlobalConfig globalConfig() {
            GlobalConfig globalConfig = new GlobalConfig();
            globalConfig.setMetaObjectHandler(new MetaHandler());
            return globalConfig;
        }
      
    }

     3、测试

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

    参考文章

    使用MyBatis Plus自动添加数据库表中的创建时间、创建者、更新时间、更新者

    mybatisPlus自动填充更新时间

    mybatis-plus实战-时间字段自动更新

    使用mybatis plus插入或更新数据时mysql表的添加时间和更新时间字段没有更新(mybatis plus坑之一)

    到此这篇关于MybatisPlus 插入或更新数据时自动填充更新数据解决方案的文章就介绍到这了

    原文链接:https://blog.csdn.net/weixin_43272781/article/details/110198852

    服务器评测 http://www.cncsto.com/ 

    服务器测评 http://www.cncsto.com/ 

    站长资源 https://www.cscnn.com/ 

    小鱼创业 https://www.237fa.com/ 


    文章分类
    后端
    版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
    相关推荐