From 49f71416eff6853e6c419a279da27634035126ed Mon Sep 17 00:00:00 2001 From: xiaohanzi Date: Wed, 4 Nov 2020 10:53:08 +0800 Subject: [PATCH] fix bug --- .../com/iformall/tenant/TenantInfoImpl.java | 264 ++++++------------ .../com/iformall/tenant/TenantInfoImpl.java | 199 +++++-------- .../com/iformall/tenant/TenantInfoImpl.java | 214 +++++--------- .../iformall/plugin/MyBatisItercepters.java | 12 +- .../java/com/iformall/plugin/MyBatisPlus.java | 5 +- .../plugin/autoTenantId/MultiTenancy.java | 40 ++- .../plugin/autoTenantId/TenantInfo.java | 6 + .../iformall/plugin/entity/ChainValue.java | 41 +++ .../plugin/entity/ChainValueHelper.java | 30 ++ .../shard/plugin/ShardingSpherePlugin.java | 72 ++--- .../com/iformall/plugin/TenantInfoImpl.java | 12 + 11 files changed, 387 insertions(+), 508 deletions(-) create mode 100644 mybatis-multi-tenancy/src/main/java/com/iformall/plugin/entity/ChainValue.java create mode 100644 mybatis-multi-tenancy/src/main/java/com/iformall/plugin/entity/ChainValueHelper.java diff --git a/mallinkAdmin/src/main/java/com/iformall/tenant/TenantInfoImpl.java b/mallinkAdmin/src/main/java/com/iformall/tenant/TenantInfoImpl.java index 211dfc65d..e583c78f5 100644 --- a/mallinkAdmin/src/main/java/com/iformall/tenant/TenantInfoImpl.java +++ b/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 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 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 filterTables() { + return Arrays.asList(filterTables); + } + + @Override + public List filterSubTables() { + return Arrays.asList(filterSubTables); + } } diff --git a/mallinkBApi/src/main/java/com/iformall/tenant/TenantInfoImpl.java b/mallinkBApi/src/main/java/com/iformall/tenant/TenantInfoImpl.java index 2208fd273..b8e3bc282 100644 --- a/mallinkBApi/src/main/java/com/iformall/tenant/TenantInfoImpl.java +++ b/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 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 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 filterTables() { + return Arrays.asList(filterTables); + } + + @Override + public List filterSubTables() { + return Arrays.asList(filterSubTables); + } } diff --git a/mallinkCApi/src/main/java/com/iformall/tenant/TenantInfoImpl.java b/mallinkCApi/src/main/java/com/iformall/tenant/TenantInfoImpl.java index 726ad74c1..503f8d457 100644 --- a/mallinkCApi/src/main/java/com/iformall/tenant/TenantInfoImpl.java +++ b/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 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 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 filterTables() { + return Arrays.asList(filterTables); + } + + @Override + public List filterSubTables() { + return Arrays.asList(filterSubTables); + } } diff --git a/mybatis-multi-tenancy/src/main/java/com/iformall/plugin/MyBatisItercepters.java b/mybatis-multi-tenancy/src/main/java/com/iformall/plugin/MyBatisItercepters.java index 3f0daeee2..9fdf28d0d 100644 --- a/mybatis-multi-tenancy/src/main/java/com/iformall/plugin/MyBatisItercepters.java +++ b/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 chainMap = new HashMap(); + Map chainValues = new HashMap(); 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 chainMap) throws Throwable { - Map resultMap = plug.intercept(invocation, chainMap); + private void invokeIntercept(MyBatisPlus plug,Invocation invocation,Map chainMap) throws Throwable { + Map resultMap = plug.intercept(invocation, chainMap); if (null != resultMap) { chainMap.putAll(resultMap); } diff --git a/mybatis-multi-tenancy/src/main/java/com/iformall/plugin/MyBatisPlus.java b/mybatis-multi-tenancy/src/main/java/com/iformall/plugin/MyBatisPlus.java index d3056d9d4..28e61dbb0 100644 --- a/mybatis-multi-tenancy/src/main/java/com/iformall/plugin/MyBatisPlus.java +++ b/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 * @throws Throwable */ - public Map intercept(Invocation invocation,Map parentChainMap) throws Throwable; + public Map intercept(Invocation invocation,Map chainMap) throws Throwable; } diff --git a/mybatis-multi-tenancy/src/main/java/com/iformall/plugin/autoTenantId/MultiTenancy.java b/mybatis-multi-tenancy/src/main/java/com/iformall/plugin/autoTenantId/MultiTenancy.java index b48ef9364..bdeaf3b96 100644 --- a/mybatis-multi-tenancy/src/main/java/com/iformall/plugin/autoTenantId/MultiTenancy.java +++ b/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 intercept(Invocation invocation,Map parentChainMap) throws Throwable { + public Map intercept(Invocation invocation,Map chainMap) throws Throwable { if (getTenantInfo().getTenantId() != null || getTenantInfo().getParentTenantId() != null) { // 能从cookie或者session获取到tenantId - mod(invocation); - Map resultMap = new HashMap(); - resultMap.put(tenantIdColumn, getTenantInfo().getTenantId()); - resultMap.put(parentTenantIdColumn, getTenantInfo().getParentTenantId()); - return resultMap; + + //Map resultMap = new HashMap(); + //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 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 newchainMap = new HashMap(); + + 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; + } /** diff --git a/mybatis-multi-tenancy/src/main/java/com/iformall/plugin/autoTenantId/TenantInfo.java b/mybatis-multi-tenancy/src/main/java/com/iformall/plugin/autoTenantId/TenantInfo.java index 53fd03fff..7d0b7911b 100644 --- a/mybatis-multi-tenancy/src/main/java/com/iformall/plugin/autoTenantId/TenantInfo.java +++ b/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 filterTables(); boolean doTableFilterSub(String tableName); + + List filterSubTables(); boolean doMappedStatementFIlter(MappedStatement ms); } diff --git a/mybatis-multi-tenancy/src/main/java/com/iformall/plugin/entity/ChainValue.java b/mybatis-multi-tenancy/src/main/java/com/iformall/plugin/entity/ChainValue.java new file mode 100644 index 000000000..1acf26069 --- /dev/null +++ b/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 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 getIgnoreTables() { + return ignoreTables; + } + public void setIgnoreTables(List ignoreTables) { + this.ignoreTables = ignoreTables; + } +} diff --git a/mybatis-multi-tenancy/src/main/java/com/iformall/plugin/entity/ChainValueHelper.java b/mybatis-multi-tenancy/src/main/java/com/iformall/plugin/entity/ChainValueHelper.java new file mode 100644 index 000000000..a04eb8f92 --- /dev/null +++ b/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 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; + } +} diff --git a/mybatis-multi-tenancy/src/main/java/com/iformall/plugin/shard/plugin/ShardingSpherePlugin.java b/mybatis-multi-tenancy/src/main/java/com/iformall/plugin/shard/plugin/ShardingSpherePlugin.java index 085a37666..574d6cd2c 100644 --- a/mybatis-multi-tenancy/src/main/java/com/iformall/plugin/shard/plugin/ShardingSpherePlugin.java +++ b/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 intercept(Invocation invocation,Map parentChainMap) throws Throwable { + public Map intercept(Invocation invocation,Map 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 parentChainMap) throws JSQLParserException { + private String buildTable(BoundSql boundSql,Object parameterObject,Map 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 parentChainMap) { - return buildTable(stmt.getTable(), oldSql, parameterObject,parentChainMap); + private String buildInsert(Insert stmt,String oldSql,Object parameterObject,Map chainMap) { + return buildTable(stmt.getTable(), oldSql, parameterObject,chainMap); } - private String buildDelete(Delete stmt,String oldSql,Object parameterObject,Map parentChainMap) { - return buildTable(stmt.getTable(), oldSql, parameterObject,parentChainMap); + private String buildDelete(Delete stmt,String oldSql,Object parameterObject,Map chainMap) { + return buildTable(stmt.getTable(), oldSql, parameterObject,chainMap); } - private String buildUpdate(Update stmt,String oldSql,Object parameterObject,Map parentChainMap) { - return buildListTables(stmt.getTables(), oldSql, parameterObject,parentChainMap); + private String buildUpdate(Update stmt,String oldSql,Object parameterObject,Map chainMap) { + return buildListTables(stmt.getTables(), oldSql, parameterObject,chainMap); } - private String buildSelect(Select stmt,String oldSql,Object parameterObject,Map parentChainMap) { + private String buildSelect(Select stmt,String oldSql,Object parameterObject,Map chainMap) { TablesNamesFinder tablesNamesFinder = new TablesNamesFinder(); List 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 parentChainMap) { - return buildByTableName(table.getName(), oldSql, parameterObject,parentChainMap); + private String buildTable(Table table,String oldSql,Object parameterObject,Map chainMap) { + return buildByTableName(table.getName(), oldSql, parameterObject,chainMap); } - private String buildByTableName(String table,String oldSql,Object parameterObject,Map parentChainMap) { + private String buildByTableName(String table,String oldSql,Object parameterObject,Map 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 tables,String oldSql,Object parameterObject,Map parentChainMap) { + private String buildListTables(List
tables,String oldSql,Object parameterObject,Map chainMap) { for (Table t : tables) { - oldSql = buildTable(t,oldSql,parameterObject,parentChainMap); + oldSql = buildTable(t,oldSql,parameterObject,chainMap); } return oldSql; } - private String buildByListTableNames(List tables,String oldSql,Object parameterObject,Map parentChainMap) { + private String buildByListTableNames(List tables,String oldSql,Object parameterObject,Map 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 parentChainMap) { + private String getShareTable(String table,Object parameterObject,Map 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 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 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 parentChainMap) { + private Object getEntityValue(String column,Object parameterObject,Map chainMap) { try { Method method = getMethodFromAllParent(parameterObject.getClass(),"get"+getField(column)); Object o = method.invoke(parameterObject); diff --git a/mybatis-multi-tenancy/src/test/java/com/iformall/plugin/TenantInfoImpl.java b/mybatis-multi-tenancy/src/test/java/com/iformall/plugin/TenantInfoImpl.java index 9e920fff0..50d76a1e7 100644 --- a/mybatis-multi-tenancy/src/test/java/com/iformall/plugin/TenantInfoImpl.java +++ b/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 filterTables() { + return null; + } + + @Override + public List filterSubTables() { + return null; + } }