springboot集成mybatis-plus配置字段⾃动填充,主键id使⽤雪
花算法,逻。。。
⼀、添加依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<!--引⼊阿⾥的TransmittableThreadLocal -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>transmittable-thread-local</artifactId>
<version>2.12.1</version>
</dependency>
⼆、编写yaml 配置⽂件:application-dev.yaml
#mybatis-plus配置项
mybatis-plus:
mapperLocations:
typeAliasesPackage:
configuration:
map-underscore-to-camel-case: true
cache-enabled: true
global-config:
db-config:
#配置全局逻辑删除,指定实体类的逻辑删除字段
logic-delete-field: delFlag
logic-delete-value: 1
logic-not-delete-value: 0
#配置主键id⽣成策略采⽤雪花算法
id-type: assign-id
#⾃定义多租户配置
exam:
tenant:
#启⽤多租户
enable: true
#指定哪些表不⾛多租户
ignoreTables:
# - exam_subject
# - exam_subject_category
#数据库租户id字段
column: tenant_id
三、主键id使⽤雪花算法⽣成策略
在配置⽂件上⾯配置了之后,在对应的实体类的id属性上添加 如下注解
@TableId(value="id",type=IdType.ASSIGN_ID)
四、配置字段⾃动填充
编写⼀个⾃定义类实现 MetaObjectHandler 接⼝
public class  MateMetaObjectHandler implements MetaObjectHandler{
//此处的createTime 和 modifiedTime 也是对应实体类的属性名称,⽽⾮数据库字段名
@Override
public void insertFill(MetaObject metaObject){
this.strictInsertFill(metaObject,"createTime",LocalDateTime.w());
this.strictInsertFill(metaObject,"modifiedTime",LocalDateTime.w());      }
@Override
public void updateFill(MetaObject metaObject){
this.strictUpdateFill(metaObject,"modifiedTime",LocalDateTime.w());      }
}
然后需要在需要进⾏⾃动填充的实体类上添加如下注解
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
@TableFidld(value="create_time",fill = FieldFill.INSERT)
private LocalDateTime createTime;
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
@TableFidld(value="modified_time",fill = FieldFill.INSERT_UPDATE)
private LocalDateTime modifiedTime;
五、配置分页,多租户,防⽌全表更新或删除插件
import lombok.Getter;
import lombok.Setter;
import org.t.properties.ConfigurationProperties; import org.t.properties.EnableConfigurationProperties; //import org.t.config.annotation.RefreshScope;
import java.util.ArrayList;
springboot推荐算法import java.util.Arrays;
import java.util.List;
/**
* @author DuanZhaoXu
* @ClassName:
* @Description:
* @date 2021年06⽉03⽇ 17:36:15
*/
@Getter
@Setter
//@RefreshScope
@ConfigurationProperties(prefix = "ant")
public class TenantProperties {
/**
* 是否开启租户模式
*/
private Boolean enable = false;
/**
* 需要排除的多租户的表
*/
private List<String> ignoreTables = new ArrayList();
/**
* 多租户字段名称
*/
private String column = "tenant_id";
//    /**
//    * 排除不进⾏租户隔离的sql
//    * 样例全路径:vip.mate.system.mapper.UserMapper.findList
//    */
//    private List<String> ignoreSqls = new ArrayList<>();
}
import l.TransmittableThreadLocal;
perimental.UtilityClass;
/**
* 多租户Holder
*
* @author pangu
* @since 2020-9-8
*/
@UtilityClass
public class TenantContextHolder {
/
**
* ⽀持⽗⼦线程之间的数据传递
*/
private final ThreadLocal<String> THREAD_LOCAL_TENANT = new TransmittableThreadLocal<>();
/**
* TTL 设置租户ID<br/>
* <b>谨慎使⽤此⽅法,避免嵌套调⽤。尽量使⽤ {@code TenantBroker} </b>
*
* @param tenantId 租户ID
*/
public void setTenantId(String tenantId) {
THREAD_LOCAL_TENANT.set(tenantId);
}
/**
* 获取TTL中的租户ID
*
* @return String
*/
public String getTenantId() {
return THREAD_();
}
/**
* 清除tenantId
*/
public void clear() {
THREAD_ve();
}
}
/**
* 多租户常量
* @author pangu
* @date 2020-9-7
*/
public interface TenantConstant {
/**
* header 中租户ID
*/
String MATE_TENANT_ID = "mate-tenant";
/**
* 租户id参数
*/
String MATE_TENANT_ID_PARAM = "tenantId";
/**
* 租户ID
*/
String TENANT_ID_DEFAULT = "1";
}
package fig;
import sion.plugins.handler.TenantLineHandler; import sion.plugins.inner.TenantLineInnerInterceptor; import lombok.AllArgsConstructor;
import net.pression.Expression;
import net.pression.NullValue;
import net.pression.StringValue;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.t.properties.EnableConfigurationProperties; import org.spring
import t.annotation.Configuration;
/**
* 多租户配置中⼼
*
* @author pangu
* @Date 2020-9-7
*/
@Configuration
@AllArgsConstructor
@AutoConfigureBefore(MybatisPlusConfiug.class)
@EnableConfigurationProperties(TenantProperties.class)
public class TenantConfiguration {
private final TenantProperties tenantProperties;
/**
* 新多租户插件配置,⼀缓和⼆缓遵循mybatis的规则,
* 需要设置 MybatisConfiguration#useDeprecatedExecutor = false
* 避免缓存万⼀出现问题
*
* @return TenantLineInnerInterceptor
*/
@Bean
public TenantLineInnerInterceptor tenantLineInnerInterceptor() {
return new TenantLineInnerInterceptor(new TenantLineHandler() {

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。