解决Mybatisplus多租户插件集成shardingjdbc后,多表关联无法按租户id分片的问题

一、背景

1、项目集成Mybatisplus和shardingjdbc,按照租户id分库分表,单表没问题,在多表关联查询时租户id分片键不生效。

<dependency>
	<groupId>com.baomidou</groupId>
	<artifactId>mybatis-plus-extension</artifactId>
	<version>3.5.7</version>
</dependency>
<dependency>
    <groupId>org.apache.shardingsphere</groupId>
    <artifactId>shardingsphere-jdbc</artifactId>
    <version>5.5.1</version>
</dependency>

2、分片配置,项目是做的分库分表sys_employee_post是只分库不分表,sys_post表是分库分表

rules:
  - !SHARDING
    tables:
      sys_employee_post:
        actualDataNodes: ds_${0..1}.sys_employee_post
      sys_post:
        actualDataNodes: ds_${0..1}.sys_post_20${25..27}_${0..9}
        tableStrategy: 
          complex:
            shardingColumns: tenant_id, create_time
            shardingAlgorithmName: table_tenant_year_algorithm
    defaultDatabaseStrategy:
      standard:
        shardingColumn: tenant_id
        shardingAlgorithmName: database_algorithm
    shardingAlgorithms:
      database_algorithm:
        type: CLASS_BASED
        props:
          strategy: STANDARD
          algorithmClassName: com.lxdz.kyj.common.sharding.algorithm.TenantShardingAlgorithm
      table_tenant_year_algorithm:
        type: CLASS_BASED
        props:
          strategy: COMPLEX
          algorithmClassName: com.lxdz.kyj.common.sharding.algorithm.TenantYearTableShardingAlgorithm

3、执行的逻辑sql和实际sql日志,租户id按照算法应该是只分在01库的,结果执行时却发现两个库都路由到了,分析问题发现是Mybatisplus的多租户插件把租户条件是拼接在join on 后面的,而不是where后面,这种情况shardingjdbc是不会识别的。

    <select id="test" resultType="com.lxdz.kyj.admin.api.entity.SysPost">
		SELECT p.post_id,
			   p.post_name,
			   p.create_time
		FROM sys_employee_post up
		left join sys_post p on p.post_id = up.post_id
			WHERE p.del_flag = '0'
       <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
           and p.create_time between #{startTime} and #{endTime}
       </if>
   </select>

在这里插入图片描述

二、解决方案

重载Mybatisplus的多租户插件的Bean,将自定义的sql拼接租户id的逻辑修改,改为拼接在where之后。

    /**
     * 创建企业维护处理器对象
     *
     * @return 处理后的企业维护处理器
     */
    @Bean
    @ConditionalOnMissingBean
    public KYJTenantLineInnerInterceptor tenantLineInnerInterceptor(KyjTenantConfigProperties tenantConfigProperties) {
   
   
        KYJTenantLineInnerInterceptor tenantLineInnerInterceptor = new KYJTenantLineInnerInterceptor();
        tenantLineInnerInterceptor.setTenantLineHandler(new KyjTenantHandler(tenantConfigProperties));
        return tenantLineInnerInterceptor;
    }
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.select.FromItem;
import net.sf.jsqlparser.statement.select.Join;
import net.sf.jsqlparser.statement.select.ParenthesedFromItem;
import net.sf
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值