18

Mybatis-plus 实体类继承关系 插入默认值

 4 years ago
source link: http://www.cnblogs.com/Mr-Kenson/p/12180526.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

在实际开发中,会定义一些公共字段,而这些公共字段,一般都是在进行操作的时候由程序自动将默认值插入。而公共的字段一般会被封装到一个基础的实体类中,同时实体类中会实现相应的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")// 日期格式转换

业务类继承基础实体类进行共有字段扩展

Brmiumu.png!web

创建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 的默认值插入就已经完成了。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK