| @@ -2,12 +2,10 @@ 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 com.iformall.plugin.MyBatisItercepters; | |||
| import org.springframework.context.annotation.Bean; | |||
| import org.springframework.context.annotation.Configuration; | |||
| import org.springframework.transaction.annotation.EnableTransactionManagement; | |||
| import java.util.Properties; | |||
| @EnableTransactionManagement | |||
| @Configuration | |||
| @@ -15,20 +13,8 @@ public class MyBatisConfiguration extends BaseMyBatisConfiguration{ | |||
| @Bean | |||
| public MultiTenancy multiTenancy() { | |||
| MultiTenancy multiTenancy = new MultiTenancy(); | |||
| Properties properties = new Properties(); | |||
| properties.setProperty("tenantIdColumn", "tenant_id"); | |||
| properties.setProperty("parentTenantIdColumn", "parent_tenant_id"); | |||
| properties.setProperty("dialect", "mysql"); | |||
| properties.setProperty("tenantInfo", "com.iformall.tenant.TenantInfoImpl"); | |||
| multiTenancy.setProperties(properties); | |||
| return multiTenancy; | |||
| } | |||
| @Bean | |||
| public ShardingSpherePlugin shardingSpherePlugin() { | |||
| return baseShardingSpherePlugin(); | |||
| public MyBatisItercepters intercepters() { | |||
| return intercepters(); | |||
| } | |||
| @Bean | |||
| @@ -1,7 +1,7 @@ | |||
| package com.iformall.tenant; | |||
| import com.iformall.common.TenantThreadLocal; | |||
| import com.iformall.plugin.TenantInfo; | |||
| import com.iformall.plugin.autoTenantId.TenantInfo; | |||
| import com.iformall.shiro.UserSession; | |||
| import org.apache.ibatis.mapping.MappedStatement; | |||
| import org.apache.shiro.SecurityUtils; | |||
| @@ -1,34 +1,15 @@ | |||
| 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 com.iformall.plugin.MyBatisItercepters; | |||
| 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 extends BaseMyBatisConfiguration{ | |||
| @Bean | |||
| public MultiTenancy multiTenancy() { | |||
| MultiTenancy multiTenancy = new MultiTenancy(); | |||
| Properties properties = new Properties(); | |||
| properties.setProperty("tenantIdColumn", "tenant_id"); | |||
| properties.setProperty("parentTenantIdColumn", "parent_tenant_id"); | |||
| properties.setProperty("dialect", "mysql"); | |||
| properties.setProperty("tenantInfo", "com.iformall.tenant.TenantInfoImpl"); | |||
| multiTenancy.setProperties(properties); | |||
| return multiTenancy; | |||
| } | |||
| @Bean | |||
| public ShardingSpherePlugin shardingSpherePlugin() { | |||
| return baseShardingSpherePlugin(); | |||
| public MyBatisItercepters intercepters() { | |||
| return intercepters(); | |||
| } | |||
| } | |||
| @@ -1,7 +1,7 @@ | |||
| package com.iformall.tenant; | |||
| import com.iformall.common.TenantThreadLocal; | |||
| import com.iformall.plugin.TenantInfo; | |||
| import com.iformall.plugin.autoTenantId.TenantInfo; | |||
| import com.iformall.utils.Constant; | |||
| import org.apache.ibatis.mapping.MappedStatement; | |||
| import org.slf4j.Logger; | |||
| @@ -1,34 +1,14 @@ | |||
| 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 com.iformall.plugin.MyBatisItercepters; | |||
| 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 extends BaseMyBatisConfiguration { | |||
| @Bean | |||
| public MultiTenancy multiTenancy() { | |||
| MultiTenancy multiTenancy = new MultiTenancy(); | |||
| Properties properties = new Properties(); | |||
| properties.setProperty("tenantIdColumn", "tenant_id"); | |||
| properties.setProperty("parentTenantIdColumn", "parent_tenant_id"); | |||
| properties.setProperty("dialect", "mysql"); | |||
| properties.setProperty("tenantInfo", "com.iformall.tenant.TenantInfoImpl"); | |||
| multiTenancy.setProperties(properties); | |||
| return multiTenancy; | |||
| } | |||
| @Bean | |||
| public ShardingSpherePlugin shardingSpherePlugin() { | |||
| return baseShardingSpherePlugin(); | |||
| public MyBatisItercepters intercepters() { | |||
| return intercepters(); | |||
| } | |||
| } | |||
| @@ -222,33 +222,33 @@ public class WxUserGrantController extends BaseController { | |||
| String token = null; | |||
| String session_key = null; | |||
| String openId = null; | |||
| String openId = "opvcM5IEkl0U5pNiHcN49Ens40jU"; | |||
| String unionId = null; | |||
| boolean boolHaveUnionId = false; | |||
| boolean boolFindByUnionId = false; | |||
| Long userId = null; | |||
| try { | |||
| WxMaJscode2SessionResult session = wxMaService.jsCode2SessionInfo(code); | |||
| logger.info("sessionResult: " + session); | |||
| if (null != session) { | |||
| logger.info("sessionResultDetail: [openid]" + session.getOpenid()+",[sessionkey]"+session.getSessionKey()+",[unionid]"+session.getUnionid()); | |||
| } | |||
| //获取会话密钥(session_key) | |||
| session_key = session.getSessionKey(); | |||
| openId = session.getOpenid(); | |||
| unionId = session.getUnionid(); | |||
| // if (StringUtils.isNotBlank(unionId)) { | |||
| // boolHaveUnionId = true; | |||
| // try { | |||
| // WxMaJscode2SessionResult session = wxMaService.jsCode2SessionInfo(code); | |||
| // logger.info("sessionResult: " + session); | |||
| // if (null != session) { | |||
| // logger.info("sessionResultDetail: [openid]" + session.getOpenid()+",[sessionkey]"+session.getSessionKey()+",[unionid]"+session.getUnionid()); | |||
| // } | |||
| logger.info("session_key: " + session_key + ", openId: " + openId + ", unionId: " + unionId); | |||
| resultMap.put("openId", openId); | |||
| } catch (WxErrorException e) { | |||
| logger.error(e.getMessage(), e); | |||
| return new ResultData(ErrorCode.SESSION_KEY_DECODE_ERR, resultMap); | |||
| } | |||
| // | |||
| // //获取会话密钥(session_key) | |||
| // session_key = session.getSessionKey(); | |||
| // openId = session.getOpenid(); | |||
| // unionId = session.getUnionid(); | |||
| //// if (StringUtils.isNotBlank(unionId)) { | |||
| //// boolHaveUnionId = true; | |||
| //// } | |||
| // | |||
| // logger.info("session_key: " + session_key + ", openId: " + openId + ", unionId: " + unionId); | |||
| // resultMap.put("openId", openId); | |||
| // } catch (WxErrorException e) { | |||
| // logger.error(e.getMessage(), e); | |||
| // return new ResultData(ErrorCode.SESSION_KEY_DECODE_ERR, resultMap); | |||
| // } | |||
| HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); | |||
| String ipaddress = IPUtil.getIpAddr(request); | |||
| @@ -1,7 +1,7 @@ | |||
| package com.iformall.tenant; | |||
| import com.iformall.common.TenantThreadLocal; | |||
| import com.iformall.plugin.TenantInfo; | |||
| import com.iformall.plugin.autoTenantId.TenantInfo; | |||
| import com.iformall.utils.Constant; | |||
| import org.apache.ibatis.mapping.MappedStatement; | |||
| import org.slf4j.Logger; | |||
| @@ -1,6 +1,11 @@ | |||
| package com.iformall.config; | |||
| import com.iformall.plugin.shard.plugin.ShardingSpherePlugin; | |||
| import com.iformall.plugin.MyBatisItercepters; | |||
| import com.iformall.plugin.MyBatisPlus; | |||
| import java.util.ArrayList; | |||
| import java.util.List; | |||
| import org.springframework.context.annotation.Bean; | |||
| import org.springframework.context.annotation.Configuration; | |||
| import org.springframework.transaction.annotation.EnableTransactionManagement; | |||
| @@ -10,7 +15,12 @@ import org.springframework.transaction.annotation.EnableTransactionManagement; | |||
| public class MyBatisConfiguration extends BaseMyBatisConfiguration{ | |||
| @Bean | |||
| public ShardingSpherePlugin shardingSpherePlugin() { | |||
| return baseShardingSpherePlugin(); | |||
| } | |||
| public MyBatisItercepters intercepters() { | |||
| MyBatisItercepters intercepters = new MyBatisItercepters(); | |||
| List<MyBatisPlus> plugins = new ArrayList<MyBatisPlus>(); | |||
| plugins.add(baseShardingSpherePlugin()); | |||
| intercepters.setPlugins(plugins); | |||
| return intercepters; | |||
| } | |||
| } | |||
| @@ -1,5 +1,7 @@ | |||
| package com.iformall.config; | |||
| import com.iformall.plugin.MyBatisItercepters; | |||
| import com.iformall.plugin.MyBatisPlus; | |||
| import com.iformall.plugin.shard.impl.EnumShardingRule; | |||
| import com.iformall.plugin.shard.impl.ShardingSphere; | |||
| import com.iformall.plugin.shard.plugin.ShardingSpherePlugin; | |||
| @@ -15,7 +17,12 @@ import java.util.Properties; | |||
| public class MyBatisConfiguration extends BaseMyBatisConfiguration { | |||
| @Bean | |||
| public ShardingSpherePlugin shardingSpherePlugin() { | |||
| return baseShardingSpherePlugin(); | |||
| } | |||
| public MyBatisItercepters intercepters() { | |||
| MyBatisItercepters intercepters = new MyBatisItercepters(); | |||
| List<MyBatisPlus> plugins = new ArrayList<MyBatisPlus>(); | |||
| plugins.add(baseShardingSpherePlugin()); | |||
| intercepters.setPlugins(plugins); | |||
| return intercepters; | |||
| } | |||
| } | |||
| @@ -1,5 +1,7 @@ | |||
| package com.iformall.config; | |||
| import com.iformall.plugin.MyBatisItercepters; | |||
| import com.iformall.plugin.MyBatisPlus; | |||
| import com.iformall.plugin.shard.impl.EnumShardingRule; | |||
| import com.iformall.plugin.shard.impl.ShardingSphere; | |||
| import com.iformall.plugin.shard.plugin.ShardingSpherePlugin; | |||
| @@ -15,7 +17,12 @@ import java.util.Properties; | |||
| public class MyBatisConfiguration extends BaseMyBatisConfiguration { | |||
| @Bean | |||
| public ShardingSpherePlugin shardingSpherePlugin() { | |||
| return baseShardingSpherePlugin(); | |||
| } | |||
| public MyBatisItercepters intercepters() { | |||
| MyBatisItercepters intercepters = new MyBatisItercepters(); | |||
| List<MyBatisPlus> plugins = new ArrayList<MyBatisPlus>(); | |||
| plugins.add(baseShardingSpherePlugin()); | |||
| intercepters.setPlugins(plugins); | |||
| return intercepters; | |||
| } | |||
| } | |||
| @@ -1,6 +1,12 @@ | |||
| package com.iformall.config; | |||
| import com.iformall.plugin.MyBatisItercepters; | |||
| import com.iformall.plugin.MyBatisPlus; | |||
| import com.iformall.plugin.shard.plugin.ShardingSpherePlugin; | |||
| import java.util.ArrayList; | |||
| import java.util.List; | |||
| import org.springframework.context.annotation.Bean; | |||
| import org.springframework.context.annotation.Configuration; | |||
| import org.springframework.transaction.annotation.EnableTransactionManagement; | |||
| @@ -10,7 +16,12 @@ import org.springframework.transaction.annotation.EnableTransactionManagement; | |||
| public class MyBatisConfiguration extends BaseMyBatisConfiguration { | |||
| @Bean | |||
| public ShardingSpherePlugin shardingSpherePlugin() { | |||
| return baseShardingSpherePlugin(); | |||
| } | |||
| public MyBatisItercepters intercepters() { | |||
| MyBatisItercepters intercepters = new MyBatisItercepters(); | |||
| List<MyBatisPlus> plugins = new ArrayList<MyBatisPlus>(); | |||
| plugins.add(baseShardingSpherePlugin()); | |||
| intercepters.setPlugins(plugins); | |||
| return intercepters; | |||
| } | |||
| } | |||
| @@ -4,20 +4,42 @@ import java.util.ArrayList; | |||
| import java.util.List; | |||
| import java.util.Properties; | |||
| import com.iformall.plugin.MyBatisItercepters; | |||
| import com.iformall.plugin.MyBatisPlus; | |||
| import com.iformall.plugin.autoTenantId.MultiTenancy; | |||
| 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 MyBatisItercepters intercepters() { | |||
| MyBatisItercepters intercepters = new MyBatisItercepters(); | |||
| List<MyBatisPlus> plugins = new ArrayList<MyBatisPlus>(); | |||
| plugins.add(multiTenancy()); | |||
| plugins.add(baseShardingSpherePlugin()); | |||
| intercepters.setPlugins(plugins); | |||
| return intercepters; | |||
| } | |||
| protected MultiTenancy multiTenancy() { | |||
| MultiTenancy multiTenancy = new MultiTenancy(); | |||
| Properties properties = new Properties(); | |||
| properties.setProperty("tenantIdColumn", "tenant_id"); | |||
| properties.setProperty("parentTenantIdColumn", "parent_tenant_id"); | |||
| properties.setProperty("dialect", "mysql"); | |||
| properties.setProperty("tenantInfo", "com.iformall.tenant.TenantInfoImpl"); | |||
| multiTenancy.setProperties(properties); | |||
| return multiTenancy; | |||
| } | |||
| //分表配置 | |||
| 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"); | |||
| @@ -26,6 +48,7 @@ public class BaseMyBatisConfiguration { | |||
| wxCouponrSharding.setRule(EnumShardingRule.HASH.getCode()); | |||
| shardingList.add(wxCouponrSharding); | |||
| ShardingSphere wxCouponrMerchantSharding = new ShardingSphere(); | |||
| wxCouponrMerchantSharding.setColumn("tenant_id"); | |||
| wxCouponrMerchantSharding.setTableName("wx_coupon_merchant"); | |||
| @@ -74,7 +97,7 @@ public class BaseMyBatisConfiguration { | |||
| wxCouponOrderSharding.setCount(100); | |||
| wxCouponOrderSharding.setRule(EnumShardingRule.HASH.getCode()); | |||
| shardingList.add(wxCouponOrderSharding); | |||
| ShardingSphere wxOrderGroupSharding = new ShardingSphere(); | |||
| wxOrderGroupSharding.setColumn("tenant_id"); | |||
| wxOrderGroupSharding.setTableName("wx_order_group"); | |||
| @@ -94,7 +117,7 @@ public class BaseMyBatisConfiguration { | |||
| wxPosOrderSharding.setTableName("pos_order"); | |||
| wxPosOrderSharding.setCount(100); | |||
| wxPosOrderSharding.setRule(EnumShardingRule.HASH.getCode()); | |||
| shardingList.add(wxPosOrderSharding); | |||
| shardingList.add(wxPosOrderSharding); | |||
| ShardingSphere wxPosPayOrderSharding = new ShardingSphere(); | |||
| wxPosPayOrderSharding.setColumn("tenant_id"); | |||
| @@ -110,13 +133,13 @@ public class BaseMyBatisConfiguration { | |||
| wxScoreHistorySharding.setCount(100); | |||
| wxScoreHistorySharding.setRule(EnumShardingRule.HASH.getCode()); | |||
| shardingList.add(wxScoreHistorySharding); | |||
| //TODO ,逻辑待处理,tenant_id未和basic_info的final_tenant_id对应,去掉parent_tenant_id | |||
| ShardingSphere wxCreditHistorySharding = new ShardingSphere(); | |||
| wxScoreHistorySharding.setColumn("tenant_id"); | |||
| wxScoreHistorySharding.setTableName("wx_credit_history"); | |||
| wxScoreHistorySharding.setCount(100); | |||
| wxScoreHistorySharding.setRule(EnumShardingRule.HASH.getCode()); | |||
| wxCreditHistorySharding.setColumn("tenant_id"); | |||
| wxCreditHistorySharding.setTableName("wx_credit_history"); | |||
| wxCreditHistorySharding.setCount(100); | |||
| wxCreditHistorySharding.setRule(EnumShardingRule.HASH.getCode()); | |||
| shardingList.add(wxCreditHistorySharding); | |||
| //TODO ,打标签定时任务。逻辑待处理, | |||
| @@ -155,6 +178,13 @@ public class BaseMyBatisConfiguration { | |||
| wxCouponActionLogSharding.setRule(EnumShardingRule.HASH.getCode()); | |||
| shardingList.add(wxCouponActionLogSharding); | |||
| //初始化 | |||
| shardingSpherePlugin.setShardingSpheres(shardingList); | |||
| Properties properties = new Properties(); | |||
| properties.setProperty("dialect", "mysql"); | |||
| shardingSpherePlugin.setProperties(properties); | |||
| return shardingSpherePlugin; | |||
| } | |||
| } | |||
| @@ -1,5 +1,7 @@ | |||
| package com.iformall.config; | |||
| import com.iformall.plugin.MyBatisItercepters; | |||
| import com.iformall.plugin.MyBatisPlus; | |||
| import com.iformall.plugin.shard.impl.EnumShardingRule; | |||
| import com.iformall.plugin.shard.impl.ShardingSphere; | |||
| import com.iformall.plugin.shard.plugin.ShardingSpherePlugin; | |||
| @@ -15,7 +17,12 @@ import java.util.Properties; | |||
| public class MyBatisConfiguration extends BaseMyBatisConfiguration { | |||
| @Bean | |||
| public ShardingSpherePlugin shardingSpherePlugin() { | |||
| return baseShardingSpherePlugin(); | |||
| } | |||
| public MyBatisItercepters intercepters() { | |||
| MyBatisItercepters intercepters = new MyBatisItercepters(); | |||
| List<MyBatisPlus> plugins = new ArrayList<MyBatisPlus>(); | |||
| plugins.add(baseShardingSpherePlugin()); | |||
| intercepters.setPlugins(plugins); | |||
| return intercepters; | |||
| } | |||
| } | |||
| @@ -1,5 +1,7 @@ | |||
| package com.iformall.config; | |||
| import com.iformall.plugin.MyBatisItercepters; | |||
| import com.iformall.plugin.MyBatisPlus; | |||
| import com.iformall.plugin.shard.impl.EnumShardingRule; | |||
| import com.iformall.plugin.shard.impl.ShardingSphere; | |||
| import com.iformall.plugin.shard.plugin.ShardingSpherePlugin; | |||
| @@ -15,7 +17,12 @@ import java.util.Properties; | |||
| public class MyBatisConfiguration extends BaseMyBatisConfiguration { | |||
| @Bean | |||
| public ShardingSpherePlugin shardingSpherePlugin() { | |||
| return baseShardingSpherePlugin(); | |||
| } | |||
| public MyBatisItercepters intercepters() { | |||
| MyBatisItercepters intercepters = new MyBatisItercepters(); | |||
| List<MyBatisPlus> plugins = new ArrayList<MyBatisPlus>(); | |||
| plugins.add(baseShardingSpherePlugin()); | |||
| intercepters.setPlugins(plugins); | |||
| return intercepters; | |||
| } | |||
| } | |||
| @@ -0,0 +1,54 @@ | |||
| package com.iformall.plugin; | |||
| import org.apache.ibatis.executor.statement.StatementHandler; | |||
| import org.apache.ibatis.plugin.*; | |||
| import org.apache.ibatis.session.ResultHandler; | |||
| import java.sql.Connection; | |||
| import java.util.List; | |||
| /** | |||
| *共享数据库的多租户系统实现 | |||
| * Created by Stormeye on 2018/10/22. | |||
| */ | |||
| @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 MyBatisItercepters implements Interceptor { | |||
| private List<MyBatisPlus> plugins; | |||
| public MyBatisItercepters(){ | |||
| } | |||
| public void setPlugins(List<MyBatisPlus> plugins) { | |||
| this.plugins = plugins; | |||
| } | |||
| public Object intercept(Invocation invocation) throws Throwable { | |||
| if (null != plugins && plugins.size() > 0 ) { | |||
| // 能从cookie或者session获取到tenantId | |||
| for (MyBatisPlus plug: plugins) { | |||
| plug.intercept(invocation); | |||
| } | |||
| } | |||
| return invocation.proceed(); | |||
| } | |||
| /** | |||
| * 生成代理对象 | |||
| * | |||
| * @param target | |||
| * @return | |||
| */ | |||
| @Override | |||
| public Object plugin(Object target) { | |||
| if (target instanceof StatementHandler) { | |||
| return Plugin.wrap(target, this); | |||
| } else { | |||
| return target; | |||
| } | |||
| } | |||
| } | |||
| @@ -0,0 +1,8 @@ | |||
| package com.iformall.plugin; | |||
| import org.apache.ibatis.plugin.Invocation; | |||
| public interface MyBatisPlus { | |||
| public void intercept(Invocation invocation) throws Throwable; | |||
| } | |||
| @@ -1,4 +1,4 @@ | |||
| package com.iformall.plugin; | |||
| package com.iformall.plugin.autoTenantId; | |||
| import net.sf.jsqlparser.JSQLParserException; | |||
| import net.sf.jsqlparser.expression.Expression; | |||
| @@ -32,6 +32,8 @@ import org.apache.ibatis.reflection.SystemMetaObject; | |||
| import org.apache.ibatis.session.Configuration; | |||
| import org.apache.ibatis.session.ResultHandler; | |||
| import com.iformall.plugin.MyBatisPlus; | |||
| import java.sql.Connection; | |||
| import java.util.Arrays; | |||
| import java.util.List; | |||
| @@ -41,13 +43,7 @@ import java.util.Properties; | |||
| *共享数据库的多租户系统实现 | |||
| * Created by Stormeye on 2018/10/22. | |||
| */ | |||
| @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 MultiTenancy implements Interceptor { | |||
| public class MultiTenancy implements MyBatisPlus { | |||
| //获取tenantId的接口 | |||
| private TenantInfo tenantInfo; | |||
| @@ -91,12 +87,11 @@ public class MultiTenancy implements Interceptor { | |||
| public MultiTenancy(){ | |||
| } | |||
| public Object intercept(Invocation invocation) throws Throwable { | |||
| public void intercept(Invocation invocation) throws Throwable { | |||
| if (getTenantInfo().getTenantId() != null || getTenantInfo().getParentTenantId() != null) { | |||
| // 能从cookie或者session获取到tenantId | |||
| mod(invocation); | |||
| } | |||
| return invocation.proceed(); | |||
| } | |||
| /** | |||
| @@ -298,21 +293,6 @@ public class MultiTenancy implements Interceptor { | |||
| return insertStatement.toString(); | |||
| } | |||
| /** | |||
| * 生成代理对象 | |||
| * | |||
| * @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} | |||
| @@ -1,4 +1,4 @@ | |||
| package com.iformall.plugin; | |||
| package com.iformall.plugin.autoTenantId; | |||
| import org.apache.ibatis.mapping.MappedStatement; | |||
| @@ -8,6 +8,7 @@ public class ShardingTableExcuter { | |||
| private static Map<String,ShardingSphere> tableMap = new ConcurrentHashMap<String, ShardingSphere>(); | |||
| public static Map<String,ShardingSphere> setTableSharding(String table ,ShardingSphere ss) { | |||
| System.out.println(">>>>>>>>> tablemap >>>>"+tableMap); | |||
| tableMap.put(table, ss); | |||
| return tableMap; | |||
| } | |||
| @@ -33,6 +33,7 @@ import org.apache.ibatis.reflection.SystemMetaObject; | |||
| import org.apache.ibatis.session.Configuration; | |||
| import org.apache.ibatis.session.ResultHandler; | |||
| import com.iformall.plugin.MyBatisPlus; | |||
| import com.iformall.plugin.shard.annotation.IgnoreSharding; | |||
| import com.iformall.plugin.shard.impl.ShardingRuleExcuter; | |||
| import com.iformall.plugin.shard.impl.ShardingSphere; | |||
| @@ -49,13 +50,7 @@ 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 class ShardingSpherePlugin implements MyBatisPlus { | |||
| public ShardingSpherePlugin(){ | |||
| @@ -78,32 +73,17 @@ public class ShardingSpherePlugin implements Interceptor { | |||
| 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) { | |||
| System.out.println(">>>>>"+ss.getTableName()+". sharing:"+ss); | |||
| ShardingTableExcuter.setTableSharding(ss.getTableName(), ss); | |||
| } | |||
| } | |||
| @@ -118,12 +98,20 @@ public class ShardingSpherePlugin implements Interceptor { | |||
| return null; | |||
| } | |||
| public Object intercept(Invocation invocation) throws Throwable { | |||
| public void intercept(Invocation invocation) throws Throwable { | |||
| if (!ShardingTableExcuter.hasShardingConfig()) { | |||
| return invocation.proceed(); | |||
| return ; | |||
| } | |||
| StatementHandler statementHandler = (StatementHandler) invocation.getTarget(); | |||
| MetaObject metaStatementHandler = SystemMetaObject.forObject(statementHandler); | |||
| Object object = null; | |||
| /*分离代理对象链(由于目标类可能被多个拦截器[插件]拦截,从而形成多次代理,通过循环可以分离出最原始的目标类)*/ | |||
| while(metaStatementHandler.hasGetter("h")){ | |||
| object = metaStatementHandler.getValue("h"); | |||
| metaStatementHandler = SystemMetaObject.forObject(object); | |||
| } | |||
| MappedStatement ms = (MappedStatement) metaStatementHandler.getValue("delegate.mappedStatement"); | |||
| String id = ms.getId(); | |||
| String className = id.substring(0, id.lastIndexOf(".")); | |||
| @@ -140,7 +128,6 @@ public class ShardingSpherePlugin implements Interceptor { | |||
| metaStatementHandler.setValue("delegate.boundSql.sql", newSQL); | |||
| } | |||
| } | |||
| return invocation.proceed(); | |||
| } | |||
| private String buildTable(BoundSql boundSql,Object parameterObject) throws JSQLParserException { | |||
| @@ -2,6 +2,8 @@ package com.iformall.plugin; | |||
| import org.apache.ibatis.mapping.MappedStatement; | |||
| import com.iformall.plugin.autoTenantId.TenantInfo; | |||
| /** | |||
| * 自行实现TenantInfo获取 | |||
| * Created by Meara on 2016/3/20. | |||