JPA和myBatis自定义填充createdBy、createdTime、updatedBy、updatedTime等字段信息

JPA:

方案一:(不支持主动传值

1.在启动类上添加@EnableJpaAuditing
2.在实体类添加@EntityListeners(AuditingEntityListener.class)
3.在字段上添加@CreatedBy、@CreatedDate、@LastModifiedBy、@LastModifiedDate

方案二:(支持传值,不传则默认当前人、当前时间

1、自定义监听器:

/**
 * 自定义监听类
 * @author gourd
 */
@Component
@Slf4j
public class DataBaseAuditListener {
    /**
     * 被@Prepersist注解的方法 ,完成save之前的操作。
     * @param object
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     */
    @PrePersist
    public void prePersist(Object object)
            throws IllegalArgumentException, IllegalAccessException {
        // 如果填充字段被分装在一个父类中: Class aClass = object.getClass().getSuperclass();
        Class aClass = object.getClass()
        try {
            Field createdBy = aClass.getDeclaredField("createdBy");
            createdBy.setAccessible(true);
            // 获取createdBy值
            Object createdByValue = createdBy.get(object);
            if(createdByValue == null){
                // currentUserId为业务中自己获取当前用户id
                Long currentUserId =1L;
                createdBy.set(object,currentUserId) ;
            }
            Field createdTime = aClass.getDeclaredField("createdTime");
            createdTime.setAccessible(true);
            // 获取createdTime值
            Object createdTimeValue = createdTime.get(object);
            if(createdTimeValue == null){
                createdTime.set(object,new Date()) ;
            }

        } catch (NoSuchFieldException e) {
            log.error("反射获取属性异常:",e);
        }


    }
    /**
     * 被@Preupdate注解的方法 ,完成update之前的操作。
     * @param object
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     */
    @PreUpdate
    public void PreUpdate(Object object)
            throws IllegalArgumentException, IllegalAccessException {
        Class aClass = object.getClass().getSuperclass();
        try {
            Field updatedBy = aClass.getDeclaredField("updatedBy");
            updatedBy.setAccessible(true);
            // 获取updatedBy值
            Object updatedByValue = updatedBy.get(object);
            if(updatedByValue == null){
                // currentUserId为业务中自己获取当前用户id
                Long currentUserId =1L;
                updatedBy.set(object,currentUserId) ;
            }
            Field updatedTime = aClass.getDeclaredField("updatedTime");
            updatedTime.setAccessible(true);
            // 获取updatedTime值
            Object updatedTimeValue = updatedTime.get(object);
            if(updatedTimeValue == null){
                updatedTime.set(object,new Date()) ;
            }

        } catch (NoSuchFieldException e) {
            log.error("反射获取属性异常:",e);
        }
    }


    /**
     * 被@Postpersist注解的方法 ,完成save之后的操作。
     * @param object
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     */
    @PostPersist
    public void postPersist(Object object)
            throws IllegalArgumentException, IllegalAccessException {

    }
    /**
     * 被@Postupdate注解的方法 ,完成update之后的操作。
     * @param object
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     */
    @PostUpdate
    public void postUpdate(Object object)
            throws IllegalArgumentException, IllegalAccessException {
    }
 
}

2、在类上增加:

@EntityListeners(DataBaseAuditListener.class)

myBatis:

 JPA和myBatis自定义填充createdBy、createdTime、updatedBy、updatedTime等字段信息_第1张图片

具体实现类:

import com.baomidou.mybatisplus.mapper.MetaObjectHandler;
import com.gourd.common.rbac.vo.JwtUser;
import com.gourd.common.utils.JwtUtil;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 自定义填充公共字段 ,即没有传的字段自动填充
 *
 * @author gourd
 **/
@Component
public class MyMetaObjectHandler extends MetaObjectHandler  {
    /**
     *  新增填充
     *
     *  @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        Object createdBy = metaObject.getValue("createdBy");
        Object createdTime = metaObject.getValue("createdTime");
        Object version = metaObject.getValue("version");
        Object deleted = metaObject.getValue("deleted");
        //获取当前登录用户
        JwtUser userInfo = JwtUtil.getUserInfo();
        if(userInfo != null){
            if (null == createdBy) {
                metaObject.setValue("createdBy", userInfo.getId());
            }
            if (null == createdTime) {
                metaObject.setValue("createdTime", new Date());
            }
            if (null == version) {
                metaObject.setValue("version", 1L);
            }
            if (null == deleted) {
                metaObject.setValue("deleted", false);
            }
        }

    }

    /**
     * 更新填充
     *
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        Object updatedBy = metaObject.getValue("updatedBy");
        Object updatedTime = metaObject.getValue("updatedTime");
        //获取当前登录用户
        JwtUser userInfo = JwtUtil.getUserInfo();
        if(userInfo != null){
            if (null == updatedBy) {
                metaObject.setValue("updatedBy", userInfo.getId());
            }
            if (null == updatedTime) {
                metaObject.setValue("updatedTime", new Date());
            }
        }

    }
}

 

你可能感兴趣的:(自动填充,自动填充)