多租户
多个用户间使用同一套程序,但每个用户之间实现数据隔离

方法一
:在 Mapper 的自定义方法上添加注解 @SqlParser(filter = true),在查询的时候不需要添加租户信息

@SqlParser(filter=true)
IPage<CognitiveCardPackagePageData> selectCognitiveCardPackagePage(Page page,@Param("query") OkyaCognitiveCardPackagePageListQueryData req);

方法二
@Slf4j
@Configuration
@MapperScan(value = {"com.**.dao"})
public class MyBatisPlusConfig {

/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor(DynamicTableNameParser dynamicTableNameParser) {
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
List<ISqlParser> sqlParserList = new ArrayList<>();
// 添加租户sql解析链,如果是开发业务系统,可以去掉这个配置,不去也可以。
TenantSqlParser tenantSqlParser = new TenantSqlParser();
tenantSqlParser.setTenantHandler(tenantHandler());
sqlParserList.add(tenantSqlParser);
// 设置sql解析链
paginationInterceptor.setSqlParserList(sqlParserList);
// 设置过滤器链
paginationInterceptor.setSqlParserFilter(sqlParserFilter());
return paginationInterceptor;
}

@Bean
public TenantHandler tenantHandler() {
return new TenantHandler() {
@Override
public Expression getTenantId() {
// 返回当前登录人员的租户ID
return new LongValue(
RbacUserUtils.getCurrentTenantId());
}
@Override
public String getTenantIdColumn() {
return "tenant_id";
}
@Override
public boolean doTableFilter(String tableName) {
return false;
}
};
}

/**
* 如何有自定义无租户的查询,可以在此过滤
* @return SQL解析过滤
*/
@Bean
public ISqlParserFilter sqlParserFilter() {
return metaObject -> {
// 如果在程序中已经手动设置了tenant_id,此处就过滤
Object boundSql = metaObject.getValue("boundSql");
String sql = String.valueOf(ReflectionUtils
.getFieldValue(boundSql, "sql"));
return StringUtils.containsIgnoreCase(sql, "insert")
&& StringUtils.containsIgnoreCase(sql, "tenant_id");
};
}
}

注意此处的sqlParserFilter的配置,在没有此配置之前,无论你是插入、更新、查询,mybatis plus 就带上tenant_id,这个tenant_id不是一直都是有的,在注册租户的时候就是没有的,不过这也没有关系,我们可以当系统当前没有登录的租户时就将租户的ID统一设置为-1(一个统一特殊的数值),有登录的用户就获取该用户的租户ID获租户自己的ID。这是没有问题的。

Failed to process, please exclude the tableName or statementId.--Mybatis-Plus的更多相关文章

  1. Cleanup failed to process the following paths错误的解决

    在使用TortoiseSVN工具执行Cleanup操作时经常出现Cleanup failed to process the following paths的错误,具体如下图: 网上搜索了一下,找到了解 ...

  2. SVN Cleanup failed to process the following paths错误的解决

    在使用TortoiseSVN工具执行Cleanup操作时经常出现Cleanup failed to process the following paths的错误,具体如下图: 网上搜索了一下,找到了解 ...

  3. svn-clearup 报错的处理(Cleanup failed to process the following paths...)

    在使用 svn 客户端执行操作失败后,执行 Clean up 操作也报错:Cleanup failed to process the following paths... ,一直不知道是什么原因.通常 ...

  4. 解决SVN Cleanup时遇到错误信息:Cleanup failed to process the following paths:xxxxxxx Previous operation has not finished: run 'cleanup' if it was interrupted Please execute the 'Cleanup' command.

    解决SVN Cleanup时遇到错误信息:Cleanup failed to process the following paths:xxxxxxx Previous operation has no ...

  5. WebHost failed to process a request.Memory gates checking failed because the free memory (140656640 bytes) is less than 5% of total memory

    WebHost failed to process a request. Sender Information: System.ServiceModel.ServiceHostingEnvironme ...

  6. react native报错处理com.android.build.api.transform.TransformException: com.android.builder.dexing.DexArchiveBuilderException: com.android.builder.dexing.DexArchiveBuilderException: Failed to process

    背景:最近准备在使用react-native开发的app中接入友盟,来进行用户行为统计,分享,授权登录等操作. 在使用的过程中,遇到了一些错误信息,在此记录一下. 在修改android目录下的buil ...

  7. SVN 执行cleanup报错:Cleanup failed to process the following paths

    SVN 执行cleanup报错:Cleanup failed to process the following paths 先来说下这个错误的原因:用SVN在使用过程中,各种原因中途取消或中断,导致需 ...

  8. SMTP Failed on Process Scheduler

    If you are seeing messages like this in your message log when running a process through the process ...

  9. SVN:Cleanup failed to process the following paths

    频繁使用SVN,于是乎玩坏了.用了一下clearup,结果爆了如题错误.查了一下,是有文件被加锁了,位置在项目根目录 .svn下的wc.db 里,需用专门工具才能看到里面.就是个数据库,里面有很多表. ...

随机推荐

  1. 记一次用python 的ConfigParser读取配置文件编码报错

    记一次用python 的ConfigParser读取配置文件编码报错 ...... raise MissingSectionHeaderError(fpname, lineno, line)Confi ...

  2. LeetCode 019 Remove Nth Node From End of List

    题目描述:Remove Nth Node From End of List Given a linked list, remove the nth node from the end of list ...

  3. C#(三)基础篇—方法,递归,条件分支,循环,三元操作符

    C# 本随笔为个人复习巩固知识用,多从书上总结与理解得来,如有错误麻烦指正 2020-12-03 1.方法 static void Main(string[] args) { float Sum(fl ...

  4. PyQt学习随笔:QtDesigner ListView控件列表项的初始化

    在QtDesigner中设计的界面中添加ListView控件后,是没办法添加需要在ListView控件中显示的列表项.由于ListView控件只是一个展示列表项的视图控件,实现了界面与数据的分离,其要 ...

  5. 深入剖析HashMap

    前言 很高兴遇见你~ HashMap是一个非常重要的集合,日常使用也非常的频繁,同时也是面试重点.本文并不打算讲解基础的使用api,而是深入HashMap的底层,讲解关于HashMap的重点知识.需要 ...

  6. Java集合源码分析(四)——Vector

    简介 Vector 是矢量队列,它是JDK1.0版本添加的类.继承于AbstractList,实现了List, RandomAccess, Cloneable这些接口. 和ArrayList不同,Ve ...

  7. 数据结构—— Trie (前缀树)

    实现一个 Trie (前缀树),包含 插入, 查询, 和 查询前缀这三个操作. Trie trie = new Trie(); trie.insert("apple"); trie ...

  8. spark中map和mapPartitions算子的区别

    区别: 1.map是对rdd中每一个元素进行操作 2.mapPartitions是对rdd中每个partition的迭代器进行操作 mapPartitions优点: 1.若是普通map,比如一个par ...

  9. Windows10自带截屏快捷键

    Windows10自带截屏快捷键使用方法大全我们知道,QQ和微信以及第三方浏览器等软件都支持截图功能,但是这个都是基于软件的一个功能,如果我们不打开这些软件的话,就不能实现截图功能,但其实window ...

  10. Oracle 要慌了!华为终于开源了自家的 Huawei JDK——毕昇 JDK!

    没错,自阿里.腾讯之后,华为也终于开源了自家的 JDK--毕昇 JDK! 免费!免费!免费!!! Oracle 要慌了? 毕昇 JDK 毕昇 JDK 是华为内部 OpenJDK 定制版 Huawei ...