Application.yml 配置
mybatis-plus:
configuration:
call-setters-on-nulls: true #设置当查询结果值为null时,同样映射该查询字段给map。
map-underscore-to-camel-case: false # 数据库下划线自动转驼峰标示关闭
auto-mapping-behavior: full
# log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath*:/mapper/*Mapper.xml,classpath*:/mapper/custom/*.xml
global-config:
# 逻辑删除配置
db-config:
# 逻辑删除字段
logic-delete-field: deleted
# 删除前
logic-not-delete-value: 0
# 删除后
logic-delete-value: 1
模型
/**
* 是否删除 0:正常 1:已删除
*/
@TableField(value = "deleted")
private Byte deleted;
/**
* 创建用户
*/
@TableField(value = "create_by", fill = FieldFill.INSERT)
private Long createBy;
/**
* 更新用户
*/
@TableField(value = "update_by", fill = FieldFill.UPDATE)
private Long updateBy;
/**
* 创建时间
*/
@TableField(value = "create_time", fill = FieldFill.INSERT)
private Date createTime;
/**
* 更新时间
*/
@TableField(value = "update_time", fill = FieldFill.UPDATE)
private Date updateTime;
package com.zyy.config;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.zyy.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
/**
* @author zhouhao
* @version 1.0
* @description: 自动填充功能
* @date 2021/4/28 15:00
*/
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
// 创建时间和更新时间都插入
Object created = getFieldValByName("createTime", metaObject);
if (null == created) {
// 字段为空,可以进行填充
setFieldValByName("createTime", DateTime.now(), metaObject);
}
Object updated = getFieldValByName("updateTime", metaObject);
if (null == updated) {
// 字段为空,可以进行填充
setFieldValByName("updateTime", DateTime.now(), metaObject);
}
this.setFieldValByName("createBy",SecurityUtils.getCurrentUserId(), metaObject);
}
@Override
public void updateFill(MetaObject metaObject) {
this.setFieldValByName("updateTime", DateTime.now(), metaObject);
this.setFieldValByName("updateBy",SecurityUtils.getCurrentUserId(), metaObject);
}
}