MyMetaObjectHandler.java 1.53 KB
package com.huaheng.framework.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.huaheng.common.utils.security.ShiroUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * Created by Enzo Cotter on 2020/7/7.
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler{
    /**
     * 插入元对象字段填充(用于插入时对公共字段的填充)
     *
     * @param metaObject 元对象
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill ....");
        this.setFieldValByName("created", new Date(), metaObject);
        if(ShiroUtils.getLoginName() != null) {
            this.setFieldValByName("createdBy", ShiroUtils.getLoginName(), metaObject);
        }
        this.setFieldValByName("lastUpdated", new Date(), metaObject);
        if(ShiroUtils.getLoginName() != null) {
            this.setFieldValByName("lastUpdatedBy", ShiroUtils.getLoginName(), metaObject);
        }
    }

    /**
     * 更新元对象字段填充(用于更新时对公共字段的填充)
     *
     * @param metaObject 元对象
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("lastUpdated", new Date(), metaObject);
        if(ShiroUtils.getLoginName() != null) {
            this.setFieldValByName("lastUpdatedBy", ShiroUtils.getLoginName(), metaObject);
        }
    }
}