59 lines
2.2 KiB
Java
59 lines
2.2 KiB
Java
package com.label.config;
|
|
|
|
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
|
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
|
|
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
|
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
|
|
import com.label.common.context.CompanyContext;
|
|
import net.sf.jsqlparser.expression.Expression;
|
|
import net.sf.jsqlparser.expression.LongValue;
|
|
import net.sf.jsqlparser.expression.NullValue;
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
import java.util.Arrays;
|
|
import java.util.List;
|
|
|
|
@Configuration
|
|
public class MybatisPlusConfig {
|
|
|
|
// Tables that do NOT need tenant isolation (either global or tenant root tables)
|
|
private static final List<String> IGNORED_TABLES = Arrays.asList(
|
|
"sys_company", // the tenant root table itself
|
|
"sys_config", // has company_id=NULL for global defaults; service handles this manually
|
|
"video_process_job" // accessed by unauthenticated callback endpoint; service validates companyId manually
|
|
);
|
|
|
|
@Bean
|
|
public MybatisPlusInterceptor mybatisPlusInterceptor() {
|
|
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
|
|
|
// 1. Tenant isolation - auto-injects WHERE company_id = ?
|
|
interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new TenantLineHandler() {
|
|
@Override
|
|
public Expression getTenantId() {
|
|
Long companyId = CompanyContext.get();
|
|
if (companyId == null) {
|
|
return new NullValue();
|
|
}
|
|
return new LongValue(companyId);
|
|
}
|
|
|
|
@Override
|
|
public String getTenantIdColumn() {
|
|
return "company_id";
|
|
}
|
|
|
|
@Override
|
|
public boolean ignoreTable(String tableName) {
|
|
return IGNORED_TABLES.contains(tableName);
|
|
}
|
|
}));
|
|
|
|
// 2. Pagination interceptor (required for MyBatis Plus Page queries)
|
|
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
|
|
|
|
return interceptor;
|
|
}
|
|
}
|