重写
TenantLineInnerInterceptor#beforeQuery
@Configuration
@MapperScan("com.fs.zhnm.edge.data.center.accountauth.dao")
public class MybatisPlusConfig {
@Autowired
private RedisFeignAPI redisFeignAPI;
public static final String TENANT_CASE_KEY = "TENANT_CASE_KEY";
/**
* 忽略添加租户ID的表
*/
private static List<String> IGNORE_TABLE_NAMES = Lists.newArrayList(
"account_employee_relation","account_org_relation","account_role_relation","application_info","default_application"
,"operate_auth","operate_auth_app","operation_account_info","role_operate_auth_relation","tenement_application_relation","tenement_info"
);
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new MyTenantLineInnerInterceptor(new TenantLineHandler()
{
@Override
public Expression getTenantId() {
HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
String token = request.getHeader("token");
RetResult<Object> value = redisFeignAPI.getValue(token);
AccountInfoVo accountInfoVo = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create().fromJson(new Gson().toJson(value.getData()), AccountInfoVo.class);
if (null != accountInfoVo) {
Long tenementId = accountInfoVo.getTenementId();
if (null == tenementId) {
return new NullValue();
}
//租户Id,可以从缓存或者cookie,token等中获取
return new LongValue(accountInfoVo.getTenementId());
}
return new NullValue();
}
/**
* 获取租户字段名(数据库的租户ID字段名)
*
* @return
*/
@Override
public String getTenantIdColumn() {
return "tenement_id";
}
/**
* 根据表名判断是否忽略拼接多租户条件
*
* @param tableName
* @return
*/
@Override
public boolean ignoreTable(String tableName) {
return IGNORE_TABLE_NAMES.contains(tableName);
}
}));
PaginationInnerInterceptor innerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
interceptor.addInnerInterceptor(innerInterceptor);
return interceptor;
}
}
@Data
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = true)
public class MyTenantLineInnerInterceptor extends TenantLineInnerInterceptor {
public MyTenantLineInnerInterceptor() {
super();
}
public MyTenantLineInnerInterceptor(TenantLineHandler tenantLineHandler) {
super(tenantLineHandler);
}
private static List<String> IGNORE_STATEMENT_NAMES = Lists.newArrayList(
"com.fs.zhnm.edge.data.center.accountauth.dao.AccountInfoDao.getLoginAccountRole"
);
@Override
public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
// 如果statementid存在,则忽略方法
if (IGNORE_STATEMENT_NAMES.contains(ms.getId())) return;
super.beforeQuery(executor, ms, parameter, rowBounds, resultHandler, boundSql);
}
}