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());
这是自的是实体类的字段,并不是数据库对应字段。
到此这篇关于mybatisplus的公共字段插入的文章就介绍到这了,更多相关mybatisplus公共字段内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
赞 (0)