后端框架是springboot+mybatisplus,数据库是MySQL,一般表中都有几个固定字段,基本上所有的表都有的,常见的固定字段如下:
create_time 创建时间
update_time 最后修改时间
creator_id 创建者id
creator_name 创建者名称
updator_id 最后修改者id
updator_name 最后修改者名称
这些字段怎么在进行数据插入或者修改的时候自动填充,一般可以通过数据库或者其他方式来实现。
1.数据库实现
create_time和update_time可以通过数据库实现
MySQL实现:
CREATE TABLE `user_info` (
`uid` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`username` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`password` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`telephone` varchar(11) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`create_time` timestamp(0) NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` timestamp(0) NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP(0) COMMENT '更新时间',
PRIMARY KEY (`uid`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
PostgreSQL实现:
CREATE TABLE `user_info` (
`uid` varchar(255) NOT NULL PRIMARY KEY,
`username` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`telephone` varchar(11) DEFAULT NULL,
`create_time` timestamptz(6) DEFAULT CURRENT_TIMESTAMP,
`update_time` timestamptz(6) DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`uid`) USING BTREE
);
CREATE OR REPLACE FUNCTION "on_update_current_timestamp_user_info"()
RETURNS "pg_catalog"."trigger" AS $BODY$
BEGIN
NEW.update_time = now();
RETURN NEW;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100
;
CREATE TRIGGER "on_update_current_timestamp" BEFORE UPDATE ON "user_info"
FOR EACH ROW
EXECUTE PROCEDURE "on_update_current_timestamp_user_info"();
2.代码实现
creator_id 创建者id
creator_name 创建者名称
updator_id 最后修改者id
updator_name 最后修改者名称
这几个字段可以通过类似切面方式进行实现,如果使用的是mybatis-plus,则可以实现接口MetaObjectHandler来实现
实体层:
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import java.util.Date;
@Data
@SuperBuilder
@NoArgsConstructor
public class BaseDO {
@TableField(fill = FieldFill.INSERT)
private String creatorId;
@TableField(fill = FieldFill.INSERT_UPDATE)
private String updatorId;
@TableField(fill = FieldFill.INSERT)
private String creatorName;
@TableField(fill = FieldFill.INSERT_UPDATE)
private String updatorName;
}
package com.alibaba.guanindex.service.handle;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.guandata.guanindex.service.auth.LoginUser;
import com.guandata.guanindex.service.auth.UnidentifiedUser;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
@Component
public class GuanIndexMetaObjHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
//获取当前登录用户,Threadlocal
LoginUser currentUser = UnidentifiedUser.getCurrentUser();
this.strictInsertFill(metaObject, "domId", String.class, currentUser.getDomId());
this.strictInsertFill(metaObject, "creatorId", String.class, currentUser.getUId());
this.strictInsertFill(metaObject, "creatorName", String.class, currentUser.getName());
this.strictInsertFill(metaObject, "updateId", String.class, currentUser.getUId());
this.strictInsertFill(metaObject, "updateName", String.class, currentUser.getName());
}
@Override
public void updateFill(MetaObject metaObject) {
LoginUser currentUser = UnidentifiedUser.getCurrentUser();
this.strictUpdateFill(metaObject, "updateId", String.class, currentUser.getUId());
this.strictUpdateFill(metaObject, "updateName", String.class, currentUser.getName());
}
}
strictInsertFill也可以使用setFieldValByName,这样TableField注解不需要加fill属性。
default MetaObjectHandler setFieldValByName(String fieldName, Object fieldVal, MetaObject metaObject) {
if (Objects.nonNull(fieldVal) && metaObject.hasSetter(fieldName)) {
metaObject.setValue(fieldName, fieldVal);
}
return this;
}
setFieldValByName中会判断是否存在set方法,不存在则不会设置,而strictUpdateFill如果没有固定的字段的话会报错,所以根据具体业务决定使用哪一个。