MyBatis-Plus-实用的功能自动填充字段
前言:
java项目用到了mybatis-plus,在一些类里面需要在更新时候,统一设置,修改人,修改ID,修改时间。新增时候设置 创建人,创建时间等
基础类:
@Data public abstract class BaseModel implements Serializable { /** * 逻辑删除 */ @TableField(value = "is_delete", fill = FieldFill.INSERT) @TableLogic @ApiModelProperty(hidden = true) protected Integer deleted = 0; @TableField(fill = FieldFill.INSERT) @ApiModelProperty(hidden = true) protected String createId; @TableField(fill = FieldFill.INSERT) @ApiModelProperty(hidden = true) protected String createName; @TableField(fill = FieldFill.INSERT) @ApiModelProperty(hidden = true) protected Date createDate; @TableField(fill = FieldFill.INSERT_UPDATE) @ApiModelProperty(hidden = true) protected String modifiedId; @TableField(fill = FieldFill.INSERT_UPDATE,updateStrategy = FieldStrategy.NOT_NULL) @ApiModelProperty(hidden = true) protected String modifiedName; @TableField(fill = FieldFill.INSERT_UPDATE) @ApiModelProperty(hidden = true) protected Date modifiedDate; }
然后,每个业务类都要继承这个基础类:确保数据库字段也都有这些字段。比如:
@Data public class ErpSaleOrderTransactionBill extends BaseModel { private static final long serialVersionUID = 561288556671900702L; /** * 主键id */ @TableId(type = IdType.ASSIGN_ID) @NotEmpty private String id; /** * 账单号 */ private String orderCode; /** * 单据类别 */ private String confOrderCategory; }
mybatis-plus框架,我们可以使用: ,这里我们可以不设置, modifiedName,modifiedDate等
updateBatchById(calculateRiskLevelListUpdate);
最重要的:mybatis-plus拦截执行
package com.alpha.erp.config; import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; import com.orderplus.core.auth.Audience; import com.orderplus.core.util.JwtTokenUtil; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.apache.ibatis.reflection.MetaObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpHeaders; import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; import java.util.Date; @Slf4j @Component public class MetaDataObjectHandler implements MetaObjectHandler { @Autowired private HttpServletRequest request; @Autowired private Audience audience; /** * 获取请求参数 - 用户ID */ protected String getUserId() { try { String token = request.getHeader(HttpHeaders.AUTHORIZATION); if (StringUtils.isEmpty(token)) { return ""; } return JwtTokenUtil.getUserId(token.substring(7), audience.getBase64Secret()); } catch (Exception e) { return ""; } } /** * 获取请求参数 - 用户ID */ protected String getUsername() { try { String token = request.getHeader(HttpHeaders.AUTHORIZATION); if (StringUtils.isEmpty(token)) { return ""; } return JwtTokenUtil.getUsername(token.substring(7), audience.getBase64Secret()); } catch (Exception e) { return ""; } } /** * 获取请求参数 - 集团代码 */ protected String getEnterpriseNumber() { try { String token = request.getHeader(HttpHeaders.AUTHORIZATION); if (StringUtils.isEmpty(token)) { return ""; } return JwtTokenUtil.getEnterpriseNumber(token.substring(7), audience.getBase64Secret()); } catch (Exception e) { return ""; } } @Override public void insertFill(MetaObject metaObject) { log.info("start insert fill ...."); this.strictInsertFill(metaObject, "deleted", Integer.class, 0); // this.strictInsertFill(metaObject, "createId", String.class, getUserId()); this.strictInsertFill(metaObject, "createDate", Date.class, new Date()); // this.strictInsertFill(metaObject, "createName", String.class, getUsername()); // this.strictInsertFill(metaObject, "modifiedId", String.class, getUserId()); this.strictInsertFill(metaObject, "modifiedDate", Date.class, new Date()); // this.strictInsertFill(metaObject, "modifiedName", String.class, getUsername()); this.strictInsertFill(metaObject, "groupCode", String.class, getEnterpriseNumber()); this.strictInsertFill(metaObject, "brand", String.class, getEnterpriseNumber()); this.fillStrategy(metaObject, "createId", getUserId()); this.fillStrategy(metaObject, "createName", getUsername()); this.fillStrategy(metaObject, "modifiedId", getUserId()); this.fillStrategy(metaObject, "modifiedName", getUsername()); } @Override public void updateFill(MetaObject metaObject) { log.info("start update fill ...."); // metaObject.setValue("modifiedDate",null); // metaObject.setValue("modifiedId",null); // metaObject.setValue("modifiedName",null); // this.strictUpdateFill(metaObject, "modifiedDate", Date.class, new Date()); // this.strictUpdateFill(metaObject, "modifiedId", String.class, getUserId()); // this.strictUpdateFill(metaObject, "modifiedName", String.class, getUsername()); metaObject.setValue("modifiedDate",null); this.strictUpdateFill(metaObject, "modifiedDate", Date.class, new Date()); if(StrUtil.isNotBlank(getUserId())){ metaObject.setValue("modifiedId",null); this.strictUpdateFill(metaObject, "modifiedId", String.class, getUserId()); } if(StrUtil.isNotBlank(getUsername())){ metaObject.setValue("modifiedName",null); this.strictUpdateFill(metaObject, "modifiedName", String.class, getUsername()); } } }
然后你可以看到,执行时候会自动赋值。