Mybatis-plus自动填充starter
前言:
上一篇我们说了自动填充,现在利用spring-boot把它抽象为一个组件,可以在项目中共同使用
工具
- IDEA 2021.1.1
- MAVEN 3.6.3
- JDK1.8
- spring-boot 2.1.3.RELEASE
配置
一、引入依赖
<!-- mybatis-plus start 勿需再引入mybatis依赖了 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
<!-- 配置提示 可有可无 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
二、配置自动填充的字段与启用
package com.ypshengxian.mybatisplus.properties;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* 描述:自动填充配置
* Created on 2021/7/15 09:34
* @author zjw
*/
@Setter
@Getter
@ConfigurationProperties(prefix = "ypsx.mybatis-plus.auto-fill")
public class MybatisPlusAutoFillProperties {
/**
* 是否开启自动填充字段
*/
private Boolean enabled = true;
/**
* 是否开启了插入填充
*/
private Boolean enableInsertFill = true;
/**
* 是否开启了更新填充
*/
private Boolean enableUpdateFill = true;
/**
* 创建时间字段名
*/
private String createTimeField = "gmtCreate";
/**
* 更新时间字段名
*/
private String updateTimeField = "gmtModified";
}
三、实现元对象处理器接口:MetaObjectHandler
- 实现元对象处理器接口:com.baomidou.mybatisplus.core.handlers.MetaObjectHandler
package com.ypshengxian.mybatisplus.config;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.ypshengxian.mybatisplus.properties.MybatisPlusAutoFillProperties;
import org.apache.ibatis.reflection.MetaObject;
import java.util.Date;
/**
* 描述:自动填充
* Created on 2021/7/14 18:58
* @author zjw
*/
public class MyMetaObjectHandler implements MetaObjectHandler {
private final MybatisPlusAutoFillProperties autoFillProperties;
public MyMetaObjectHandler(MybatisPlusAutoFillProperties autoFillProperties) {
this.autoFillProperties = autoFillProperties;
}
/**
* 是否开启了插入填充
*/
@Override
public boolean openInsertFill() {
return autoFillProperties.getEnableInsertFill();
}
/**
* 是否开启了更新填充
*/
@Override
public boolean openUpdateFill() {
return autoFillProperties.getEnableUpdateFill();
}
/**
* 插入填充,字段为空自动填充
*/
@Override
public void insertFill(MetaObject metaObject) {
Object createTime = getFieldValByName(autoFillProperties.getCreateTimeField(), metaObject);
Object updateTime = getFieldValByName(autoFillProperties.getUpdateTimeField(), metaObject);
if (createTime == null || updateTime == null) {
Date date = new Date();
if (createTime == null) {
setFieldValByName(autoFillProperties.getCreateTimeField(), date, metaObject);
}
if (updateTime == null) {
setFieldValByName(autoFillProperties.getUpdateTimeField(), date, metaObject);
}
}
}
/**
* 更新填充
*/
@Override
public void updateFill(MetaObject metaObject) {
setFieldValByName(autoFillProperties.getUpdateTimeField(), new Date(), metaObject);
}
// // TODO: 2021/7/14 测试?
// @Override
// public void insertFill(MetaObject metaObject) {
// this.strictInsertFill(metaObject, "gmtCreate", Date.class, new Date()); // 起始版本 3.3.0(推荐使用)
// this.strictInsertFill(metaObject, "gmtModified", Date.class, new Date()); // 起始版本 3.3.0(推荐使用)
// }
//
// @Override
// public void updateFill(MetaObject metaObject) {
// this.strictUpdateFill(metaObject, "gmtModified", Date.class, new Date()); // 起始版本 3.3.0(推荐)
// }
}
四、配置AutoConfigure
注入 MetaObjectHandler
Bean
package com.ypshengxian.mybatisplus.config;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.ypshengxian.mybatisplus.properties.MybatisPlusAutoFillProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
/**
* 描述:mybatis-plus配置
* Created on 2021/7/15 09:37
* @author zjw
*/
@EnableConfigurationProperties(MybatisPlusAutoFillProperties.class)
public class MybatisPlusAutoConfigure {
@Autowired
private MybatisPlusAutoFillProperties autoFillProperties;
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix = "ypsx.mybatis-plus.auto-fill", name = "enabled", havingValue = "true", matchIfMissing = true)
public MetaObjectHandler metaObjectHandler() {
return new MyMetaObjectHandler(autoFillProperties);
}
}
五、默认生效
resources
资源文件夹下新建META-INF/spring.factories
,内容如下
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.ypshengxian.mybatisplus.config.MybatisPlusAutoConfigure
标签:metaObject,填充,plus,Mybatis,import,autoFillProperties,com,public,starter
From: https://blog.51cto.com/u_11906056/7067573