mybatisplus的公共字段插入的实现
程序员文章站
2022-07-03 19:47:09
mybatis的公共字段插入的实现,具体内容如下所示:公共的基础类package com.yss.common.domain;import com.baomidou.mybatisplus.annot...
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公共字段内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!