目前線上很多SAAS系統(tǒng),但是SAAS系統(tǒng)必須支持多租戶,對于多租戶有多種實現(xiàn)方式,當(dāng)然不同的實現(xiàn)方式的成本也是不同的。這里我介紹一種最節(jié)省成本的實現(xiàn)方式,通過數(shù)據(jù)記錄增加租戶標(biāo)識字段tenant_id的方式。但是每次對數(shù)據(jù)庫操作都要帶上租戶標(biāo)識tenant_id是非常不方便,很容易出錯。一旦出錯就是破壞了SAAS的數(shù)據(jù)隔離,出現(xiàn)非常嚴(yán)重的事故。本次介紹的Spring boot 2.x 集成MyBatis-plus 通過自動攔截SQL語句并自動填充租戶標(biāo)識tenant_id,
一、創(chuàng)建maven項目zircloud-tenant, pom文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.Apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.2</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.zircloud.tenant</groupId>
<artifactId>zircloud-tenant</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>zircloud-tenant</name>
<description>Demo project for Spring Boot</description>
<properties>
<JAVA.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>MySQL</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.0</version>
</dependency>
<!-- alibaba的druid數(shù)據(jù)庫連接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
項目結(jié)構(gòu)如下:
二、創(chuàng)建Mybatis-plus 配置類 MybatisPlusConfig.java
Mybaits-plus 配置用于自動填充租戶標(biāo)識tenant_id, 忽略不需要填充租戶標(biāo)識tenant_id的數(shù)據(jù)庫表
package com.zircloud.tenant.config;
import org.mybatis.spring.annotation.MApperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
/**
* @author test
* @since 2022年1月10日
*/
@Configuration
@MapperScan("com.zircloud.**.mapper.**") //要掃描的mapper目錄
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new TenantLineHandler() {
@Override
public Expression getTenantId() {
Long tenantId = 1L; //租戶標(biāo)識,可以從服務(wù)上下文中獲取,
return new LongValue(tenantId);
}
@Override
public boolean ignoreTable(String tableName) {
return !"user_info".equalsIgnoreCase(tableName); //配置要忽略多租戶的數(shù)據(jù)庫表
}
}));
return interceptor;
}
}
三、創(chuàng)建的Service、Controller類,
創(chuàng)建的Service、Controller就不用手動填充租戶標(biāo)識tenant_id了
package com.zircloud.tenant.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zircloud.tenant.dto.UserInfoDto;
import com.zircloud.tenant.entity.UserInfo;
import com.zircloud.tenant.mapper.UserInfoMapper;
import com.zircloud.tenant.service.UserInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* <p>
* 服務(wù)實現(xiàn)類
* </p>
*
* @author test
* @since 2022-01-06
*/
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {
@Autowired
private UserInfoMapper mapper;
@Override
public UserInfoDto getUserByid(Integer id) {
UserInfo user = mapper.selectById(id);
if(user!=null) {
UserInfoDto dto = new UserInfoDto();
BeanUtils.copyProperties(user, dto);
return dto;
}
return null;
}
@Override
public UserInfoDto getUserByname(String name) {
QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
queryWrapper.eq("name",name);
UserInfo user = mapper.selectOne(queryWrapper);
if(user!=null) {
UserInfoDto dto = new UserInfoDto();
BeanUtils.copyProperties(user, dto);
return dto;
}
return null;
}
@Override
public boolean creaate(UserInfoDto dto) {
if(dto!=null) {
UserInfo user = new UserInfo();
BeanUtils.copyProperties(dto, user);
return mapper.insert(user)>0;
}
return false;
}
}
Controller
package com.zircloud.tenant.web;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zircloud.tenant.dto.UserInfoDto;
import com.zircloud.tenant.service.UserInfoService;
/**
* 用戶管理
* @author test
*
*/
@RestController
@RequestMapping("/v1/users")
public class UserInfoController {
@Resource
private UserInfoService userInfoService;
/**
* 根據(jù)ID查詢用戶
* @param id
* @return
*/
@GetMapping("/details/{id}")
public UserInfoDto getUserbyId(@PathVariable("id") Integer id) {
return userInfoService.getUserByid(id);
}
/**
* 根據(jù)ID查詢用戶
* @param id
* @return
*/
@GetMapping("/query/{name}")
public UserInfoDto getUserbyname(@PathVariable("name") String name) {
return userInfoService.getUserByname(name);
}
/**
* 創(chuàng)建用戶
* @param dto
* @return
*/
@PostMapping("/create")
public boolean create(@RequestBody UserInfoDto dto) {
return userInfoService.creaate(dto);
}
}
通過以上步驟就實現(xiàn)Mybatis-plus對多租戶的支持。以后就不再需要手動填充租戶標(biāo)識了。
zircloud-tentant: 本項目是采用spring boot 2.x 集成 myabatis-plus 實現(xiàn)多租戶