mybatisplus公共字段 mybatisplus的公共字段插入的实现
Start afresh 人气:0想了解mybatisplus的公共字段插入的实现的相关内容吗,Start afresh在本文为您仔细讲解mybatisplus公共字段的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:mybatisplus公共字段,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 <T extends Model<?>> extends Model<T> { @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());
这是自的是实体类的字段,并不是数据库对应字段。
加载全部内容