feat: 添加 MyBatis-Plus 多租户配置,自动注入行级数据隔离
This commit is contained in:
@@ -142,7 +142,7 @@
|
||||
<forceJavacCompilerUse>true</forceJavacCompilerUse>
|
||||
<source>17</source>
|
||||
<target>17</target>
|
||||
<compilerArgs>--enable-preview</compilerArgs>
|
||||
|
||||
<annotationProcessorPaths>
|
||||
<path>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.guo.learningprogresstracker.config;
|
||||
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import net.sf.jsqlparser.expression.StringValue;
|
||||
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
|
||||
import net.sf.jsqlparser.expression.Expression;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* MyBatis-Plus 多租户配置
|
||||
* 利用 TenantLineInnerInterceptor 自动在 SELECT/UPDATE/DELETE 语句中
|
||||
* 注入 WHERE created_by = #{当前登录用户},实现行级数据隔离。
|
||||
*/
|
||||
@Configuration
|
||||
public class MybatisPlusConfig {
|
||||
|
||||
/**
|
||||
* 不需要租户过滤的表(仅排除不含 created_by 的系统表)
|
||||
*/
|
||||
private static final List<String> EXCLUDE_TABLES = Arrays.asList(
|
||||
"user",
|
||||
"flyway_schema_history",
|
||||
"databasechangelog",
|
||||
"databasechangeloglock"
|
||||
);
|
||||
|
||||
@Bean
|
||||
public MybatisPlusInterceptor mybatisPlusInterceptor() {
|
||||
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
||||
interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new TenantLineHandler() {
|
||||
@Override
|
||||
public Expression getTenantId() {
|
||||
return new StringValue(StpUtil.getLoginIdAsString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTenantIdColumn() {
|
||||
return "created_by";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean ignoreTable(String tableName) {
|
||||
return EXCLUDE_TABLES.contains(tableName.toLowerCase());
|
||||
}
|
||||
}));
|
||||
return interceptor;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user