多租户实现
参考文章:https://blog.csdn.net/qq_37361535/article/details/121212070
1.两个配置文件,需要修改字段就修改getTenantIdColumn的返回值,值的修改同理
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
@Component
@Slf4j
public class CustomTenantLineHandler implements TenantLineHandler {
private static final List<String> IGNORE_TENANT_TABLES = Arrays.asList("`sys_user`");//不配置多租户功能的表;这里需要注意表名不要写错,是否需要``根据自己所写的sql语句来定
//sql语句中``的作用就是与数据库的关键字区分
/**
* 获取租户 ID 值表达式,只支持单个 ID 值
* @return 租户 ID 值表达式
*/
@Override
public Expression getTenantId() {
return new LongValue(1);
}
/**
* 获取租户字段名
* 默认字段名叫: tenant_id
* 数据库中字段用的userId
* @return 租户字段名
*/
@Override
public String getTenantIdColumn() {
return "tenant_id";
}
/**
* 根据表名判断是否忽略拼接多租户条件
* 默认都要进行解析并拼接多租户条件
*
* @param tableName 表名
* @return 是否忽略, true:表示忽略,false:需要解析并拼接多租户条件
*/
@Override
public boolean ignoreTable(String tableName) {
return IGNORE_TENANT_TABLES.stream().anyMatch(ignoreTableName -> ignoreTableName.equalsIgnoreCase(tableName));
}
}
第二个:
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableTransactionManagement
@MapperScan(basePackages = {"com.example.demo.dao"})
public class MybatisPlusConfig {
/**
* 配置拦截器(包括分页拦截器)
* 新多租户插件配置,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存万一出现问题
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// 多租户拦截器
interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new CustomTenantLineHandler()));
//interceptor.addInnerInterceptor(new MyTenantLineInnerInterceptor(new MyTenantLineHandler()));
// 如果用了分页插件注意先 add TenantLineInnerInterceptor 再 add PaginationInnerInterceptor
// 用了分页插件必须设置 MybatisConfiguration#useDeprecatedExecutor = false
// 分页插件
return interceptor;
}
@Bean
public ConfigurationCustomizer configurationCustomizer() {
return configuration -> configuration.setUseDeprecatedExecutor(false);
}
}
如果是多个参数,即
原来是
select * from xx where tenant_id=1
修改为
select * from xx where tenant_id in (1,2)
不看上面的配置文件,新的几个配置文件:
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.StringValue;
import net.sf.jsqlparser.util.cnfexpression.MultipleExpression;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
public class MyTenantLineHandler implements TenantLineHandler {
@Override
public Expression getTenantId() {
List<Expression> childlist =new ArrayList<>();
childlist.add(new StringValue("1"));
childlist.add(new StringValue("2"));
MultipleExpression multipleExpression=new MultipleExpression(childlist) {
@Override
public String getStringExpression() {
return ",";
}
};
return multipleExpression;
}
@Override
public String getTenantIdColumn() {
return "tenant_id";
}
/**
* 这个地方需要排除没有这个权限字段的表,如果是嵌套或者连表,都会添加上这个多租户
* @param tableName
* @return
*/
@Override
public boolean ignoreTable(String tableName) {
// if(tableName.equals("subselect")){
// return false;
// }
// return true;
return false;
}
}
第二个:
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.Parenthesis;
import net.sf.jsqlparser.expression.StringValue;
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
import net.sf.jsqlparser.expression.operators.conditional.OrExpression;
import net.sf.jsqlparser.expression.operators.relational.InExpression;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.delete.Delete;
import net.sf.jsqlparser.statement.insert.Insert;
import net.sf.jsqlparser.statement.select.SelectBody;
import net.sf.jsqlparser.statement.update.Update;
import net.sf.jsqlparser.util.cnfexpression.MultipleExpression;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
public class MyTenantLineInnerInterceptor extends TenantLineInnerInterceptor {
public MyTenantLineInnerInterceptor(TenantLineHandler tenantLineHandler) {
super(tenantLineHandler);
}
/**
* 重构mybatispuls的多租户改为支持in
* @param currentExpression
* @param table
* @return
*/
@Override
protected Expression builderExpression(Expression currentExpression, Table table) {
Column aliasColumn = this.getAliasColumn(table);
boolean presenceOfField=true;
if(presenceOfField) {
InExpression inExpression = new InExpression();
inExpression.setLeftExpression(aliasColumn);
inExpression.setRightExpression(getTenantLineHandler().getTenantId());
if (currentExpression == null) {
return inExpression;
} else {
return currentExpression instanceof OrExpression ? new AndExpression(new Parenthesis(currentExpression), inExpression) : new AndExpression(currentExpression, inExpression);
}
}else{
return currentExpression;
}
}
// @Override
// protected void processInsert(Insert insert, int index, String sql, Object obj) {
//
// }
//
// @Override
// protected void processUpdate(Update update, int index, String sql, Object obj) {
//
// }
//
// @Override
// protected void processDelete(Delete delete, int index, String sql, Object obj) {
//
// }
@Override
protected void processInsertSelect(SelectBody selectBody) {
}
}
第三个
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableTransactionManagement
@MapperScan(basePackages = {"com.example.demo.dao"})
public class MybatisPlusConfig {
/**
* 配置拦截器(包括分页拦截器)
* 新多租户插件配置,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存万一出现问题
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// 多租户拦截器
//interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new CustomTenantLineHandler()));
interceptor.addInnerInterceptor(new MyTenantLineInnerInterceptor(new MyTenantLineHandler()));
// 如果用了分页插件注意先 add TenantLineInnerInterceptor 再 add PaginationInnerInterceptor
// 用了分页插件必须设置 MybatisConfiguration#useDeprecatedExecutor = false
// 分页插件
return interceptor;
}
@Bean
public ConfigurationCustomizer configurationCustomizer() {
return configuration -> configuration.setUseDeprecatedExecutor(false);
}
}