在實際開發中,會定義一些公共字段,而這些公共字段,一般都是在進行操作的時候由程序自動將默認值插入。而公共的字段一般會被封裝到一個基礎的實體類中,同時實體類中會實現相應的getter setter 方法(注:如果使用了Lombok 插件,就沒有getter setter方法,相關注解請自行了解),同時,會用到相關注解。在下文中會一一講到。
本文的技術選型為: springboot 2.2.2 + mybatis-plus 3, maven構建項目
相關依賴:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> </parent> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.3.0</version> </dependency>
實體類:
將公共字段封裝到基類中,供其他業務實體類進行調用
package com.hl001.system.base.entity; import java.io.Serializable; import java.util.Date; import org.springframework.format.annotation.DateTimeFormat; import com.baomidou.mybatisplus.annotation.FieldFill; import com.baomidou.mybatisplus.annotation.TableField; import com.fasterxml.jackson.annotation.JsonFormat; import cn.afterturn.easypoi.excel.annotation.Excel; public class HEntity implements Serializable { private static final long serialVersionUID = 1L; @TableField(value="crt_code",fill = FieldFill.INSERT) @Excel(name = "創建人編碼", width = 15) private String crtCode; @TableField(value="crt_name",fill = FieldFill.INSERT) @Excel(name = "創建人", width = 15) private String crtName; @Excel(name = "創建時間", width = 15,format = "yyyy-MM-dd HH:mm:ss") @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @TableField(value="crt_dt",fill = FieldFill.INSERT) private Date crtDt; @TableField(value="proj_code",fill = FieldFill.INSERT) @Excel(name = "項目編碼", width = 15) private String projCode; @TableField(value="comp_code",fill = FieldFill.INSERT) @Excel(name = "公司編碼", width = 15) private String compCode; @TableField(value="org_code",fill = FieldFill.INSERT) @Excel(name = "組織機構碼", width = 15) private String orgCode; // 省略 getter setter 方法 }
注解解釋:
@TableField(value="crt_code",fill = FieldFill.INSERT): 該注解主要是Mybatis 插入默認值 value值為數據庫表中的字段,file是用來指定策略的,可以新增時插入,也可以修改時插入
@Excel(name = "創建人編碼", width = 15) // 用於 Excel 導入導出(此處不做交多闡述)
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss") // JSON 日期格式
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")// 日期格式轉換
業務類繼承基礎實體類進行共有字段擴展
創建Mybatis plus 攔截器的配置
/** * 自動填充功能 * @return */ @Bean public GlobalConfig globalConfig() { GlobalConfig globalConfig = new GlobalConfig(); globalConfig.setMetaObjectHandler(new MetaHandler()); return globalConfig; }
@Component public class MetaHandler implements MetaObjectHandler { @Override public void insertFill(MetaObject metaObject) { // 插入操作時,添加默認數據 SysUser user = ResourceUtil.getSessionUser();
// 注意 第一個參數必須和實體類中的參數名一致,否則會報錯 this.setFieldValByName("crtName", user.getRealname(), metaObject); this.setFieldValByName("crtCode", user.getUsername(),metaObject); this.setFieldValByName("crtDt", new Date(),metaObject); this.setFieldValByName("projCode","ss" ,metaObject); this.setFieldValByName("orgCode", "ssss",metaObject); this.setFieldValByName("compCode", "來了老弟",metaObject); } @Override public void updateFill(MetaObject metaObject) {// 更新操作時 添加默認數據 // TODO Auto-generated method stub }
單元測試 。。。。 省略
至此 Mybatis-plus 的默認值插入就已經完成了。