广告
返回顶部
首页 > 资讯 > 后端开发 > Python >详解基于MybatisPlus两步实现多租户方案
  • 627
分享到

详解基于MybatisPlus两步实现多租户方案

2024-04-02 19:04:59 627人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

1.定义一个TenantLineHandler的实现类: import com.baomidou.mybatisplus.extension.plugins.handler.Te

1.定义一个TenantLineHandler的实现类:


import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.Google.common.collect.Lists;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
 
import java.util.List;
 

public class CustomTenantLineHandler implements TenantLineHandler {
 
    
    private final static List<String> IGNORE_TABLE_NAMES = Lists.newArrayList(
            "t_country",
            "t_language"
    );
 
    
    @Override
    public Expression getTenantId() {
        return new LongValue(1L);
    }
 
    
    @Override
    public String getTenantIdColumn() {
        return "tenant_id";
    }
 
    
    @Override
    public boolean ignoreTable(String tableName) {
        return IGNORE_TABLE_NAMES.contains(tableName);
    }
}

2.定义MybatisPlusConfig配置类将多租户插件生效:


import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import com.bzcst.bop.component.mybatis.config.handler.AutoFillMetaObjectHandler;
import com.bzcst.bop.component.mybatis.config.handler.CustomTenantLineHandler;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 

@Configuration
@MapperScan("scan.your.mapper.package")
public class MybatisPlusConfig {
 
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 多租户插件(注意:这个一定要放在最上面)
        interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new CustomTenantLineHandler()));
        // 分页插件
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.Mysql));
        return interceptor;
    }
 
    @Bean
    public ConfigurationCustomizer configurationCustomizer() {
        return configuration -> configuration.setUseDeprecatedExecutor(false);
    }
 
    @Bean
    public AutoFillMetaObjectHandler fillMetaObjectHandler() {
        return new AutoFillMetaObjectHandler();
    }
}

测试


@Test
    public void select() {
        Role role = roleService.getById(40L);
        System.out.println(role);
    }

Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@2fedfff1] was not reGIStered for synchronization because synchronization is not active
Original SQL: SELECT id,tenant_id,frame_id,name,type,description,meta_created,meta_updated,meta_logic_flag FROM t_sec_role WHERE id=? 
parser sql: SELECT id, tenant_id, frame_id, name, type, description, meta_created, meta_updated, meta_logic_flag FROM t_sec_role WHERE id = ? AND tenant_id = 1
2021-04-26 14:58:55.534  INFO 24980 --- [           main] com.zaxxer.hikari.HikariDataSource       : DatebookHikariCP - Starting...
2021-04-26 14:58:55.903  INFO 24980 --- [           main] com.zaxxer.hikari.HikariDataSource       : DatebookHikariCP - Start completed.
JDBC Connection [HikariProxyConnection@1100660981 wrapping com.mysql.cj.jdbc.ConnectionImpl@628fa8ea] will not be managed by Spring
==>  Preparing: SELECT id, tenant_id, frame_id, name, type, description, meta_created, meta_updated, meta_logic_flag FROM t_sec_role WHERE id = ? AND tenant_id = 1
==> Parameters: 40(Long)
<==    Columns: id, tenant_id, frame_id, name, type, description, meta_created, meta_updated, meta_logic_flag
<==        Row: 40, 1, 123, 一个角色啊, 2, haha, 2021-04-26 14:07:42, 2021-04-26 14:07:42, 1
<==      Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@2fedfff1]
Role(id=40, sasTenantId=1, orgFrameId=123, name=一个角色啊, type=2, description=haha, metaCreated=Mon Apr 26 14:07:42 CST 2021, metaUpdated=Mon Apr 26 14:07:42 CST 2021, metaLogicFlag=1)

可以看到查询语句后面拼接了tenant_id = 1


==>  Preparing: SELECT id, tenant_id, frame_id, name, type, description, meta_created, meta_updated, meta_logic_flag FROM t_sec_role WHERE id = ? AND tenant_id = 1

注意:如果表中没有定义tenant_id会报错,不需要添加多租户的表配置到CustomTenantLineHandler 中的IGNORE_TABLE_NAMES集合

到此这篇关于详解基于MybatisPlus两步实现多租户方案的文章就介绍到这了,更多相关MybatisPlus多租户内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: 详解基于MybatisPlus两步实现多租户方案

本文链接: https://www.lsjlt.com/news/124803.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作