package com.ruoyi.framework.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement; 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.handler.TenantLineHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import com.ruoyi.common.extension.MpSqlInjector; import lombok.AllArgsConstructor;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue; @AllArgsConstructor
@EnableTransactionManagement(proxyTargetClass = true)
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new TenantLineHandler() {
// manager_id = 1088248166370832385 // 获取租户 ID 值表达式,只支持单个 ID 值
@Override
public Expression getTenantId() {
// return new LongValue(1088248166370832385L);
return new LongValue(1L);
} // 这是 default 方法,默认返回 false 表示所有表都需要拼多租户条件,
// 这里设置 role表不需要该条件
@Override
public boolean ignoreTable(String tableName) {
// if ("role".equals(tableName)) {
// return true;
// }
// return false;
if (tableName.substring(0, 2).equals("at"))
return false;
return true;
} @Override
public String getTenantIdColumn() {
return "tenantid";
}
})); // interceptor.addInnerInterceptor(tenantLineInnerInterceptor());
// 如果用了分页插件注意先 add TenantLineInnerInterceptor 再 add
// PaginationInnerInterceptor
// 用了分页插件必须设置 MybatisConfiguration#useDeprecatedExecutor = false
// 分页插件
// interceptor.addInnerInterceptor(paginationInnerInterceptor()); // 乐观锁插件
interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor());
// 阻断插件
interceptor.addInnerInterceptor(blockAttackInnerInterceptor());
return interceptor;
} @SuppressWarnings("deprecation")
@Bean
public ConfigurationCustomizer configurationCustomizer() {
return configuration -> configuration.setUseDeprecatedExecutor(Boolean.FALSE);
} @Bean
public MpSqlInjector easySqlInjector() {
return new MpSqlInjector();
} /**
* 分页插件,自动识别数据库类型 https://baomidou.com/guide/interceptor-pagination.html
*/
public PaginationInnerInterceptor paginationInnerInterceptor() {
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
// 设置数据库类型为mysql
paginationInnerInterceptor.setDbType(DbType.MYSQL);
// 设置最大单页限制数量,默认 500 条,-1 不受限制
paginationInnerInterceptor.setMaxLimit(-1L);
return paginationInnerInterceptor;
} /**
* 乐观锁插件 https://baomidou.com/guide/interceptor-optimistic-locker.html
*/
public OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor() {
return new OptimisticLockerInnerInterceptor();
} /**
* 如果是对全表的删除或更新操作,就会终止该操作
* https://baomidou.com/guide/interceptor-block-attack.html
*/
public BlockAttackInnerInterceptor blockAttackInnerInterceptor() {
return new BlockAttackInnerInterceptor();
}
/**
* sql性能规范插件(垃圾SQL拦截) 如有需要可以启用
*/
// public IllegalSQLInnerInterceptor illegalSQLInnerInterceptor() {
// return new IllegalSQLInnerInterceptor();
// }
/**
* 自定义主键策略 https://baomidou.com/guide/id-generator.html
*/
// @Bean
// public IdentifierGenerator idGenerator() {
// return new CustomIdGenerator();
// }
/**
* 元对象字段填充控制器 https://baomidou.com/guide/auto-fill-metainfo.html
*/
// @Bean
// public MetaObjectHandler metaObjectHandler() {
// return new MyMetaObjectHandler();
// }
/**
* sql注入器配置 https://baomidou.com/guide/sql-injector.html
*/
// @Bean
// public ISqlInjector sqlInjector() {
// return new DefaultSqlInjector();
// }
/**
* TenantLineInnerInterceptor 多租户插件 本项目已经实现完整的多租户功能,但应用不广泛,基本框架中删除
*/
// @Bean
// public TenantLineInnerInterceptor tenantLineInnerInterceptor()
// {
// return new TenantLineInnerInterceptor(new TenantLineHandler()
// {
// /**
// * 获取租户ID
// * @return
// */
// @Override
// public Expression getTenantId()
// {
// String tenant = TenantContextHolder.getTenantId();
// if (tenant != null)
// {
// return new StringValue(TenantContextHolder.getTenantId());
// }
// return new StringValue("99999");
// }
//
// /**
// * 获取多租户的字段名
// * @return String
// */
// @Override
// public String getTenantIdColumn()
// {
// return tenantProperties.getColumn();
// }
//
// /**
// * 过滤不需要根据租户隔离的表
// * 这是 default 方法,默认返回 false 表示所有表都需要拼多租户条件
// * @param tableName 表名
// */
// @Override
// public boolean ignoreTable(String tableName)
// {
// return tenantProperties.getIgnores().stream().anyMatch((t) ->
// t.equalsIgnoreCase(tableName));
// }
// });
// }
}
package com.ruoyi.framework.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement; 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.handler.TenantLineHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import com.ruoyi.common.extension.MpSqlInjector; import lombok.AllArgsConstructor;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue; @AllArgsConstructor
@EnableTransactionManagement(proxyTargetClass = true)
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new TenantLineHandler() {
// manager_id = 1088248166370832385 // 获取租户 ID 值表达式,只支持单个 ID 值
@Override
public Expression getTenantId() {
// return new LongValue(1088248166370832385L);
return new LongValue(1L);
} // 这是 default 方法,默认返回 false 表示所有表都需要拼多租户条件,
// 这里设置 role表不需要该条件
@Override
public boolean ignoreTable(String tableName) {
// if ("role".equals(tableName)) {
// return true;
// }
// return false;
if (tableName.substring(0, 2).equals("at"))
return false;
return true;
} @Override
public String getTenantIdColumn() {
return "tenantid";
}
})); // interceptor.addInnerInterceptor(tenantLineInnerInterceptor());
// 如果用了分页插件注意先 add TenantLineInnerInterceptor 再 add
// PaginationInnerInterceptor
// 用了分页插件必须设置 MybatisConfiguration#useDeprecatedExecutor = false
// 分页插件
// interceptor.addInnerInterceptor(paginationInnerInterceptor()); // 乐观锁插件
interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor());
// 阻断插件
interceptor.addInnerInterceptor(blockAttackInnerInterceptor());
return interceptor;
} @SuppressWarnings("deprecation")
@Bean
public ConfigurationCustomizer configurationCustomizer() {
return configuration -> configuration.setUseDeprecatedExecutor(Boolean.FALSE);
} @Bean
public MpSqlInjector easySqlInjector() {
return new MpSqlInjector();
} /**
* 分页插件,自动识别数据库类型 https://baomidou.com/guide/interceptor-pagination.html
*/
public PaginationInnerInterceptor paginationInnerInterceptor() {
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
// 设置数据库类型为mysql
paginationInnerInterceptor.setDbType(DbType.MYSQL);
// 设置最大单页限制数量,默认 500 条,-1 不受限制
paginationInnerInterceptor.setMaxLimit(-1L);
return paginationInnerInterceptor;
} /**
* 乐观锁插件 https://baomidou.com/guide/interceptor-optimistic-locker.html
*/
public OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor() {
return new OptimisticLockerInnerInterceptor();
} /**
* 如果是对全表的删除或更新操作,就会终止该操作
* https://baomidou.com/guide/interceptor-block-attack.html
*/
public BlockAttackInnerInterceptor blockAttackInnerInterceptor() {
return new BlockAttackInnerInterceptor();
}
/**
* sql性能规范插件(垃圾SQL拦截) 如有需要可以启用
*/
// public IllegalSQLInnerInterceptor illegalSQLInnerInterceptor() {
// return new IllegalSQLInnerInterceptor();
// }
/**
* 自定义主键策略 https://baomidou.com/guide/id-generator.html
*/
// @Bean
// public IdentifierGenerator idGenerator() {
// return new CustomIdGenerator();
// }
/**
* 元对象字段填充控制器 https://baomidou.com/guide/auto-fill-metainfo.html
*/
// @Bean
// public MetaObjectHandler metaObjectHandler() {
// return new MyMetaObjectHandler();
// }
/**
* sql注入器配置 https://baomidou.com/guide/sql-injector.html
*/
// @Bean
// public ISqlInjector sqlInjector() {
// return new DefaultSqlInjector();
// }
/**
* TenantLineInnerInterceptor 多租户插件 本项目已经实现完整的多租户功能,但应用不广泛,基本框架中删除
*/
// @Bean
// public TenantLineInnerInterceptor tenantLineInnerInterceptor()
// {
// return new TenantLineInnerInterceptor(new TenantLineHandler()
// {
// /**
// * 获取租户ID
// * @return
// */
// @Override
// public Expression getTenantId()
// {
// String tenant = TenantContextHolder.getTenantId();
// if (tenant != null)
// {
// return new StringValue(TenantContextHolder.getTenantId());
// }
// return new StringValue("99999");
// }
//
// /**
// * 获取多租户的字段名
// * @return String
// */
// @Override
// public String getTenantIdColumn()
// {
// return tenantProperties.getColumn();
// }
//
// /**
// * 过滤不需要根据租户隔离的表
// * 这是 default 方法,默认返回 false 表示所有表都需要拼多租户条件
// * @param tableName 表名
// */
// @Override
// public boolean ignoreTable(String tableName)
// {
// return tenantProperties.getIgnores().stream().anyMatch((t) ->
// t.equalsIgnoreCase(tableName));
// }
// });
// }
}

mybatis-plus 多租户的更多相关文章

  1. Mybatis Plus 多租户架构实现(完美教程)

    一.背景介绍 多租户技术或称多重租赁技术,简称SaaS,是一种软件架构技术,是实现如何在多用户环境下(此处的多用户一般是面向企业用户)共用相同的系统或程序组件,并且可确保各用户间数据的隔离性. 简单讲 ...

  2. Mybatis Plus使用租户过滤无效解决方案

    异常内容: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.Pe ...

  3. MyBatis多租户隔离插件开发

    在SASS的大潮流下,相信依然存在很多使用一个数据库为多个租户提供服务的场景,这个情况下一般是多个租户共用同一套表通过sql语句级别来隔离不同租户的资源,比如设置一个租户标识字段,每次查询的时候在后面 ...

  4. 多租户实现之基于Mybatis,Mycat的共享数据库,共享数据架构

    前言 SaaS模式是什么? 传统的软件模式是在开发出软件产品后,需要去客户现场进行实施,通常部署在局域网,这样开发.部署及维护的成本都是比较高的. 现在随着云服务技术的蓬勃发展,就出现了SaaS模式. ...

  5. mybatis运行时拦截ParameterHandler注入参数

    在实现多租户系统时,每个租户下的用户,角色,权限,菜单都是独立的,每张表里都有租户Id字段 (tenantId),每次做数据库操作的时候都需要带上这个字段,很烦. 现在的需求就是在mybatis向sq ...

  6. 两种实现方式mycat多租户,枚举分片,注解拦截

    第一种: 优点:支持进一步分片 缺点:schema配置繁琐 注解式  /*!mycat:schema=[schemaName] */   注意:这在navicat 里面是会报错的,请用命令行登陆myc ...

  7. 使用mybatis plus 操作数据库

    mybatis plus 是基于mybatis 的一个增强包,比 mybatis 更加容易使用. 特点: 1.分页支持 2.支持自定义查询. 3.简单的情况下,不需要写map.xml 文件 4.支持租 ...

  8. Spring Boot + Mybatis 实现动态数据源

    动态数据源 在很多具体应用场景的时候,我们需要用到动态数据源的情况,比如多租户的场景,系统登录时需要根据用户信息切换到用户对应的数据库.又比如业务A要访问A数据库,业务B要访问B数据库等,都可以使用动 ...

  9. SpringBoot集成Mybatis并具有分页功能PageHelper

    SpringBoot集成Mybatis并具有分页功能PageHelper   环境:IDEA编译工具   第一步:生成测试的数据库表和数据   SET FOREIGN_KEY_CHECKS=0;   ...

  10. mybatis拦截器使用

    目录 mybatis 拦截器接口Interceptor spring boot + mybatis整合 创建自己的拦截器MyInterceptor @Intercepts注解 mybatis拦截器入门 ...

随机推荐

  1. JS数据结构与算法-数组结构

    数组结构 几乎所有的编程语言都原生支持数组类型,因为数组是最简单的内存数据结构. 数组通常情况下用于存储一系列同一种数据类型的值. 但在JavaScript里,也可以在数组中保存不同类型的值. 但我们 ...

  2. SpringBoot启动流程源码分析

    前言 SpringBoot项目的启动流程是很多面试官面试中高级Java程序员喜欢问的问题.这个问题的答案涉及到了SpringBoot工程中的源码,也许我们之前看过别的大牛写过的有关SpringBoot ...

  3. Conda 环境移植 (两种方式)

    ------------------------方法一------------------------ 优点: 在原机器上需要进行的操作较少,且除了conda不需要其余的库来支撑:需要传输的文件小,操 ...

  4. Go1.20 新版覆盖率方案解读

    玩过Go覆盖率的同学当有所了解,Go的覆盖率方案最初的设计目标仅是针对单测场景,导致其局限性很大.而为了适配更多的场景,行业内各种博客.插件.黑科技介绍也层出不穷.当然,过去我们也开源过Go系统测试覆 ...

  5. Blender修改视野范围

    首先,我不是专门的建模人员.但是有时候会拿到建模人员的制作的模型导入进行修改. 比如简单的删除某个模型,调整模型的尺寸. 还有就是调整模型的建模中心点,这点有时候显得特别重要,模型的中心点偏离较大会给 ...

  6. 【每日一题】【双指针、位运算】2022年2月3日-NC103 反转字符串

    描述 写出一个程序,接受一个字符串,然后输出该字符串反转后的字符串.(字符串长度不超过1000) 答案:双指针 import java.util.*; public class Solution { ...

  7. 开发一个最简单的iOS App

    开发一个最简单的iOS App 大家好,我是孜孜不倦学习的Zhangbeihai. 上月底我组织了[组队学习]TensorFlow 入门课程(中文) ,截至目前有300多同学加入.主要就是 Tenso ...

  8. Dijkstra 算法说明与实现

    Dijkstra 算法说明与实现 作者:Grey 原文地址: 博客园:Dijkstra 算法说明与实现 CSDN:Dijkstra 算法说明与实现 问题描述 问题:给定出发点,出发点到所有点的距离之和 ...

  9. 多表查询两种方法、可视化软件navicat、python操作mysql、pymysql模块

    目录 多表查询的思路 多表查询的两种方法 小知识点补充数说明 可视化软件Navicat 安装教程 数据库常用操作 多表查询练习题 python 操作MySQL pymysql补充说明 Non-grou ...

  10. pytest.ini配置文件格式

    [pytest] # 命令行参数,用空格分隔 addopts = -v -n=2 # 配置测试用例所在文件夹 testpaths = ./pytest_1 # 配置需要执行的模块文件名称 python ...