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

方法一
:在 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. 01-Python里字符串的常用操作方法--replace()函数

    1. replace()  函数 作用: 替换字符串 语法: replace('需要替换的子串', '新的子串', '替换的次数(可不传)') # 当替换次数不传时,默认全部替换 实例一: mystr ...

  2. VMware Workstation Pro 16 官方正式版下载(含密钥)

    VMware官方网站 https://www.vmware.com VMware Workstation Pro已于近日更新.毫无疑问,这可能是Windows系统上最强大最好用的虚拟机! VMware ...

  3. SQL Server 索引碎片整理

    索引碎片整理的四种方法: 1)删除索引并重建 2)使用 DROP_EXISTING 语句重建索引 3)使用 ALTER INDEX REBUILD 语句重建索引 4)使用 ALTER INDEX RE ...

  4. AlanShan数据库课程设计报告

    目    录 1.绪论.... 2 1.1前言... 2 1.2社会背景... 2 1.3超市背景... 3 2.系统可行性研究.... 4 2.1 技术可行性研究... 4 2.2 经济可行性研究. ...

  5. 手写koa-static源码,深入理解静态服务器原理

    这篇文章继续前面的Koa源码系列,这个系列已经有两篇文章了: 第一篇讲解了Koa的核心架构和源码:手写Koa.js源码 第二篇讲解了@koa/router的架构和源码:手写@koa/router源码 ...

  6. 转:python提取浏览器Cookie

    在用浏览器进行网页访问时,会向网页所在的服务器发送http协议的GET或者POST等请求,在请求中除了指定所请求的方法以及URI之外,后面还跟随着一段Request Header.Request He ...

  7. PyQt(Python+Qt)学习随笔:QListWidget插入多项的insertItems方法

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 除了insertItem方法能插入项外,QListWidget支持一次插入多个项,对应的方法就是in ...

  8. 对flask的学习

    任务需求:一个登录,注册页面 任务环境:pycharm 2018 专业版,python3.7,win 10专业版 ------------------------------------------- ...

  9. 【面试】关于get和post两种方法的不同。

    最近在面试题和笔试题中经常会看到这道题,所以打算系统的整理一下. 一般标准的答案是这样的. GET在浏览器回退时是无害的,而POST会再次提交请求(浏览器应该告知用户数据会被重新提交). GET产生的 ...

  10. watch监听对象的属性

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...