xiaohanzi 5 лет назад
Родитель
Сommit
49f71416ef
11 измененных файлов: 387 добавлений и 508 удалений
  1. +78
    -186
      mallinkAdmin/src/main/java/com/iformall/tenant/TenantInfoImpl.java
  2. +67
    -132
      mallinkBApi/src/main/java/com/iformall/tenant/TenantInfoImpl.java
  3. +73
    -141
      mallinkCApi/src/main/java/com/iformall/tenant/TenantInfoImpl.java
  4. +8
    -4
      mybatis-multi-tenancy/src/main/java/com/iformall/plugin/MyBatisItercepters.java
  5. +4
    -1
      mybatis-multi-tenancy/src/main/java/com/iformall/plugin/MyBatisPlus.java
  6. +32
    -8
      mybatis-multi-tenancy/src/main/java/com/iformall/plugin/autoTenantId/MultiTenancy.java
  7. +6
    -0
      mybatis-multi-tenancy/src/main/java/com/iformall/plugin/autoTenantId/TenantInfo.java
  8. +41
    -0
      mybatis-multi-tenancy/src/main/java/com/iformall/plugin/entity/ChainValue.java
  9. +30
    -0
      mybatis-multi-tenancy/src/main/java/com/iformall/plugin/entity/ChainValueHelper.java
  10. +36
    -36
      mybatis-multi-tenancy/src/main/java/com/iformall/plugin/shard/plugin/ShardingSpherePlugin.java
  11. +12
    -0
      mybatis-multi-tenancy/src/test/java/com/iformall/plugin/TenantInfoImpl.java

+ 78
- 186
mallinkAdmin/src/main/java/com/iformall/tenant/TenantInfoImpl.java Просмотреть файл

@@ -3,6 +3,10 @@ package com.iformall.tenant;
import com.iformall.common.TenantThreadLocal;
import com.iformall.plugin.autoTenantId.TenantInfo;
import com.iformall.shiro.UserSession;

import java.util.Arrays;
import java.util.List;

import org.apache.ibatis.mapping.MappedStatement;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.InvalidSessionException;
@@ -12,6 +16,62 @@ import org.slf4j.LoggerFactory;
public class TenantInfoImpl implements TenantInfo {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private static String[] filterTables = new String[] {
"wx_mall",
"mall_permission",
"mall_sale_type",
"mall_user_role",
"wx_channel",
"wx_business",
"wx_sub_business",
"wx_coupon_type",
"wx_game_template",
"wx_group",
"wx_mall_apply",
"wx_tags",
"wx_tags_group",
"wx_tags_type",
"wx_component_verify_ticket",
"wx_authorizer_info",
"wx_user_channel",
"wx_msg_record",
"wx_data_rule_target",
"wx_logic_permission",
"wx_score_rules",
"wx_level_config",
"wx_c_user_basic_info",
"wx_credit_history",
"wx_score_history",
"wx_c_user_tags",
"wx_weapp_ext_set",
"wx_msg_validationcode_model"};
private static String[] filterSubTables = new String[] {
"wx_mall",
"wx_appinfo",
"wx_authorizer_info",
"wx_c_user",
"wx_c_user_car",
"wx_c_user_from_b",
"wx_c_user_tags",
"wx_cuser_floating_layer",
"wx_data_rule_target",
"wx_floating_layer",
"wx_question",
"wx_question_config",
"wx_question_log",
"wx_user_visit",
"wx_level_config",
"wx_score_rules",
"view_coupon_data",
"view_touch_user",
"wx_weapp_audit_status",
"wx_weapp_code_status",
"wx_weapp_release_status",
"wx_c_user_basic_info",
"mem_coupon_from_dsp",
"wx_msg_validationcode_model"};
@Override
public String getTenantId() {
String tenantId = null;
@@ -38,197 +98,19 @@ public class TenantInfoImpl implements TenantInfo {

@Override
public boolean doTableFilter(String tableName) {
if ("wx_mall".equals(tableName)) {
return true;
}
if ("mall_permission".equals(tableName)) {
return true;
}
if ("mall_sale_type".equals(tableName)) {
return true;
}
if ("mall_user_role".equals(tableName)) {
return true;
}
if ("wx_channel".equals(tableName)) {
return true;
}
if ("wx_business".equals(tableName)) {
return true;
}
if ("wx_sub_business".equals(tableName)) {
return true;
}
if ("wx_coupon_type".equals(tableName)) {
return true;
}
if ("wx_game_template".equals(tableName)) {
return true;
}
if ("wx_group".equals(tableName)) {
return true;
}
if ("wx_mall_apply".equals(tableName)) {
return true;
}
if ("wx_tags".equals(tableName)) {
return true;
}
if ("wx_tags_group".equals(tableName)) {
return true;
}
if ("wx_tags_type".equals(tableName)) {
return true;
}
if ("wx_component_verify_ticket".equals(tableName)) {
return true;
}
if ("wx_authorizer_info".equals(tableName)) {
return true;
}
if ("wx_user_channel".equals(tableName)) {
return true;
}
if ("wx_msg_record".equals(tableName)) {
return true;
}
if ("wx_data_rule_target".equals(tableName)) {
return true;
}
if ("wx_logic_permission".equals(tableName)) {
return true;
}
if ("wx_score_rules".equals(tableName)) {
return true;
}
if ("wx_level_config".equals(tableName)) {
return true;
}
if ("wx_c_user_basic_info".equals(tableName)) {
return true;
}
if ("wx_credit_history".equals(tableName)) {
return true;
}
if ("wx_score_history".equals(tableName)) {
return true;
}
if ("wx_c_user_tags".equals(tableName)) {
return true;
}
if ("wx_weapp_ext_set".equals(tableName)) {
return true;
}
if ("wx_msg_validationcode_model".equals(tableName)) {
return true;
}
/*if ("wx_flow_config".equals(tableName)) {
return true;
}
if ("wx_msg_config".equals(tableName)) {
return true;
}
if ("wx_mall_building".equals(tableName)) {
return true;
}
if ("wx_mall_floor".equals(tableName)) {
return true;
}
if ("wx_park".equals(tableName)) {
return true;
}*/
List<String> list = filterTables();
if (list.contains(tableName)) {
return true;
}
return false;
}

@Override
public boolean doTableFilterSub(String tableName) {
if ("wx_mall".equals(tableName)) {
return true;
}
if ("wx_appinfo".equals(tableName)) {
return true;
}
if ("wx_authorizer_info".equals(tableName)) {
return true;
}
if ("wx_c_user".equals(tableName)) {
return true;
}
if ("wx_c_user_car".equals(tableName)) {
return true;
}
if ("wx_c_user_from_b".equals(tableName)) {
return true;
}
if ("wx_c_user_tags".equals(tableName)) {
return true;
}
if ("wx_cuser_floating_layer".equals(tableName)) {
return true;
}
if ("wx_data_rule_target".equals(tableName)) {
return true;
}
if ("wx_floating_layer".equals(tableName)) {
return true;
}
if ("wx_question".equals(tableName)) {
return true;
}
if ("wx_question_config".equals(tableName)) {
return true;
}
if ("wx_question_log".equals(tableName)) {
return true;
}
if ("wx_user_visit".equals(tableName)) {
return true;
}
if ("wx_level_config".equals(tableName)) {
return true;
}
if ("wx_score_rules".equals(tableName)) {
return true;
}
if ("view_coupon_data".equals(tableName)) {
return true;
}
if ("view_touch_user".equals(tableName)) {
return true;
}
if ("wx_weapp_audit_status".equals(tableName)) {
return true;
}
if ("wx_weapp_code_status".equals(tableName)) {
return true;
}
if ("wx_weapp_release_status".equals(tableName)) {
return true;
}
if ("wx_c_user_basic_info".equals(tableName)) {
return true;
}
if ("mem_coupon_from_dsp".equals(tableName)) {
return true;
}
if ("wx_msg_validationcode_model".equals(tableName)) {
return true;
}
/* if ("wx_flow_config".equals(tableName)) {
return true;
}
if ("wx_msg_config".equals(tableName)) {
return true;
}
if ("wx_mall_building".equals(tableName)) {
return true;
}
if ("wx_mall_floor".equals(tableName)) {
return true;
}
if ("wx_park".equals(tableName)) {
return true;
}*/
List<String> list = filterSubTables();
if (list.contains(tableName)) {
return true;
}
return false;
}

@@ -288,4 +170,14 @@ public class TenantInfoImpl implements TenantInfo {
return true;
return false;
}

@Override
public List<String> filterTables() {
return Arrays.asList(filterTables);
}

@Override
public List<String> filterSubTables() {
return Arrays.asList(filterSubTables);
}
}

+ 67
- 132
mallinkBApi/src/main/java/com/iformall/tenant/TenantInfoImpl.java Просмотреть файл

@@ -9,11 +9,62 @@ import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

public class TenantInfoImpl implements TenantInfo {
private final Logger logger = LoggerFactory.getLogger(this.getClass());

private static String[] filterTables = new String[] {
"wx_mall",
"mall_permission",
"mall_sale_type",
"mall_user_role",
"wx_channel",
"wx_business",
"wx_coupon_type",
"wx_game_template",
"wx_group",
"wx_mall_apply",
"wx_tags",
"wx_tags_group",
"wx_tags_type",
"wx_user_channel",
"wx_score_rules",
"wx_level_config",
"wx_c_user_basic_info",
"wx_credit_history",
"wx_score_history",
"wx_c_user_tags",
"wx_weapp_ext_set",
"wx_msg_validationcode_model",
"wx_component_verify_ticket"};
private static String[] filterSubTables = new String[] {
"wx_mall",
"wx_appinfo",
"wx_authorizer_info",
"wx_c_user",
"wx_c_user_basic_info",
"wx_c_user_car",
"wx_c_user_from_b",
"wx_c_user_tags",
"wx_cuser_floating_layer",
"wx_buser",
"wx_floating_layer",
"wx_user_visit",
"wx_level_config",
"wx_score_rules",
"view_coupon_data",
"view_touch_user",
"wx_weapp_audit_status",
"wx_weapp_code_status",
"wx_weapp_release_status",
"mem_coupon_from_dsp",
"wx_component_verify_ticket"};
@Override
public String getTenantId() {
String tenantId = null;
@@ -44,144 +95,18 @@ public class TenantInfoImpl implements TenantInfo {

@Override
public boolean doTableFilter(String tableName) {
if ("wx_mall".equals(tableName)) {
return true;
}
if ("mall_permission".equals(tableName)) {
return true;
}
if ("mall_sale_type".equals(tableName)) {
return true;
}
if ("mall_user_role".equals(tableName)) {
return true;
}
if ("wx_channel".equals(tableName)) {
return true;
}
if ("wx_business".equals(tableName)) {
return true;
List<String> list = filterTables();
if (list.contains(tableName)) {
return true;
}
if ("wx_coupon_type".equals(tableName)) {
return true;
}
if ("wx_game_template".equals(tableName)) {
return true;
}
if ("wx_group".equals(tableName)) {
return true;
}
if ("wx_mall_apply".equals(tableName)) {
return true;
}
if ("wx_tags".equals(tableName)) {
return true;
}
if ("wx_tags_group".equals(tableName)) {
return true;
}
if ("wx_tags_type".equals(tableName)) {
return true;
}
if ("wx_user_channel".equals(tableName)) {
return true;
}
if ("wx_score_rules".equals(tableName)) {
return true;
}
if ("wx_level_config".equals(tableName)) {
return true;
}
if ("wx_c_user_basic_info".equals(tableName)) {
return true;
}
if ("wx_credit_history".equals(tableName)) {
return true;
}
if ("wx_score_history".equals(tableName)) {
return true;
}
if ("wx_c_user_tags".equals(tableName)) {
return true;
}
if ("wx_weapp_ext_set".equals(tableName)) {
return true;
}
if ("wx_msg_validationcode_model".equals(tableName)) {
return true;
}
if ("wx_component_verify_ticket".equals(tableName)) {
return true;
}
// wx_profit_sharing_receiver
// wx_profit_sharing_result
return false;
}

@Override
public boolean doTableFilterSub(String tableName) {
if ("wx_mall".equals(tableName)) {
return true;
}
if ("wx_appinfo".equals(tableName)) {
return true;
}
if ("wx_authorizer_info".equals(tableName)) {
return true;
}
if ("wx_c_user".equals(tableName)) {
return true;
}
if ("wx_c_user_basic_info".equals(tableName)) {
return true;
}
if ("wx_c_user_car".equals(tableName)) {
return true;
}
if ("wx_c_user_from_b".equals(tableName)) {
return true;
}
if ("wx_c_user_tags".equals(tableName)) {
return true;
}
if ("wx_cuser_floating_layer".equals(tableName)) {
return true;
}
if ("wx_buser".equals(tableName)) {
return true;
}
if ("wx_floating_layer".equals(tableName)) {
return true;
}
if ("wx_user_visit".equals(tableName)) {
return true;
}
if ("wx_level_config".equals(tableName)) {
return true;
}
if ("wx_score_rules".equals(tableName)) {
return true;
}
if ("view_coupon_data".equals(tableName)) {
return true;
}
if ("view_touch_user".equals(tableName)) {
return true;
}
if ("wx_weapp_audit_status".equals(tableName)) {
return true;
}
if ("wx_weapp_code_status".equals(tableName)) {
return true;
}
if ("wx_weapp_release_status".equals(tableName)) {
return true;
}
if ("mem_coupon_from_dsp".equals(tableName)) {
return true;
}
if ("wx_component_verify_ticket".equals(tableName)) {
return true;
List<String> list = filterSubTables();
if (list.contains(tableName)) {
return true;
}
return false;
}
@@ -196,4 +121,14 @@ public class TenantInfoImpl implements TenantInfo {
return true;
return false;
}

@Override
public List<String> filterTables() {
return Arrays.asList(filterTables);
}

@Override
public List<String> filterSubTables() {
return Arrays.asList(filterSubTables);
}
}

+ 73
- 141
mallinkCApi/src/main/java/com/iformall/tenant/TenantInfoImpl.java Просмотреть файл

@@ -9,11 +9,65 @@ import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

public class TenantInfoImpl implements TenantInfo {
private final Logger logger = LoggerFactory.getLogger(this.getClass());

private static String[] filterTables = new String[] {
"wx_mall",
"mall_permission",
"mall_sale_type",
"mall_user_role",
"wx_channel",
"wx_business",
"wx_coupon_type",
"wx_game_template",
"wx_group",
"wx_mall_apply",
"wx_tags",
"wx_tags_group",
"wx_tags_type",
"wx_user_channel",
"wx_score_rules",
"wx_level_config",
"wx_c_user_basic_info",
"wx_credit_history",
"wx_score_history",
"wx_c_user_tags",
"wx_weapp_ext_set",
"wx_msg_validationcode_model",
"wx_component_verify_ticket"};
private static String[] filterSubTables = new String[] {
"wx_mall",
"wx_appinfo",
"wx_authorizer_info",
"wx_c_user",
"wx_c_user_basic_info",
"wx_c_user_car",
"wx_c_user_from_b",
"wx_c_user_tags",
"wx_cuser_floating_layer",
"wx_floating_layer",
"wx_question",
"wx_question_config",
"wx_question_log",
"wx_user_visit",
"wx_level_config",
"wx_score_rules",
"view_coupon_data",
"view_touch_user",
"wx_weapp_audit_status",
"wx_weapp_code_status",
"wx_weapp_release_status",
"mem_coupon_from_dsp",
"wx_component_verify_ticket"};
@Override
public String getTenantId() {
String tenantId = null;
@@ -44,151 +98,19 @@ public class TenantInfoImpl implements TenantInfo {

@Override
public boolean doTableFilter(String tableName) {
if ("wx_mall".equals(tableName)) {
return true;
}
if ("mall_permission".equals(tableName)) {
return true;
}
if ("mall_sale_type".equals(tableName)) {
return true;
}
if ("mall_user_role".equals(tableName)) {
return true;
}
if ("wx_channel".equals(tableName)) {
return true;
}
if ("wx_business".equals(tableName)) {
return true;
}
if ("wx_coupon_type".equals(tableName)) {
return true;
}
if ("wx_game_template".equals(tableName)) {
return true;
}
if ("wx_group".equals(tableName)) {
return true;
}
if ("wx_mall_apply".equals(tableName)) {
return true;
}
if ("wx_tags".equals(tableName)) {
return true;
}
if ("wx_tags_group".equals(tableName)) {
return true;
}
if ("wx_tags_type".equals(tableName)) {
return true;
}
if ("wx_user_channel".equals(tableName)) {
return true;
}
if ("wx_score_rules".equals(tableName)) {
return true;
}
if ("wx_level_config".equals(tableName)) {
return true;
}
if ("wx_c_user_basic_info".equals(tableName)) {
return true;
}
if ("wx_credit_history".equals(tableName)) {
return true;
}
if ("wx_score_history".equals(tableName)) {
return true;
}
if ("wx_c_user_tags".equals(tableName)) {
return true;
}
if ("wx_weapp_ext_set".equals(tableName)) {
return true;
}
if ("wx_msg_validationcode_model".equals(tableName)) {
return true;
}
if ("wx_component_verify_ticket".equals(tableName)) {
return true;
}
// wx_profit_sharing_receiver
// wx_profit_sharing_result
List<String> list = filterTables();
if (list.contains(tableName)) {
return true;
}
return false;
}

@Override
public boolean doTableFilterSub(String tableName) {
if ("wx_mall".equals(tableName)) {
return true;
}
if ("wx_appinfo".equals(tableName)) {
return true;
}
if ("wx_authorizer_info".equals(tableName)) {
return true;
}
if ("wx_c_user".equals(tableName)) {
return true;
}
if ("wx_c_user_basic_info".equals(tableName)) {
return true;
}
if ("wx_c_user_car".equals(tableName)) {
return true;
}
if ("wx_c_user_from_b".equals(tableName)) {
return true;
}
if ("wx_c_user_tags".equals(tableName)) {
return true;
}
if ("wx_cuser_floating_layer".equals(tableName)) {
return true;
}
if ("wx_floating_layer".equals(tableName)) {
return true;
}
if ("wx_question".equals(tableName)) {
return true;
}
if ("wx_question_config".equals(tableName)) {
return true;
}
if ("wx_question_log".equals(tableName)) {
return true;
}
if ("wx_user_visit".equals(tableName)) {
return true;
}
if ("wx_level_config".equals(tableName)) {
return true;
}
if ("wx_score_rules".equals(tableName)) {
return true;
}
if ("view_coupon_data".equals(tableName)) {
return true;
}
if ("view_touch_user".equals(tableName)) {
return true;
}
if ("wx_weapp_audit_status".equals(tableName)) {
return true;
}
if ("wx_weapp_code_status".equals(tableName)) {
return true;
}
if ("wx_weapp_release_status".equals(tableName)) {
return true;
}
if ("mem_coupon_from_dsp".equals(tableName)) {
return true;
}
if ("wx_component_verify_ticket".equals(tableName)) {
return true;
}
List<String> list = filterSubTables();
if (list.contains(tableName)) {
return true;
}
return false;
}

@@ -200,4 +122,14 @@ public class TenantInfoImpl implements TenantInfo {
return true;
return false;
}

@Override
public List<String> filterTables() {
return Arrays.asList(filterTables);
}

@Override
public List<String> filterSubTables() {
return Arrays.asList(filterSubTables);
}
}

+ 8
- 4
mybatis-multi-tenancy/src/main/java/com/iformall/plugin/MyBatisItercepters.java Просмотреть файл

@@ -3,7 +3,11 @@ package com.iformall.plugin;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;

import com.iformall.plugin.entity.ChainValue;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -31,17 +35,17 @@ public class MyBatisItercepters implements Interceptor {
public Object intercept(Invocation invocation) throws Throwable {
if (null != plugins && plugins.size() > 0 ) {
// 能从cookie或者session获取到tenantId
Map<String,Object> chainMap = new HashMap<String,Object>();
Map<String,ChainValue> chainValues = new HashMap<String,ChainValue>();
for (int i = 0 ; i < plugins.size() ; i++) {
MyBatisPlus plug = plugins.get(i);
invokeIntercept(plug, invocation, chainMap);
invokeIntercept(plug, invocation, chainValues);
}
}
return invocation.proceed();
}
private void invokeIntercept(MyBatisPlus plug,Invocation invocation,Map<String,Object> chainMap) throws Throwable {
Map<String,Object> resultMap = plug.intercept(invocation, chainMap);
private void invokeIntercept(MyBatisPlus plug,Invocation invocation,Map<String,ChainValue> chainMap) throws Throwable {
Map<String,ChainValue> resultMap = plug.intercept(invocation, chainMap);
if (null != resultMap) {
chainMap.putAll(resultMap);
}


+ 4
- 1
mybatis-multi-tenancy/src/main/java/com/iformall/plugin/MyBatisPlus.java Просмотреть файл

@@ -1,9 +1,12 @@
package com.iformall.plugin;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.plugin.Invocation;

import com.iformall.plugin.entity.ChainValue;

public interface MyBatisPlus {

/**
@@ -11,5 +14,5 @@ public interface MyBatisPlus {
* @return Map<coloumn,value>
* @throws Throwable
*/
public Map<String,Object> intercept(Invocation invocation,Map<String,Object> parentChainMap) throws Throwable;
public Map<String,ChainValue> intercept(Invocation invocation,Map<String,ChainValue> chainMap) throws Throwable;
}

+ 32
- 8
mybatis-multi-tenancy/src/main/java/com/iformall/plugin/autoTenantId/MultiTenancy.java Просмотреть файл

@@ -33,8 +33,10 @@ import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.ResultHandler;

import com.iformall.plugin.MyBatisPlus;
import com.iformall.plugin.entity.ChainValue;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
@@ -90,14 +92,15 @@ public class MultiTenancy implements MyBatisPlus {
}

@Override
public Map<String,Object> intercept(Invocation invocation,Map<String,Object> parentChainMap) throws Throwable {
public Map<String,ChainValue> intercept(Invocation invocation,Map<String,ChainValue> chainMap) throws Throwable {
if (getTenantInfo().getTenantId() != null || getTenantInfo().getParentTenantId() != null) {
// 能从cookie或者session获取到tenantId
mod(invocation);
Map<String,Object> resultMap = new HashMap<String,Object>();
resultMap.put(tenantIdColumn, getTenantInfo().getTenantId());
resultMap.put(parentTenantIdColumn, getTenantInfo().getParentTenantId());
return resultMap;
//Map<String,Object> resultMap = new HashMap<String,Object>();
//resultMap.put(tenantIdColumn, getTenantInfo().getTenantId());
//resultMap.put(parentTenantIdColumn, getTenantInfo().getParentTenantId());
return mod(invocation);
}
return null;
}
@@ -107,14 +110,15 @@ public class MultiTenancy implements MyBatisPlus {
* @param invocation
* @throws Throwable
*/
public void mod(Invocation invocation) throws Throwable {
public Map<String,ChainValue> mod(Invocation invocation) throws Throwable {
StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
MetaObject metaStatementHandler = SystemMetaObject.forObject(statementHandler);
Configuration configuration = (Configuration) metaStatementHandler.getValue("delegate.configuration");

MappedStatement ms = (MappedStatement) metaStatementHandler.getValue("delegate.mappedStatement");
if (getTenantInfo().doMappedStatementFIlter(ms))
return;
return null;
SqlCommandType type = ms.getSqlCommandType();

BoundSql boundSql = statementHandler.getBoundSql();
@@ -124,6 +128,26 @@ public class MultiTenancy implements MyBatisPlus {
metaStatementHandler.setValue("delegate.boundSql.sql", newSQL);
}

Map<String,ChainValue> newchainMap = new HashMap<String,ChainValue>();
ChainValue tenantIdChain = new ChainValue();
tenantIdChain.setColumn(tenantIdColumn);
tenantIdChain.setValue(getTenantInfo().getTenantId());
tenantIdChain.setIgnoreTables(getTenantInfo().filterTables());
newchainMap.put(tenantIdColumn,tenantIdChain);
ChainValue parentTenantIdChain = new ChainValue();
parentTenantIdChain.setColumn(parentTenantIdColumn);
parentTenantIdChain.setValue(getTenantInfo().getParentTenantId());
parentTenantIdChain.setIgnoreTables(getTenantInfo().filterSubTables());
newchainMap.put(parentTenantIdColumn,parentTenantIdChain);
return newchainMap;
}

/**


+ 6
- 0
mybatis-multi-tenancy/src/main/java/com/iformall/plugin/autoTenantId/TenantInfo.java Просмотреть файл

@@ -1,5 +1,7 @@
package com.iformall.plugin.autoTenantId;

import java.util.List;

import org.apache.ibatis.mapping.MappedStatement;

/**
@@ -13,8 +15,12 @@ public interface TenantInfo {
String getParentTenantId();

boolean doTableFilter(String tableName);
List<String> filterTables();

boolean doTableFilterSub(String tableName);
List<String> filterSubTables();

boolean doMappedStatementFIlter(MappedStatement ms);
}

+ 41
- 0
mybatis-multi-tenancy/src/main/java/com/iformall/plugin/entity/ChainValue.java Просмотреть файл

@@ -0,0 +1,41 @@
package com.iformall.plugin.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* 链里的值
* @author alascor
* {column:"tennat_id", value:"789", ignoreTables:"wx_c_user","wx_c_user_basic_info"...}
*
*/

public class ChainValue implements Serializable{
private static final long serialVersionUID = 6290377774649358950L;
private String column;
private Object value;
private List<String> ignoreTables;
public String getColumn() {
return column;
}
public void setColumn(String column) {
this.column = column;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
public List<String> getIgnoreTables() {
return ignoreTables;
}
public void setIgnoreTables(List<String> ignoreTables) {
this.ignoreTables = ignoreTables;
}
}

+ 30
- 0
mybatis-multi-tenancy/src/main/java/com/iformall/plugin/entity/ChainValueHelper.java Просмотреть файл

@@ -0,0 +1,30 @@
package com.iformall.plugin.entity;

import java.util.Map;

public class ChainValueHelper {

public static Object getValue(String table,String column,Map<String,ChainValue> parentChainMap) {
if (null != parentChainMap && parentChainMap.containsKey(column)) {
ChainValue chainvalue = parentChainMap.get(column);
if (null != chainvalue) {
if (null != chainvalue.getIgnoreTables()) {
if (!chainvalue.getIgnoreTables().contains(table)) {
Object o = chainvalue.getValue();
if (null != o) {
return o;
}
}
}else {
Object o = chainvalue.getValue();
if (null != o) {
return o;
}
}
}
}
return null;
}
}

+ 36
- 36
mybatis-multi-tenancy/src/main/java/com/iformall/plugin/shard/plugin/ShardingSpherePlugin.java Просмотреть файл

@@ -20,6 +20,8 @@ import org.apache.ibatis.reflection.SystemMetaObject;

import com.alibaba.druid.wall.violation.ErrorCode;
import com.iformall.plugin.MyBatisPlus;
import com.iformall.plugin.entity.ChainValue;
import com.iformall.plugin.entity.ChainValueHelper;
import com.iformall.plugin.shard.annotation.IgnoreSharding;
import com.iformall.plugin.shard.impl.ShardingRuleExcuter;
import com.iformall.plugin.shard.impl.ShardingSphere;
@@ -76,9 +78,9 @@ public class ShardingSpherePlugin implements MyBatisPlus {
}
}
public Map<String,Object> intercept(Invocation invocation,Map<String,Object> parentChainMap) throws Throwable {
public Map<String,ChainValue> intercept(Invocation invocation,Map<String,ChainValue> chainMap) throws Throwable {
if (!ShardingTableExcuter.hasShardingConfig()) {
return parentChainMap;
return null;
}
StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
MetaObject metaStatementHandler = SystemMetaObject.forObject(statementHandler);
@@ -94,31 +96,31 @@ public class ShardingSpherePlugin implements MyBatisPlus {
// if (null == ignore) {
Object parameterObject = metaStatementHandler.getValue("delegate.boundSql.parameterObject");
BoundSql boundSql = statementHandler.getBoundSql();
String newSQL = buildTable(boundSql,parameterObject,parentChainMap);
String newSQL = buildTable(boundSql,parameterObject,chainMap);
metaStatementHandler.setValue("delegate.boundSql.sql", newSQL);
// }
//}
//没有添加字段,只是根据字段分表,没有别的需要返回
return parentChainMap;
return chainMap;
}

private String buildTable(BoundSql boundSql,Object parameterObject,Map<String,Object> parentChainMap) throws JSQLParserException {
private String buildTable(BoundSql boundSql,Object parameterObject,Map<String,ChainValue> chainMap) throws JSQLParserException {
String oldSql = boundSql.getSql();
Statement stmt = CCJSqlParserUtil.parse(oldSql);
if (stmt instanceof Insert) {
return buildInsert((Insert) stmt,oldSql,parameterObject,parentChainMap);
return buildInsert((Insert) stmt,oldSql,parameterObject,chainMap);
}

if (stmt instanceof Delete) {
return buildDelete((Delete) stmt,oldSql,parameterObject,parentChainMap);
return buildDelete((Delete) stmt,oldSql,parameterObject,chainMap);
}

if (stmt instanceof Update) {
return buildUpdate((Update) stmt,oldSql,parameterObject,parentChainMap);
return buildUpdate((Update) stmt,oldSql,parameterObject,chainMap);
}

if (stmt instanceof Select) {
return buildSelect((Select) stmt,oldSql,parameterObject,parentChainMap);
return buildSelect((Select) stmt,oldSql,parameterObject,chainMap);
}

if (stmt instanceof CreateTable) {
@@ -127,62 +129,60 @@ public class ShardingSpherePlugin implements MyBatisPlus {
throw new RuntimeException("非法sql语句,请检查"+oldSql);
}
private String buildInsert(Insert stmt,String oldSql,Object parameterObject,Map<String,Object> parentChainMap) {
return buildTable(stmt.getTable(), oldSql, parameterObject,parentChainMap);
private String buildInsert(Insert stmt,String oldSql,Object parameterObject,Map<String,ChainValue> chainMap) {
return buildTable(stmt.getTable(), oldSql, parameterObject,chainMap);
}
private String buildDelete(Delete stmt,String oldSql,Object parameterObject,Map<String,Object> parentChainMap) {
return buildTable(stmt.getTable(), oldSql, parameterObject,parentChainMap);
private String buildDelete(Delete stmt,String oldSql,Object parameterObject,Map<String,ChainValue> chainMap) {
return buildTable(stmt.getTable(), oldSql, parameterObject,chainMap);
}
private String buildUpdate(Update stmt,String oldSql,Object parameterObject,Map<String,Object> parentChainMap) {
return buildListTables(stmt.getTables(), oldSql, parameterObject,parentChainMap);
private String buildUpdate(Update stmt,String oldSql,Object parameterObject,Map<String,ChainValue> chainMap) {
return buildListTables(stmt.getTables(), oldSql, parameterObject,chainMap);
}
private String buildSelect(Select stmt,String oldSql,Object parameterObject,Map<String,Object> parentChainMap) {
private String buildSelect(Select stmt,String oldSql,Object parameterObject,Map<String,ChainValue> chainMap) {
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tableList = tablesNamesFinder.getTableList(stmt);
return buildByListTableNames(tableList, oldSql, parameterObject,parentChainMap);
return buildByListTableNames(tableList, oldSql, parameterObject,chainMap);
}
private String buildTable(Table table,String oldSql,Object parameterObject,Map<String,Object> parentChainMap) {
return buildByTableName(table.getName(), oldSql, parameterObject,parentChainMap);
private String buildTable(Table table,String oldSql,Object parameterObject,Map<String,ChainValue> chainMap) {
return buildByTableName(table.getName(), oldSql, parameterObject,chainMap);
}
private String buildByTableName(String table,String oldSql,Object parameterObject,Map<String,Object> parentChainMap) {
private String buildByTableName(String table,String oldSql,Object parameterObject,Map<String,ChainValue> chainMap) {
if (!ShardingTableExcuter.isShardingTable(table)) {
return oldSql;
}
String newtable = getShareTable(table,parameterObject,parentChainMap);
String newtable = getShareTable(table,parameterObject,chainMap);
//此处处理前缀相似的表,如wx_c_user, wx_c_user_basic_info, 直接替换wx_c_user会有问题
return oldSql.replaceAll(table+"_", "########").replaceAll(table, newtable).replaceAll("########", table+"_");
}
private String buildListTables(List<Table> tables,String oldSql,Object parameterObject,Map<String,Object> parentChainMap) {
private String buildListTables(List<Table> tables,String oldSql,Object parameterObject,Map<String,ChainValue> chainMap) {
for (Table t : tables) {
oldSql = buildTable(t,oldSql,parameterObject,parentChainMap);
oldSql = buildTable(t,oldSql,parameterObject,chainMap);
}
return oldSql;
}
private String buildByListTableNames(List<String> tables,String oldSql,Object parameterObject,Map<String,Object> parentChainMap) {
private String buildByListTableNames(List<String> tables,String oldSql,Object parameterObject,Map<String,ChainValue> chainMap) {
for (String t : tables) {
oldSql = buildByTableName(t,oldSql,parameterObject,parentChainMap);
oldSql = buildByTableName(t,oldSql,parameterObject,chainMap);
}
return oldSql;
}
private String getShareTable(String table,Object parameterObject,Map<String,Object> parentChainMap) {
private String getShareTable(String table,Object parameterObject,Map<String,ChainValue> chainMap) {
ShardingSphere ss = ShardingTableExcuter.getShardingSphere(table);
return ss.getTableName()+ShardingSphere.TABLE_SUFFIX+ShardingRuleExcuter.getTableIndex(ss.getRule(), getValue(ss.getColumn(),parameterObject,parentChainMap), ss.getCount());
return ss.getTableName()+ShardingSphere.TABLE_SUFFIX+ShardingRuleExcuter.getTableIndex(ss.getRule(), getValue(table,ss.getColumn(),parameterObject,chainMap), ss.getCount());
}
private Object getValue(String column,Object parameterObject,Map<String,Object> parentChainMap) {
if (null != parentChainMap && parentChainMap.containsKey(column)) {
Object o = parentChainMap.get(column);
if (null != o) {
return o;
}
private Object getValue(String table,String column,Object parameterObject,Map<String,ChainValue> parentChainMap) {
Object ov = ChainValueHelper.getValue(table, column, parentChainMap);
if (null != ov ) {
return ov;
}
if (parameterObject instanceof Map) {
@@ -217,13 +217,13 @@ public class ShardingSpherePlugin implements MyBatisPlus {
continue;
}
}
Object v = getValue(column,value,parentChainMap);
Object v = getValue(table,column,value,parentChainMap);
if (null != v) {
return v;
}
}
}
return getValue(column,o,parentChainMap);
return getValue(table,column,o,parentChainMap);
}else if (isBasicTypeValue(parameterObject)) {
return parameterObject;
//认为是实体
@@ -266,7 +266,7 @@ public class ShardingSpherePlugin implements MyBatisPlus {
return false;
}
private Object getEntityValue(String column,Object parameterObject,Map<String,Object> parentChainMap) {
private Object getEntityValue(String column,Object parameterObject,Map<String,ChainValue> chainMap) {
try {
Method method = getMethodFromAllParent(parameterObject.getClass(),"get"+getField(column));
Object o = method.invoke(parameterObject);


+ 12
- 0
mybatis-multi-tenancy/src/test/java/com/iformall/plugin/TenantInfoImpl.java Просмотреть файл

@@ -1,5 +1,7 @@
package com.iformall.plugin;

import java.util.List;

import org.apache.ibatis.mapping.MappedStatement;

import com.iformall.plugin.autoTenantId.TenantInfo;
@@ -34,4 +36,14 @@ public class TenantInfoImpl implements TenantInfo {
public boolean doMappedStatementFIlter(MappedStatement ms) {
return false;
}

@Override
public List<String> filterTables() {
return null;
}

@Override
public List<String> filterSubTables() {
return null;
}
}

Загрузка…
Отмена
Сохранить