Spring中的aware接口详情
291
2022-11-13
mybatisplus的公共字段插入的实现
Mybatis的公共字段插入的实现,具体内容如下所示:
公共的基础类
package com.yss.common.domain;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import lombok.Data;
import java.util.Date;
/**
* @author wyy
* @version 1.0
* @date 2020/11/5 13:23
*/
@Data
public abstract class BasicEntity
@TableId(value = "ID",type = IdType.ASSIGN_ID)
private String id;
@TableField(value = "CREATOR_ID",fill = FieldFill.INSERT )
private String creatorId;
@TableField(value = "CREATOR",fill = FieldFill.INSERT)
private String creator;
@TableField(value = "CREATETIME",fill = FieldFill.INSERT)
private Date createTime;
@TableField(value = "MODIFY_ID",fill = FieldFill.UPDATE)
private String modifyId;
@TableField(value = "MODIFY_BY",fill = FieldFill.UPDATE)
private String modifyBy;
@TableField(value = "MODIFYTIME",fill = FieldFill.UPDATE)
private Date modifyTime;
@TableField("DELETED")
private int deleted;
}
该类定义了主要基于泛型,以及公共插入(createor/creatorid)与更新的字段(modifyid/modifyby)。
新增注解:@TableField(value = “CREATETIME”,fill = FieldFill.INSERT)
更新注解:@TableField(value = “MODIFY_BY”,fill = FieldFill.UPDATE)
MybatisPlus对公共字段的配置
package com.yss.common.utils;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.yss.common.domain.SSOUserInfo;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
* @author wyy
* @version 1.0
* @date 2020/11/5 13:30
*/
@Component
public class MybatisAuditHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
SSOUserInfo ssoUserInfo = UserInfoUtils.getThreadUserInfo();
this.strictInsertFill(metaObject,"creatorId",String.class, ssoUserInfo.getId());
this.strictInsertFill(metaObject,"creator", String.class,ssoUserInfo.getUserPrincipalName());
this.strictInsertFill(metaObject,"createTime", Date.class,new Date());
}
@Override
public void updateFill(MetaObject metaObject) {
SSOUserInfo ssoUserInfo = UserInfoUtils.getThreadUserInfo();
this.strictInsertFill(metaObject,"modifyId",String.class, ssoUserInfo.getId());
this.strictInsertFill(metaObject,"modifyBy", String.class,ssoUserInfo.getUserPrincipalName());
this.strictInsertFill(metaObject,"modifyTime", Date.class,new Date());
}
}
需要注意的是采用@component注解,告知spring容器是一个bean等。这样的话就实现了mybatisplus的公共自断的新增与更改。
要注意的是,在进行handler方法重写时,
this.strictInsertFill(metaObject,“creator”, String.class,ssoUserInfo.getUserPrincipalName());
这是自的是实体类的字段,并不是数据库对应字段。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~