| @@ -23,11 +23,6 @@ | |||
| <artifactId>mallinkService</artifactId> | |||
| <version>1.0</version> | |||
| </dependency> | |||
| <dependency> | |||
| <groupId>com.iformall</groupId> | |||
| <artifactId>mybatis-multi-tenancy</artifactId> | |||
| <version>1.0</version> | |||
| </dependency> | |||
| <dependency> | |||
| <groupId>commons-fileupload</groupId> | |||
| <artifactId>commons-fileupload</artifactId> | |||
| @@ -3,15 +3,15 @@ package com.iformall.config; | |||
| import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor; | |||
| import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor; | |||
| import com.iformall.plugin.MultiTenancy; | |||
| import com.iformall.plugin.shard.plugin.ShardingSpherePlugin; | |||
| import org.springframework.context.annotation.Bean; | |||
| import org.springframework.context.annotation.Configuration; | |||
| import org.springframework.transaction.annotation.EnableTransactionManagement; | |||
| import java.util.Properties; | |||
| @EnableTransactionManagement | |||
| @Configuration | |||
| public class MyBatisConfiguration { | |||
| public class MyBatisConfiguration extends BaseMyBatisConfiguration{ | |||
| @Bean | |||
| @@ -25,6 +25,11 @@ public class MyBatisConfiguration { | |||
| multiTenancy.setProperties(properties); | |||
| return multiTenancy; | |||
| } | |||
| @Bean | |||
| public ShardingSpherePlugin shardingSpherePlugin() { | |||
| return baseShardingSpherePlugin(); | |||
| } | |||
| @Bean | |||
| public PaginationInterceptor paginationInterceptor() { | |||
| @@ -17,11 +17,6 @@ | |||
| <artifactId>mallinkService</artifactId> | |||
| <version>1.0</version> | |||
| </dependency> | |||
| <dependency> | |||
| <groupId>com.iformall</groupId> | |||
| <artifactId>mybatis-multi-tenancy</artifactId> | |||
| <version>1.0</version> | |||
| </dependency> | |||
| </dependencies> | |||
| <build> | |||
| <plugins> | |||
| @@ -1,13 +1,19 @@ | |||
| package com.iformall.config; | |||
| import com.iformall.plugin.MultiTenancy; | |||
| import com.iformall.plugin.shard.impl.EnumShardingRule; | |||
| import com.iformall.plugin.shard.impl.ShardingSphere; | |||
| import com.iformall.plugin.shard.plugin.ShardingSpherePlugin; | |||
| import org.springframework.context.annotation.Bean; | |||
| import org.springframework.context.annotation.Configuration; | |||
| import java.util.ArrayList; | |||
| import java.util.List; | |||
| import java.util.Properties; | |||
| @Configuration | |||
| public class MyBatisConfiguration { | |||
| public class MyBatisConfiguration extends BaseMyBatisConfiguration{ | |||
| @Bean | |||
| public MultiTenancy multiTenancy() { | |||
| @@ -20,4 +26,9 @@ public class MyBatisConfiguration { | |||
| multiTenancy.setProperties(properties); | |||
| return multiTenancy; | |||
| } | |||
| @Bean | |||
| public ShardingSpherePlugin shardingSpherePlugin() { | |||
| return baseShardingSpherePlugin(); | |||
| } | |||
| } | |||
| @@ -17,11 +17,6 @@ | |||
| <artifactId>mallinkService</artifactId> | |||
| <version>1.0</version> | |||
| </dependency> | |||
| <dependency> | |||
| <groupId>com.iformall</groupId> | |||
| <artifactId>mybatis-multi-tenancy</artifactId> | |||
| <version>1.0</version> | |||
| </dependency> | |||
| </dependencies> | |||
| <build> | |||
| <plugins> | |||
| @@ -1,13 +1,19 @@ | |||
| package com.iformall.config; | |||
| import com.iformall.plugin.MultiTenancy; | |||
| import com.iformall.plugin.shard.impl.EnumShardingRule; | |||
| import com.iformall.plugin.shard.impl.ShardingSphere; | |||
| import com.iformall.plugin.shard.plugin.ShardingSpherePlugin; | |||
| import org.springframework.context.annotation.Bean; | |||
| import org.springframework.context.annotation.Configuration; | |||
| import java.util.ArrayList; | |||
| import java.util.List; | |||
| import java.util.Properties; | |||
| @Configuration | |||
| public class MyBatisConfiguration { | |||
| public class MyBatisConfiguration extends BaseMyBatisConfiguration { | |||
| @Bean | |||
| public MultiTenancy multiTenancy() { | |||
| @@ -20,4 +26,9 @@ public class MyBatisConfiguration { | |||
| multiTenancy.setProperties(properties); | |||
| return multiTenancy; | |||
| } | |||
| @Bean | |||
| public ShardingSpherePlugin shardingSpherePlugin() { | |||
| return baseShardingSpherePlugin(); | |||
| } | |||
| } | |||
| @@ -23,7 +23,6 @@ | |||
| <artifactId>mallinkService</artifactId> | |||
| <version>1.0</version> | |||
| </dependency> | |||
| </dependencies> | |||
| <build> | |||
| @@ -0,0 +1,16 @@ | |||
| package com.iformall.config; | |||
| import com.iformall.plugin.shard.plugin.ShardingSpherePlugin; | |||
| import org.springframework.context.annotation.Bean; | |||
| import org.springframework.context.annotation.Configuration; | |||
| import org.springframework.transaction.annotation.EnableTransactionManagement; | |||
| @EnableTransactionManagement | |||
| @Configuration | |||
| public class MyBatisConfiguration extends BaseMyBatisConfiguration{ | |||
| @Bean | |||
| public ShardingSpherePlugin shardingSpherePlugin() { | |||
| return baseShardingSpherePlugin(); | |||
| } | |||
| } | |||
| @@ -0,0 +1,21 @@ | |||
| package com.iformall.config; | |||
| import com.iformall.plugin.shard.impl.EnumShardingRule; | |||
| import com.iformall.plugin.shard.impl.ShardingSphere; | |||
| import com.iformall.plugin.shard.plugin.ShardingSpherePlugin; | |||
| import org.springframework.context.annotation.Bean; | |||
| import org.springframework.context.annotation.Configuration; | |||
| import org.springframework.transaction.annotation.EnableTransactionManagement; | |||
| import java.util.ArrayList; | |||
| import java.util.List; | |||
| import java.util.Properties; | |||
| @EnableTransactionManagement | |||
| @Configuration | |||
| public class MyBatisConfiguration extends BaseMyBatisConfiguration { | |||
| @Bean | |||
| public ShardingSpherePlugin shardingSpherePlugin() { | |||
| return baseShardingSpherePlugin(); | |||
| } | |||
| } | |||
| @@ -21,7 +21,7 @@ | |||
| <groupId>org.springframework.boot</groupId> | |||
| <artifactId>spring-boot-starter-test</artifactId> | |||
| <scope>test</scope> | |||
| </dependency> | |||
| </dependency> | |||
| </dependencies> | |||
| <build> | |||
| <plugins> | |||
| @@ -0,0 +1,21 @@ | |||
| package com.iformall.config; | |||
| import com.iformall.plugin.shard.impl.EnumShardingRule; | |||
| import com.iformall.plugin.shard.impl.ShardingSphere; | |||
| import com.iformall.plugin.shard.plugin.ShardingSpherePlugin; | |||
| import org.springframework.context.annotation.Bean; | |||
| import org.springframework.context.annotation.Configuration; | |||
| import org.springframework.transaction.annotation.EnableTransactionManagement; | |||
| import java.util.ArrayList; | |||
| import java.util.List; | |||
| import java.util.Properties; | |||
| @EnableTransactionManagement | |||
| @Configuration | |||
| public class MyBatisConfiguration extends BaseMyBatisConfiguration { | |||
| @Bean | |||
| public ShardingSpherePlugin shardingSpherePlugin() { | |||
| return baseShardingSpherePlugin(); | |||
| } | |||
| } | |||
| @@ -17,7 +17,7 @@ | |||
| <groupId>com.iformall</groupId> | |||
| <artifactId>mallinkService</artifactId> | |||
| <version>1.0</version> | |||
| </dependency> | |||
| </dependency> | |||
| </dependencies> | |||
| <build> | |||
| @@ -0,0 +1,16 @@ | |||
| package com.iformall.config; | |||
| import com.iformall.plugin.shard.plugin.ShardingSpherePlugin; | |||
| import org.springframework.context.annotation.Bean; | |||
| import org.springframework.context.annotation.Configuration; | |||
| import org.springframework.transaction.annotation.EnableTransactionManagement; | |||
| @EnableTransactionManagement | |||
| @Configuration | |||
| public class MyBatisConfiguration extends BaseMyBatisConfiguration { | |||
| @Bean | |||
| public ShardingSpherePlugin shardingSpherePlugin() { | |||
| return baseShardingSpherePlugin(); | |||
| } | |||
| } | |||
| @@ -30,7 +30,11 @@ | |||
| <artifactId>ons-client</artifactId> | |||
| <version>1.8.4.Final</version> | |||
| </dependency> | |||
| <dependency> | |||
| <groupId>com.iformall</groupId> | |||
| <artifactId>mybatis-multi-tenancy</artifactId> | |||
| <version>1.0</version> | |||
| </dependency> | |||
| </dependencies> | |||
| @@ -0,0 +1,150 @@ | |||
| package com.iformall.config; | |||
| import java.util.ArrayList; | |||
| import java.util.List; | |||
| import java.util.Properties; | |||
| import com.iformall.plugin.shard.impl.EnumShardingRule; | |||
| import com.iformall.plugin.shard.impl.ShardingSphere; | |||
| import com.iformall.plugin.shard.plugin.ShardingSpherePlugin; | |||
| public class BaseMyBatisConfiguration { | |||
| //分表配置 | |||
| protected ShardingSpherePlugin baseShardingSpherePlugin() { | |||
| ShardingSpherePlugin shardingSpherePlugin = new ShardingSpherePlugin(); | |||
| Properties properties = new Properties(); | |||
| properties.setProperty("dialect", "mysql"); | |||
| shardingSpherePlugin.setProperties(properties); | |||
| List<ShardingSphere> shardingList = new ArrayList<ShardingSphere>(); | |||
| shardingSpherePlugin.setShardingSpheres(shardingList); | |||
| ShardingSphere wxCouponrSharding = new ShardingSphere(); | |||
| wxCouponrSharding.setColumn("tenant_id"); | |||
| wxCouponrSharding.setTableName("wx_coupon"); | |||
| wxCouponrSharding.setCount(100); | |||
| wxCouponrSharding.setRule(EnumShardingRule.HASH.getCode()); | |||
| shardingList.add(wxCouponrSharding); | |||
| ShardingSphere wxCouponrMerchantSharding = new ShardingSphere(); | |||
| wxCouponrMerchantSharding.setColumn("tenant_id"); | |||
| wxCouponrMerchantSharding.setTableName("wx_coupon_merchant"); | |||
| wxCouponrMerchantSharding.setCount(100); | |||
| wxCouponrMerchantSharding.setRule(EnumShardingRule.HASH.getCode()); | |||
| shardingList.add(wxCouponrMerchantSharding); | |||
| ShardingSphere wxCouponrChannelSharding = new ShardingSphere(); | |||
| wxCouponrChannelSharding.setColumn("tenant_id"); | |||
| wxCouponrChannelSharding.setTableName("wx_coupon_channel"); | |||
| wxCouponrChannelSharding.setCount(100); | |||
| wxCouponrChannelSharding.setRule(EnumShardingRule.HASH.getCode()); | |||
| shardingList.add(wxCouponrChannelSharding); | |||
| ShardingSphere wxCUserSharding = new ShardingSphere(); | |||
| wxCUserSharding.setColumn("tenant_id"); | |||
| wxCUserSharding.setTableName("wx_c_user"); | |||
| wxCUserSharding.setCount(100); | |||
| wxCUserSharding.setRule(EnumShardingRule.HASH.getCode()); | |||
| shardingList.add(wxCUserSharding); | |||
| ShardingSphere basicInfoSharding = new ShardingSphere(); | |||
| basicInfoSharding.setColumn("final_tenant_id"); | |||
| basicInfoSharding.setTableName("wx_c_user_basic_info"); | |||
| basicInfoSharding.setCount(100); | |||
| basicInfoSharding.setRule(EnumShardingRule.HASH.getCode()); | |||
| shardingList.add(basicInfoSharding); | |||
| ShardingSphere wxOrderSharding = new ShardingSphere(); | |||
| wxOrderSharding.setColumn("tenant_id"); | |||
| wxOrderSharding.setTableName("wx_order"); | |||
| wxOrderSharding.setCount(100); | |||
| wxOrderSharding.setRule(EnumShardingRule.HASH.getCode()); | |||
| shardingList.add(wxOrderSharding); | |||
| ShardingSphere wxPayOrderSharding = new ShardingSphere(); | |||
| wxPayOrderSharding.setColumn("tenant_id"); | |||
| wxPayOrderSharding.setTableName("wx_pay_order"); | |||
| wxPayOrderSharding.setCount(100); | |||
| wxPayOrderSharding.setRule(EnumShardingRule.HASH.getCode()); | |||
| shardingList.add(wxPayOrderSharding); | |||
| ShardingSphere wxCouponOrderSharding = new ShardingSphere(); | |||
| wxCouponOrderSharding.setColumn("tenant_id"); | |||
| wxCouponOrderSharding.setTableName("wx_coupon_order"); | |||
| wxCouponOrderSharding.setCount(100); | |||
| wxCouponOrderSharding.setRule(EnumShardingRule.HASH.getCode()); | |||
| shardingList.add(wxCouponOrderSharding); | |||
| ShardingSphere wxOrderGroupSharding = new ShardingSphere(); | |||
| wxOrderGroupSharding.setColumn("tenant_id"); | |||
| wxOrderGroupSharding.setTableName("wx_order_group"); | |||
| wxOrderGroupSharding.setCount(100); | |||
| wxOrderGroupSharding.setRule(EnumShardingRule.HASH.getCode()); | |||
| shardingList.add(wxOrderGroupSharding); | |||
| ShardingSphere wxOrderPressSharding = new ShardingSphere(); | |||
| wxOrderPressSharding.setColumn("tenant_id"); | |||
| wxOrderPressSharding.setTableName("wx_order_press"); | |||
| wxOrderPressSharding.setCount(100); | |||
| wxOrderPressSharding.setRule(EnumShardingRule.HASH.getCode()); | |||
| shardingList.add(wxOrderPressSharding); | |||
| ShardingSphere wxPosOrderSharding = new ShardingSphere(); | |||
| wxPosOrderSharding.setColumn("tenant_id"); | |||
| wxPosOrderSharding.setTableName("pos_order"); | |||
| wxPosOrderSharding.setCount(100); | |||
| wxPosOrderSharding.setRule(EnumShardingRule.HASH.getCode()); | |||
| shardingList.add(wxPosOrderSharding); | |||
| ShardingSphere wxPosPayOrderSharding = new ShardingSphere(); | |||
| wxPosPayOrderSharding.setColumn("tenant_id"); | |||
| wxPosPayOrderSharding.setTableName("pos_pay_order"); | |||
| wxPosPayOrderSharding.setCount(100); | |||
| wxPosPayOrderSharding.setRule(EnumShardingRule.HASH.getCode()); | |||
| shardingList.add(wxPosPayOrderSharding); | |||
| ShardingSphere wxScoreHistorySharding = new ShardingSphere(); | |||
| wxScoreHistorySharding.setColumn("tenant_id"); | |||
| wxScoreHistorySharding.setTableName("wx_score_history"); | |||
| wxScoreHistorySharding.setCount(100); | |||
| wxScoreHistorySharding.setRule(EnumShardingRule.HASH.getCode()); | |||
| shardingList.add(wxScoreHistorySharding); | |||
| ShardingSphere wxCreditHistorySharding = new ShardingSphere(); | |||
| wxScoreHistorySharding.setColumn("tenant_id"); | |||
| wxScoreHistorySharding.setTableName("wx_credit_history"); | |||
| wxScoreHistorySharding.setCount(100); | |||
| wxScoreHistorySharding.setRule(EnumShardingRule.HASH.getCode()); | |||
| shardingList.add(wxCreditHistorySharding); | |||
| ShardingSphere wxCarCmdLogSharding = new ShardingSphere(); | |||
| wxCarCmdLogSharding.setColumn("tenant_id"); | |||
| wxCarCmdLogSharding.setTableName("wx_car_cmd_log"); | |||
| wxCarCmdLogSharding.setCount(100); | |||
| wxCarCmdLogSharding.setRule(EnumShardingRule.HASH.getCode()); | |||
| shardingList.add(wxCarCmdLogSharding); | |||
| ShardingSphere wxMerchantSubsidySharding = new ShardingSphere(); | |||
| wxMerchantSubsidySharding.setColumn("tenant_id"); | |||
| wxMerchantSubsidySharding.setTableName("wx_merchant_subsidy"); | |||
| wxMerchantSubsidySharding.setCount(100); | |||
| wxMerchantSubsidySharding.setRule(EnumShardingRule.HASH.getCode()); | |||
| shardingList.add(wxMerchantSubsidySharding); | |||
| ShardingSphere wxProfitSharingOrderSharding = new ShardingSphere(); | |||
| wxProfitSharingOrderSharding.setColumn("tenant_id"); | |||
| wxProfitSharingOrderSharding.setTableName("wx_profit_sharing_order"); | |||
| wxProfitSharingOrderSharding.setCount(100); | |||
| wxProfitSharingOrderSharding.setRule(EnumShardingRule.HASH.getCode()); | |||
| shardingList.add(wxProfitSharingOrderSharding); | |||
| ShardingSphere wxCouponActionLogSharding = new ShardingSphere(); | |||
| wxCouponActionLogSharding.setColumn("tenant_id"); | |||
| wxCouponActionLogSharding.setTableName("wx_coupon_action_log"); | |||
| wxCouponActionLogSharding.setCount(100); | |||
| wxCouponActionLogSharding.setRule(EnumShardingRule.HASH.getCode()); | |||
| shardingList.add(wxCouponActionLogSharding); | |||
| return shardingSpherePlugin; | |||
| } | |||
| } | |||
| @@ -40,7 +40,6 @@ | |||
| <artifactId>kaptcha</artifactId> | |||
| <version>0.0.9</version> | |||
| </dependency> | |||
| </dependencies> | |||
| <build> | |||
| @@ -0,0 +1,21 @@ | |||
| package com.iformall.config; | |||
| import com.iformall.plugin.shard.impl.EnumShardingRule; | |||
| import com.iformall.plugin.shard.impl.ShardingSphere; | |||
| import com.iformall.plugin.shard.plugin.ShardingSpherePlugin; | |||
| import org.springframework.context.annotation.Bean; | |||
| import org.springframework.context.annotation.Configuration; | |||
| import org.springframework.transaction.annotation.EnableTransactionManagement; | |||
| import java.util.ArrayList; | |||
| import java.util.List; | |||
| import java.util.Properties; | |||
| @EnableTransactionManagement | |||
| @Configuration | |||
| public class MyBatisConfiguration extends BaseMyBatisConfiguration { | |||
| @Bean | |||
| public ShardingSpherePlugin shardingSpherePlugin() { | |||
| return baseShardingSpherePlugin(); | |||
| } | |||
| } | |||
| @@ -3,7 +3,11 @@ spring: | |||
| include: rabbitMQ | |||
| # JDBC | |||
| datasource: | |||
| <<<<<<< HEAD | |||
| url: jdbc:mysql://202.165.179.86:3306/mallink_share?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useUnicode=true&useSSL=false&useAffectedRows=true | |||
| ======= | |||
| url: jdbc:mysql://101.200.130.134:3306/mallink?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useUnicode=true&useSSL=false&useAffectedRows=true | |||
| >>>>>>> release_real_202010 | |||
| username: root | |||
| password: fm2020test | |||
| type: com.alibaba.druid.pool.DruidDataSource | |||
| @@ -25,7 +25,7 @@ | |||
| <dependency> | |||
| <groupId>org.springframework.boot</groupId> | |||
| <artifactId>spring-boot-starter-reactor-netty</artifactId> | |||
| </dependency> | |||
| </dependency> | |||
| </dependencies> | |||
| <build> | |||
| @@ -0,0 +1,21 @@ | |||
| package com.iformall.config; | |||
| import com.iformall.plugin.shard.impl.EnumShardingRule; | |||
| import com.iformall.plugin.shard.impl.ShardingSphere; | |||
| import com.iformall.plugin.shard.plugin.ShardingSpherePlugin; | |||
| import org.springframework.context.annotation.Bean; | |||
| import org.springframework.context.annotation.Configuration; | |||
| import org.springframework.transaction.annotation.EnableTransactionManagement; | |||
| import java.util.ArrayList; | |||
| import java.util.List; | |||
| import java.util.Properties; | |||
| @EnableTransactionManagement | |||
| @Configuration | |||
| public class MyBatisConfiguration extends BaseMyBatisConfiguration { | |||
| @Bean | |||
| public ShardingSpherePlugin shardingSpherePlugin() { | |||
| return baseShardingSpherePlugin(); | |||
| } | |||
| } | |||
| @@ -0,0 +1,16 @@ | |||
| package com.iformall.plugin.shard.annotation; | |||
| import java.lang.annotation.Documented; | |||
| import java.lang.annotation.ElementType; | |||
| import java.lang.annotation.Retention; | |||
| import java.lang.annotation.RetentionPolicy; | |||
| import java.lang.annotation.Target; | |||
| /** | |||
| * 在mapper方法上面用 | |||
| */ | |||
| @Documented | |||
| @Retention(RetentionPolicy.RUNTIME) | |||
| @Target(ElementType.METHOD) | |||
| public @interface IgnoreSharding { | |||
| } | |||
| @@ -0,0 +1,27 @@ | |||
| package com.iformall.plugin.shard.impl; | |||
| public enum EnumShardingRule { | |||
| HASH(1,"哈希值分表"); | |||
| private Integer code; | |||
| private String name; | |||
| private EnumShardingRule(Integer code, String name) { | |||
| this.code = code; | |||
| this.name = name; | |||
| } | |||
| public Integer getCode() { | |||
| return code; | |||
| } | |||
| public void setCode(Integer code) { | |||
| this.code = code; | |||
| } | |||
| public String getName() { | |||
| return name; | |||
| } | |||
| public void setName(String name) { | |||
| this.name = name; | |||
| } | |||
| } | |||
| @@ -0,0 +1,29 @@ | |||
| package com.iformall.plugin.shard.impl; | |||
| import java.util.Map; | |||
| import java.util.concurrent.ConcurrentHashMap; | |||
| import com.alibaba.druid.util.StringUtils; | |||
| import com.iformall.plugin.shard.impl.rule.ShardingRuleDefaultService; | |||
| import com.iformall.plugin.shard.impl.rule.ShardingRuleService; | |||
| public class ShardingRuleExcuter { | |||
| private static Map<Integer,ShardingRuleService> serviceMap = null; | |||
| private static Map<Integer,ShardingRuleService> getServiceMap() { | |||
| if (null == serviceMap) { | |||
| serviceMap = new ConcurrentHashMap<Integer, ShardingRuleService>(); | |||
| serviceMap.put(EnumShardingRule.HASH.getCode(), new ShardingRuleDefaultService()); | |||
| } | |||
| return serviceMap; | |||
| } | |||
| public static String getTableIndex(Integer rule,Object value,int tableCount) { | |||
| String index = getServiceMap().get(rule).getTableIndex(value, tableCount); | |||
| if (StringUtils.isEmpty(index)) { | |||
| throw new RuntimeException("Sharding Rule["+rule+"] not support. column value:"+value+" tableCount:"+tableCount) ; | |||
| } | |||
| return index; | |||
| } | |||
| } | |||
| @@ -0,0 +1,38 @@ | |||
| package com.iformall.plugin.shard.impl; | |||
| public class ShardingSphere { | |||
| public static final String TABLE_SUFFIX = "_"; | |||
| /*表名,不用带下划线**/ | |||
| private String tableName; | |||
| /*分表列名**/ | |||
| private String column; | |||
| /*分表规则**/ | |||
| private Integer rule; | |||
| /*表数量**/ | |||
| private Integer count; | |||
| public String getTableName() { | |||
| return tableName; | |||
| } | |||
| public void setTableName(String tableName) { | |||
| this.tableName = tableName; | |||
| } | |||
| public String getColumn() { | |||
| return column; | |||
| } | |||
| public void setColumn(String column) { | |||
| this.column = column; | |||
| } | |||
| public Integer getRule() { | |||
| return rule; | |||
| } | |||
| public void setRule(Integer rule) { | |||
| this.rule = rule; | |||
| } | |||
| public Integer getCount() { | |||
| return count; | |||
| } | |||
| public void setCount(Integer count) { | |||
| this.count = count; | |||
| } | |||
| } | |||
| @@ -0,0 +1,33 @@ | |||
| package com.iformall.plugin.shard.impl; | |||
| import java.util.Map; | |||
| import java.util.concurrent.ConcurrentHashMap; | |||
| public class ShardingTableExcuter { | |||
| private static Map<String,ShardingSphere> tableMap = new ConcurrentHashMap<String, ShardingSphere>(); | |||
| public static Map<String,ShardingSphere> setTableSharding(String table ,ShardingSphere ss) { | |||
| tableMap.put(table, ss); | |||
| return tableMap; | |||
| } | |||
| public static ShardingSphere getShardingSphere(String table) { | |||
| return tableMap.get(table); | |||
| } | |||
| public static boolean isShardingTable(String table) { | |||
| ShardingSphere ss = getShardingSphere(table); | |||
| if (null == ss) { | |||
| return false; | |||
| } | |||
| return true; | |||
| } | |||
| public static boolean hasShardingConfig() { | |||
| if (null == tableMap || tableMap.size() <= 0) { | |||
| return false; | |||
| } | |||
| return true; | |||
| } | |||
| } | |||
| @@ -0,0 +1,32 @@ | |||
| package com.iformall.plugin.shard.impl.rule; | |||
| public class ShardingRuleDefaultService implements ShardingRuleService { | |||
| @Override | |||
| public String getTableIndex(Object value, int count) { | |||
| if (null == value || count <= 0 ) { | |||
| return null; | |||
| } | |||
| int hash = 9999999; | |||
| //如果是整数,则直接变为Long,好查询 | |||
| if (value instanceof String) { | |||
| try { | |||
| Long lvalue = Long.parseLong((String)value); | |||
| hash = getHashCode(lvalue); | |||
| }catch(Exception e) { | |||
| hash = getHashCode(value); | |||
| } | |||
| }else { | |||
| hash = getHashCode(value); | |||
| } | |||
| return String.valueOf(hash%count); | |||
| } | |||
| private int getHashCode(Object value) { | |||
| int hash = value.hashCode(); | |||
| if (hash < 0 ) { | |||
| hash = Math.abs(hash); | |||
| } | |||
| return hash; | |||
| } | |||
| } | |||
| @@ -0,0 +1,6 @@ | |||
| package com.iformall.plugin.shard.impl.rule; | |||
| public interface ShardingRuleService { | |||
| public String getTableIndex(Object value,int count); | |||
| } | |||
| @@ -0,0 +1,227 @@ | |||
| package com.iformall.plugin.shard.plugin; | |||
| import net.sf.jsqlparser.JSQLParserException; | |||
| 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.EqualsTo; | |||
| import net.sf.jsqlparser.expression.operators.relational.ExpressionList; | |||
| import net.sf.jsqlparser.expression.operators.relational.ItemsList; | |||
| import net.sf.jsqlparser.expression.operators.relational.MultiExpressionList; | |||
| import net.sf.jsqlparser.parser.CCJSqlParserUtil; | |||
| import net.sf.jsqlparser.schema.Column; | |||
| import net.sf.jsqlparser.schema.Table; | |||
| import net.sf.jsqlparser.statement.Statement; | |||
| import net.sf.jsqlparser.statement.create.table.ColDataType; | |||
| import net.sf.jsqlparser.statement.create.table.ColumnDefinition; | |||
| import net.sf.jsqlparser.statement.create.table.CreateTable; | |||
| import net.sf.jsqlparser.statement.delete.Delete; | |||
| import net.sf.jsqlparser.statement.insert.Insert; | |||
| import net.sf.jsqlparser.statement.select.*; | |||
| import net.sf.jsqlparser.statement.update.Update; | |||
| import net.sf.jsqlparser.util.TablesNamesFinder; | |||
| import org.apache.ibatis.executor.statement.StatementHandler; | |||
| import org.apache.ibatis.mapping.BoundSql; | |||
| import org.apache.ibatis.mapping.MappedStatement; | |||
| import org.apache.ibatis.mapping.ParameterMapping; | |||
| import org.apache.ibatis.mapping.SqlCommandType; | |||
| import org.apache.ibatis.plugin.*; | |||
| import org.apache.ibatis.reflection.MetaObject; | |||
| import org.apache.ibatis.reflection.SystemMetaObject; | |||
| import org.apache.ibatis.session.Configuration; | |||
| import org.apache.ibatis.session.ResultHandler; | |||
| import com.iformall.plugin.shard.annotation.IgnoreSharding; | |||
| import com.iformall.plugin.shard.impl.ShardingRuleExcuter; | |||
| import com.iformall.plugin.shard.impl.ShardingSphere; | |||
| import com.iformall.plugin.shard.impl.ShardingTableExcuter; | |||
| import java.lang.reflect.Method; | |||
| import java.sql.Connection; | |||
| import java.util.Arrays; | |||
| import java.util.HashMap; | |||
| import java.util.List; | |||
| import java.util.Map; | |||
| import java.util.Properties; | |||
| /** | |||
| *分表 | |||
| */ | |||
| @Intercepts({ | |||
| //@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}), | |||
| //@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}) | |||
| @Signature( method = "prepare", type = StatementHandler.class,args = {Connection.class, Integer.class}), | |||
| @Signature(method = "query", type = StatementHandler.class, args = {java.sql.Statement.class, ResultHandler.class}) | |||
| }) | |||
| public class ShardingSpherePlugin implements Interceptor { | |||
| public ShardingSpherePlugin(){ | |||
| } | |||
| //属性参数信息 | |||
| private Properties properties; | |||
| private String dialect = "mysql"; | |||
| private List<ShardingSphere> ShardingSpheres; | |||
| public String getDialect() { | |||
| return dialect; | |||
| } | |||
| public void setDialect(String dialect) { | |||
| this.dialect = dialect; | |||
| } | |||
| public List<ShardingSphere> getShardingSpheres() { | |||
| return ShardingSpheres; | |||
| } | |||
| public void setShardingSpheres(List<ShardingSphere> shardingSpheres) { | |||
| ShardingSpheres = shardingSpheres; | |||
| } | |||
| /** | |||
| * 生成代理对象 | |||
| * | |||
| * @param target | |||
| * @return | |||
| */ | |||
| @Override | |||
| public Object plugin(Object target) { | |||
| if (target instanceof StatementHandler) { | |||
| return Plugin.wrap(target, this); | |||
| } else { | |||
| return target; | |||
| } | |||
| } | |||
| /** | |||
| * 获取配置信息 | |||
| * {dialect=mysql, tenantInfo=org.xue.test.TenantInfoImpl, tenantIdColumn=tenant_id, parentTenantIdColumn=parent_tenant_id} | |||
| * @param properties | |||
| */ | |||
| @Override | |||
| public void setProperties(Properties properties) { | |||
| this.properties=properties; | |||
| this.dialect = this.properties.getProperty("dialect"); | |||
| if (null != this.ShardingSpheres) { | |||
| for (ShardingSphere ss : this.ShardingSpheres) { | |||
| ShardingTableExcuter.setTableSharding(ss.getTableName(), ss); | |||
| } | |||
| } | |||
| } | |||
| private Method findMethod(Method[] methods, String methodName) { | |||
| for (Method method : methods) { | |||
| if (method.getName().equals(methodName)) { | |||
| return method; | |||
| } | |||
| } | |||
| return null; | |||
| } | |||
| public Object intercept(Invocation invocation) throws Throwable { | |||
| if (!ShardingTableExcuter.hasShardingConfig()) { | |||
| return invocation.proceed(); | |||
| } | |||
| StatementHandler statementHandler = (StatementHandler) invocation.getTarget(); | |||
| MetaObject metaStatementHandler = SystemMetaObject.forObject(statementHandler); | |||
| MappedStatement ms = (MappedStatement) metaStatementHandler.getValue("delegate.mappedStatement"); | |||
| String id = ms.getId(); | |||
| String className = id.substring(0, id.lastIndexOf(".")); | |||
| String methodName = id.substring(id.lastIndexOf(".") + 1); | |||
| Class<?> clazz = Class.forName(className); | |||
| Method method = findMethod(clazz.getDeclaredMethods(), methodName); | |||
| if (method != null) { | |||
| IgnoreSharding ignore = method.getAnnotation(IgnoreSharding.class); | |||
| if (null == ignore) { | |||
| //Configuration configuration = (Configuration) metaStatementHandler.getValue("delegate.configuration"); | |||
| Object parameterObject = metaStatementHandler.getValue("delegate.boundSql.parameterObject"); | |||
| BoundSql boundSql = statementHandler.getBoundSql(); | |||
| String newSQL = buildTable(boundSql,parameterObject); | |||
| metaStatementHandler.setValue("delegate.boundSql.sql", newSQL); | |||
| } | |||
| } | |||
| return invocation.proceed(); | |||
| } | |||
| private String buildTable(BoundSql boundSql,Object parameterObject) throws JSQLParserException { | |||
| String oldSql = boundSql.getSql(); | |||
| Statement stmt = CCJSqlParserUtil.parse(oldSql); | |||
| if (stmt instanceof Insert) { | |||
| return buildInsert((Insert) stmt,oldSql,parameterObject); | |||
| } | |||
| if (stmt instanceof Delete) { | |||
| return buildDelete((Delete) stmt,oldSql,parameterObject); | |||
| } | |||
| if (stmt instanceof Update) { | |||
| return buildUpdate((Update) stmt,oldSql,parameterObject); | |||
| } | |||
| if (stmt instanceof Select) { | |||
| return buildSelect((Select) stmt,oldSql,parameterObject); | |||
| } | |||
| if (stmt instanceof CreateTable) { | |||
| return oldSql; | |||
| } | |||
| throw new RuntimeException("非法sql语句,请检查"+oldSql); | |||
| } | |||
| private String buildInsert(Insert stmt,String oldSql,Object parameterObject) { | |||
| return buildTable(stmt.getTable(), oldSql, parameterObject); | |||
| } | |||
| private String buildDelete(Delete stmt,String oldSql,Object parameterObject) { | |||
| return buildTable(stmt.getTable(), oldSql, parameterObject); | |||
| } | |||
| private String buildUpdate(Update stmt,String oldSql,Object parameterObject) { | |||
| return buildListTables(stmt.getTables(), oldSql, parameterObject); | |||
| } | |||
| private String buildSelect(Select stmt,String oldSql,Object parameterObject) { | |||
| TablesNamesFinder tablesNamesFinder = new TablesNamesFinder(); | |||
| List<String> tableList = tablesNamesFinder.getTableList(stmt); | |||
| return buildByListTableNames(tableList, oldSql, parameterObject); | |||
| } | |||
| private String buildTable(Table table,String oldSql,Object parameterObject) { | |||
| return buildByTableName(table.getName(), oldSql, parameterObject); | |||
| } | |||
| private String buildByTableName(String table,String oldSql,Object parameterObject) { | |||
| if (!ShardingTableExcuter.isShardingTable(table)) { | |||
| return oldSql; | |||
| } | |||
| String newtable = getShareTable(table,parameterObject); | |||
| return oldSql.replaceAll(table+" ", newtable+" ").replaceAll(table+".", newtable+"。").replaceAll(table+")", newtable+")"); | |||
| } | |||
| private String buildListTables(List<Table> tables,String oldSql,Object parameterObject) { | |||
| for (Table t : tables) { | |||
| oldSql = buildTable(t,oldSql,parameterObject); | |||
| } | |||
| return oldSql; | |||
| } | |||
| private String buildByListTableNames(List<String> tables,String oldSql,Object parameterObject) { | |||
| for (String t : tables) { | |||
| oldSql = buildByTableName(t,oldSql,parameterObject); | |||
| } | |||
| return oldSql; | |||
| } | |||
| private String getShareTable(String table,Object parameterObject) { | |||
| ShardingSphere ss = ShardingTableExcuter.getShardingSphere(table); | |||
| return ss.getTableName()+ShardingSphere.TABLE_SUFFIX+ShardingRuleExcuter.getTableIndex(ss.getRule(), getValue(ss.getColumn(),parameterObject), ss.getCount()); | |||
| } | |||
| private Object getValue(String column,Object parameterObject) { | |||
| if (parameterObject instanceof Map) { | |||
| return ((Map) parameterObject).get(column); | |||
| } | |||
| return parameterObject; | |||
| } | |||
| } | |||