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

网友投稿 1676 2022-12-05

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

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

目录解决方案1、 实体类 2、-MetaObjectHandler 3、测试参考文章

Maven

org.springframework.boot

spring-boot-starter-parent

2.2.6.RELEASE

com.baomidou

mybatis-plus-boot-starter

3.1.0

解决方案

1、 实体类

/**

* 基础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;

}

@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 children;

}

2、-MetaObjectHandler

/**

* @author ShenTuZhiGang

* @version 1.0.0

* @date 202http://0-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(authenticatihttp://on) && authentication!=null){

AuthenticationUser user = (AuthenticationUser) authentication.getPrincipal();

this.setFieldValByName("createUser", user.getUsername(), metaObject);

olggEoG 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操作,代码更优美

return R.ok();

}

参考文章

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

mybatisPlus自动填充更新时间

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

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

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:idea中加入git版本控制的方法及步骤详解
下一篇:Mybatis Plus 实现批量插入的示例代码
相关文章

 发表评论

暂时没有评论,来抢沙发吧~