MybatisPlus自动填充字段值

MybatisPlus自动填充字段值

lixiangrong
2024-01-03 / 0 评论 / 7 阅读 / 正在检测是否收录...
package com.risesun.business.hander;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import com.risesun.common.security.utils.SecurityUtils;
import java.util.Date;

/**
 * @Author LiXiangrong
 * @Description 兼容MybatisPlus自动填充字段值
 * @Date 2023/06/01 14:33:37
 **/
@Component
public class MybatisPlusHandler implements MetaObjectHandler
{
    public static final Logger LOGGER = LoggerFactory.getLogger(MybatisPlusHandler.class);

    @Override
    public void insertFill(MetaObject metaObject)
    {
        LOGGER.info("MybatisPlusHandler开始自动填充创建时间、创建人属性值...");
        this.setFieldValByName("createBy", SecurityUtils.getUsername(),metaObject);
        this.setFieldValByName("createTime",new Date(),metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject)
    {
        LOGGER.info("MybatisPlusHandler开始自动填充更新时间、更新人属性值...");
        this.setFieldValByName("updateBy",SecurityUtils.getUsername(),metaObject);
        this.setFieldValByName("updateTime",new Date(),metaObject);
    }
}
0

评论 (0)

取消