Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5be6fe3

Browse files
committed
增加 mybatis plus 多租户的示例
1 parent 85c7322 commit 5be6fe3

File tree

11 files changed

+346
-1
lines changed

11 files changed

+346
-1
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.1.3.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<modelVersion>4.0.0</modelVersion>
12+
13+
<artifactId>lab-12-mybatis-plus-tenant</artifactId>
14+
15+
<dependencies>
16+
<!-- 实现对数据库连接池的自动化配置 -->
17+
<dependency>
18+
<groupId>org.springframework.boot</groupId>
19+
<artifactId>spring-boot-starter-jdbc</artifactId>
20+
</dependency>
21+
<dependency> <!-- 本示例,我们使用 MySQL -->
22+
<groupId>mysql</groupId>
23+
<artifactId>mysql-connector-java</artifactId>
24+
<version>5.1.48</version>
25+
</dependency>
26+
27+
<!-- 实现对 MyBatis Plus 的自动化配置 -->
28+
<dependency>
29+
<groupId>com.baomidou</groupId>
30+
<artifactId>mybatis-plus-boot-starter</artifactId>
31+
<version>3.4.1</version>
32+
</dependency>
33+
34+
<!-- 方便等会写单元测试 -->
35+
<dependency>
36+
<groupId>org.springframework.boot</groupId>
37+
<artifactId>spring-boot-starter-test</artifactId>
38+
<scope>test</scope>
39+
</dependency>
40+
41+
</dependencies>
42+
43+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package cn.iocoder.springboot.lab12.mybatis;
2+
3+
import org.mybatis.spring.annotation.MapperScan;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
@MapperScan(basePackages = "cn.iocoder.springboot.lab12.mybatis.mapper")
8+
public class Application {
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package cn.iocoder.springboot.lab12.mybatis.config;
2+
3+
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
4+
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
5+
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
6+
import net.sf.jsqlparser.expression.Expression;
7+
import net.sf.jsqlparser.expression.LongValue;
8+
import org.springframework.context.annotation.Bean;
9+
import org.springframework.context.annotation.Configuration;
10+
11+
@Configuration
12+
public class MybatisPlusConfig {
13+
14+
/**
15+
* 新多租户插件配置,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存万一出现问题
16+
*/
17+
@Bean
18+
public MybatisPlusInterceptor mybatisPlusInterceptor() {
19+
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
20+
interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new TenantLineHandler() {
21+
22+
@Override
23+
public Expression getTenantId() {
24+
return new LongValue(10);
25+
}
26+
27+
// 这是 default 方法,默认返回 false 表示所有表都需要拼多租户条件
28+
@Override
29+
public boolean ignoreTable(String tableName) {
30+
return false;
31+
}
32+
33+
}));
34+
// 如果用了分页插件注意先 add TenantLineInnerInterceptor 再 add PaginationInnerInterceptor
35+
// 用了分页插件必须设置 MybatisConfiguration#useDeprecatedExecutor = false
36+
// interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
37+
return interceptor;
38+
}
39+
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package cn.iocoder.springboot.lab12.mybatis.dataobject;
2+
3+
import com.baomidou.mybatisplus.annotation.TableLogic;
4+
import com.baomidou.mybatisplus.annotation.TableName;
5+
6+
import java.util.Date;
7+
8+
/**
9+
* 用户 DO
10+
*/
11+
@TableName(value = "users")
12+
public class UserDO {
13+
14+
/**
15+
* 用户编号
16+
*/
17+
private Integer id;
18+
/**
19+
* 账号
20+
*/
21+
private String username;
22+
/**
23+
* 密码(明文)
24+
*
25+
* ps:生产环境下,千万不要明文噢
26+
*/
27+
private String password;
28+
/**
29+
* 创建时间
30+
*/
31+
private Date createTime;
32+
/**
33+
* 是否删除
34+
*/
35+
@TableLogic
36+
private Integer deleted;
37+
/**
38+
* 租户编号
39+
*/
40+
private Integer tenantId;
41+
42+
public Integer getId() {
43+
return id;
44+
}
45+
46+
public UserDO setId(Integer id) {
47+
this.id = id;
48+
return this;
49+
}
50+
51+
public String getUsername() {
52+
return username;
53+
}
54+
55+
public UserDO setUsername(String username) {
56+
this.username = username;
57+
return this;
58+
}
59+
60+
public String getPassword() {
61+
return password;
62+
}
63+
64+
public UserDO setPassword(String password) {
65+
this.password = password;
66+
return this;
67+
}
68+
69+
public Date getCreateTime() {
70+
return createTime;
71+
}
72+
73+
public UserDO setCreateTime(Date createTime) {
74+
this.createTime = createTime;
75+
return this;
76+
}
77+
78+
public Integer getDeleted() {
79+
return deleted;
80+
}
81+
82+
public UserDO setDeleted(Integer deleted) {
83+
this.deleted = deleted;
84+
return this;
85+
}
86+
87+
public Integer getTenantId() {
88+
return tenantId;
89+
}
90+
91+
public UserDO setTenantId(Integer tenantId) {
92+
this.tenantId = tenantId;
93+
return this;
94+
}
95+
96+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package cn.iocoder.springboot.lab12.mybatis.mapper;
2+
3+
import cn.iocoder.springboot.lab12.mybatis.dataobject.UserDO;
4+
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
5+
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
6+
import com.baomidou.mybatisplus.core.metadata.IPage;
7+
import org.apache.ibatis.annotations.Param;
8+
import org.springframework.stereotype.Repository;
9+
10+
import java.util.Collection;
11+
import java.util.Date;
12+
import java.util.List;
13+
14+
@Repository
15+
public interface UserMapper extends BaseMapper<UserDO> {
16+
17+
default UserDO selectByUsername(@Param("username") String username) {
18+
return selectOne(new QueryWrapper<UserDO>().eq("username", username));
19+
}
20+
21+
List<UserDO> selectByIds(@Param("ids") Collection<Integer> ids);
22+
23+
default IPage<UserDO> selectPageByCreateTime(IPage<UserDO> page, @Param("createTime") Date createTime) {
24+
return selectPage(page,
25+
new QueryWrapper<UserDO>().gt("create_time", createTime)
26+
// new QueryWrapper<UserDO>().like("username", "46683d9d")
27+
);
28+
}
29+
30+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
spring:
2+
# datasource 数据源配置内容
3+
datasource:
4+
url: jdbc:mysql://127.0.0.1:3306/testb5f4?useSSL=false&useUnicode=true&characterEncoding=UTF-8
5+
driver-class-name: com.mysql.jdbc.Driver
6+
username: root
7+
password: 123456
8+
9+
# mybatis-plus 配置内容
10+
mybatis-plus:
11+
configuration:
12+
map-underscore-to-camel-case: true # 虽然默认为 true ,但是还是显示去指定下。
13+
global-config:
14+
db-config:
15+
id-type: auto # ID 主键自增
16+
logic-delete-value: 1 # 逻辑已删除值(默认为 1)
17+
logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)
18+
mapper-locations: classpath*:mapper/*.xml
19+
type-aliases-package: cn.iocoder.springboot.lab12.mybatis.dataobject
20+
21+
# logging
22+
logging:
23+
level:
24+
# dao 开启 debug 模式 mybatis 输入 sql
25+
cn:
26+
iocoder:
27+
springboot:
28+
lab12:
29+
mybatis:
30+
mapper: debug
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
3+
<mapper namespace="cn.iocoder.springboot.lab12.mybatis.mapper.UserMapper">
4+
5+
<sql id="FIELDS">
6+
id, username, password, create_time
7+
</sql>
8+
9+
<select id="selectByIds" resultType="UserDO">
10+
SELECT
11+
<include refid="FIELDS" />
12+
FROM users
13+
WHERE id IN
14+
<foreach item="id" collection="ids" separator="," open="(" close=")" index="">
15+
#{id}
16+
</foreach>
17+
</select>
18+
19+
</mapper>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CREATE TABLE `users` (
2+
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户编号',
3+
`username` varchar(64) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '账号',
4+
`password` varchar(32) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '密码',
5+
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
6+
`deleted` bit(1) DEFAULT NULL COMMENT '是否删除。0-未删除;1-删除',
7+
`tenant_id` int(11) NOT NULL COMMENT '租户编号',
8+
PRIMARY KEY (`id`),
9+
UNIQUE KEY `idx_username` (`username`)
10+
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package cn.iocoder.springboot.lab12.mybatis.mapper;
2+
3+
import cn.iocoder.springboot.lab12.mybatis.Application;
4+
import cn.iocoder.springboot.lab12.mybatis.dataobject.UserDO;
5+
import com.baomidou.mybatisplus.core.metadata.IPage;
6+
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.boot.test.context.SpringBootTest;
11+
import org.springframework.test.context.junit4.SpringRunner;
12+
13+
import java.util.*;
14+
15+
@RunWith(SpringRunner.class)
16+
@SpringBootTest(classes = Application.class)
17+
public class UserMapperTest {
18+
19+
@Autowired
20+
private UserMapper userMapper;
21+
22+
@Test
23+
public void testInsert() {
24+
UserDO user = new UserDO().setUsername(UUID.randomUUID().toString())
25+
.setPassword("nicai").setCreateTime(new Date())
26+
.setDeleted(0); // 一般情况下,是否删除,可以全局枚举下。
27+
userMapper.insert(user);
28+
}
29+
30+
@Test
31+
public void testUpdateById() {
32+
UserDO updateUser = new UserDO().setId(1)
33+
.setPassword("wobucai");
34+
userMapper.updateById(updateUser);
35+
}
36+
37+
@Test
38+
public void testDeleteById() {
39+
userMapper.deleteById(2);
40+
}
41+
42+
@Test
43+
public void testSelectById() {
44+
userMapper.selectById(1);
45+
}
46+
47+
@Test
48+
public void testSelectByUsername() {
49+
UserDO userDO = userMapper.selectByUsername("yunai");
50+
System.out.println(userDO);
51+
}
52+
53+
@Test
54+
public void testSelectByIds() {
55+
List<UserDO> users = userMapper.selectByIds(Arrays.asList(1, 3));
56+
System.out.println("users:" + users.size());
57+
}
58+
59+
@Test
60+
public void testSelectPageByCreateTime() {
61+
IPage<UserDO> page = new Page<>(1, 10);
62+
Date createTime = new Date(2018 - 1990, Calendar.FEBRUARY, 24); // 临时 Demo ,实际不建议这么写
63+
page = userMapper.selectPageByCreateTime(page, createTime);
64+
System.out.println("users:" + page.getRecords().size());
65+
}
66+
67+
}

‎lab-12-mybatis/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<module>lab-12-mybatis-annotation</module>
1717
<module>lab-12-mybatis-plus</module>
1818
<module>lab-12-mybatis-tk</module>
19+
<module>lab-12-mybatis-plus-tenant</module>
1920
</modules>
2021

2122

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /