当前位置:   article > 正文

MybatisPlus多租户架构实现过程_com/baomidou/mybatisplus/core/parser/isqlparserfil

com/baomidou/mybatisplus/core/parser/isqlparserfilter

1.引入依赖(pom.xml)

  1. <dependency>
  2. <groupId>com.baomidou</groupId>
  3. <artifactId>mybatis-plus-boot-starter</artifactId>
  4. <version>${mybatis-plus.version}</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.baomidou</groupId>
  8. <artifactId>mybatis-plus-generator</artifactId>
  9. <version>${mybatis-plus.version}</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>com.baomidou</groupId>
  13. <artifactId>mybatis-plus</artifactId>
  14. <version>${mybatis-plus.version}</version>
  15. </dependency>
  16. <dependency>
  17. <groupId>com.baomidou</groupId>
  18. <artifactId>mybatis-plus-dts</artifactId>
  19. <version>${mybatis-plus.version}</version>
  20. </dependency>

2.配置文件application.yml

多数据源

  1. spring:
  2. datasource:
  3. master:
  4. jdbc-url: jdbc:mysql://192.168.102.31:3306/test
  5. username: root
  6. password: 123456
  7. driver-class-name: com.mysql.jdbc.Driver
  8. slave1:
  9. jdbc-url: jdbc:mysql://192.168.102.56:3306/test
  10. username: pig # 只读账户
  11. password: 123456
  12. driver-class-name: com.mysql.jdbc.Driver
  13. slave2:
  14. jdbc-url: jdbc:mysql://192.168.102.36:3306/test
  15. username: pig # 只读账户
  16. password: 123456
  17. driver-class-name: com.mysql.jdbc.Driver

单数据源

  1. spring:
  2. datasource:
  3. name: db
  4. type: com.alibaba.druid.pool.DruidDataSource
  5. url: jdbc:mysql://127.0.0.1:3306/user?characterEncoding=utf-8
  6. username: root
  7. password: root
  8. driver-class-name: com.mysql.jdbc.Driver

3.maper层写法

继承BaseMapper<实体类>

  1. public interface TUserMapper extends BaseMapper<TUser> {
  2. }

4.配置Mybatis Plus的租户

  1. package com.fight.strive.sys.modules.mybatisplus.config;
  2. import com.baomidou.mybatisplus.core.parser.ISqlParser;
  3. import com.baomidou.mybatisplus.core.parser.ISqlParserFilter;
  4. import com.baomidou.mybatisplus.extension.parsers.DynamicTableNameParser;
  5. import com.baomidou.mybatisplus.extension.parsers.ITableNameHandler;
  6. import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
  7. import com.baomidou.mybatisplus.extension.plugins.tenant.TenantHandler;
  8. import com.baomidou.mybatisplus.extension.plugins.tenant.TenantSqlParser;
  9. import com.fight.strive.sys.modules.rbac.utils.RbacUserUtils;
  10. import com.fight.strive.sys.utils.ReflectionUtils;
  11. import com.fight.strive.sys.utils.StringUtils;
  12. import lombok.extern.slf4j.Slf4j;
  13. import net.sf.jsqlparser.expression.Expression;
  14. import net.sf.jsqlparser.expression.LongValue;
  15. import org.mybatis.spring.annotation.MapperScan;
  16. import org.springframework.context.annotation.Bean;
  17. import org.springframework.context.annotation.Configuration;
  18. import java.util.ArrayList;
  19. import java.util.HashMap;
  20. import java.util.List;
  21. import java.util.Map;
  22. @Slf4j
  23. @Configuration
  24. @MapperScan(value = {"com.**.dao"})
  25. public class MyBatisPlusConfig {
  26. /**
  27. * 分页插件
  28. */
  29. @Bean
  30. public PaginationInterceptor paginationInterceptor(DynamicTableNameParser dynamicTableNameParser) {
  31. PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
  32. List<ISqlParser> sqlParserList = new ArrayList<>();
  33. // 添加租户sql解析链,如果是开发业务系统,可以去掉这个配置,不去也可以。
  34. TenantSqlParser tenantSqlParser = new TenantSqlParser();
  35. tenantSqlParser.setTenantHandler(tenantHandler());
  36. sqlParserList.add(tenantSqlParser);
  37. // 设置sql解析链
  38. paginationInterceptor.setSqlParserList(sqlParserList);
  39. // 设置过滤器链
  40. paginationInterceptor.setSqlParserFilter(sqlParserFilter());
  41. return paginationInterceptor;
  42. }
  43. @Bean
  44. public TenantHandler tenantHandler() {
  45. return new TenantHandler() {
  46. @Override
  47. public Expression getTenantId() {
  48. // 返回当前登录人员的租户ID
  49. return new LongValue(
  50. RbacUserUtils.getCurrentTenantId());
  51. }
  52. @Override
  53. public String getTenantIdColumn() {
  54. return "tenant_id";
  55. }
  56. @Override
  57. public boolean doTableFilter(String tableName) {
  58. return false;
  59. }
  60. };
  61. }
  62. /**
  63. * 如何有自定义无租户的查询,可以在此过滤
  64. * @return SQL解析过滤
  65. */
  66. @Bean
  67. public ISqlParserFilter sqlParserFilter() {
  68. return metaObject -> {
  69. // 如果在程序中已经手动设置了tenant_id,此处就过滤
  70. Object boundSql = metaObject.getValue("boundSql");
  71. String sql = String.valueOf(ReflectionUtils
  72. .getFieldValue(boundSql, "sql"));
  73. return StringUtils.containsIgnoreCase(sql, "insert")
  74. && StringUtils.containsIgnoreCase(sql, "tenant_id");
  75. };
  76. }
  77. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/407494
推荐阅读
相关标签
  

闽ICP备14008679号