赞
踩
1.引入依赖(pom.xml)
-
- <dependency>
- <groupId>com.baomidou</groupId>
- <artifactId>mybatis-plus-boot-starter</artifactId>
- <version>${mybatis-plus.version}</version>
- </dependency>
-
- <dependency>
- <groupId>com.baomidou</groupId>
- <artifactId>mybatis-plus-generator</artifactId>
- <version>${mybatis-plus.version}</version>
- </dependency>
-
- <dependency>
- <groupId>com.baomidou</groupId>
- <artifactId>mybatis-plus</artifactId>
- <version>${mybatis-plus.version}</version>
- </dependency>
-
- <dependency>
- <groupId>com.baomidou</groupId>
- <artifactId>mybatis-plus-dts</artifactId>
- <version>${mybatis-plus.version}</version>
- </dependency>
2.配置文件application.yml
多数据源
spring: datasource: master: jdbc-url: jdbc:mysql://192.168.102.31:3306/test username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver slave1: jdbc-url: jdbc:mysql://192.168.102.56:3306/test username: pig # 只读账户 password: 123456 driver-class-name: com.mysql.jdbc.Driver slave2: jdbc-url: jdbc:mysql://192.168.102.36:3306/test username: pig # 只读账户 password: 123456 driver-class-name: com.mysql.jdbc.Driver
单数据源
- spring:
- datasource:
- name: db
- type: com.alibaba.druid.pool.DruidDataSource
- url: jdbc:mysql://127.0.0.1:3306/user?characterEncoding=utf-8
- username: root
- password: root
- driver-class-name: com.mysql.jdbc.Driver
3.maper层写法
继承BaseMapper<实体类>
- public interface TUserMapper extends BaseMapper<TUser> {
-
- }
4.配置Mybatis Plus的租户
- package com.fight.strive.sys.modules.mybatisplus.config;
-
- import com.baomidou.mybatisplus.core.parser.ISqlParser;
- import com.baomidou.mybatisplus.core.parser.ISqlParserFilter;
- import com.baomidou.mybatisplus.extension.parsers.DynamicTableNameParser;
- import com.baomidou.mybatisplus.extension.parsers.ITableNameHandler;
- import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
- import com.baomidou.mybatisplus.extension.plugins.tenant.TenantHandler;
- import com.baomidou.mybatisplus.extension.plugins.tenant.TenantSqlParser;
- import com.fight.strive.sys.modules.rbac.utils.RbacUserUtils;
- import com.fight.strive.sys.utils.ReflectionUtils;
- import com.fight.strive.sys.utils.StringUtils;
- import lombok.extern.slf4j.Slf4j;
- import net.sf.jsqlparser.expression.Expression;
- import net.sf.jsqlparser.expression.LongValue;
- import org.mybatis.spring.annotation.MapperScan;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
-
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
-
- @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");
- };
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。