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 b96e0a1

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

File tree

7 files changed

+279
-10
lines changed

7 files changed

+279
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
/**
7+
* 用户拓展 DO
8+
*/
9+
@TableName(value = "user_profile")
10+
public class UserProfileDO {
11+
12+
/**
13+
* 编号
14+
*/
15+
private Integer id;
16+
/**
17+
* 用户编号
18+
*/
19+
private Integer userId;
20+
/**
21+
* 性别
22+
*/
23+
private Integer gender;
24+
/**
25+
* 是否删除
26+
*/
27+
@TableLogic
28+
private Integer deleted;
29+
/**
30+
* 租户编号
31+
*/
32+
private Integer tenantId;
33+
34+
public Integer getId() {
35+
return id;
36+
}
37+
38+
public UserProfileDO setId(Integer id) {
39+
this.id = id;
40+
return this;
41+
}
42+
43+
public Integer getGender() {
44+
return gender;
45+
}
46+
47+
public UserProfileDO setGender(Integer gender) {
48+
this.gender = gender;
49+
return this;
50+
}
51+
52+
public Integer getDeleted() {
53+
return deleted;
54+
}
55+
56+
public UserProfileDO setDeleted(Integer deleted) {
57+
this.deleted = deleted;
58+
return this;
59+
}
60+
61+
public Integer getTenantId() {
62+
return tenantId;
63+
}
64+
65+
public UserProfileDO setTenantId(Integer tenantId) {
66+
this.tenantId = tenantId;
67+
return this;
68+
}
69+
70+
public Integer getUserId() {
71+
return userId;
72+
}
73+
74+
public UserProfileDO setUserId(Integer userId) {
75+
this.userId = userId;
76+
return this;
77+
}
78+
}

‎lab-12-mybatis/lab-12-mybatis-plus-tenant/src/main/java/cn/iocoder/springboot/lab12/mybatis/mapper/UserMapper.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cn.iocoder.springboot.lab12.mybatis.mapper;
22

33
import cn.iocoder.springboot.lab12.mybatis.dataobject.UserDO;
4+
import cn.iocoder.springboot.lab12.mybatis.vo.UserDetailVO;
45
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
56
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
67
import com.baomidou.mybatisplus.core.metadata.IPage;
@@ -27,4 +28,8 @@ default IPage<UserDO> selectPageByCreateTime(IPage<UserDO> page, @Param("createT
2728
);
2829
}
2930

31+
List<UserDetailVO> selectListA();
32+
33+
List<UserDetailVO> selectListB();
34+
3035
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package cn.iocoder.springboot.lab12.mybatis.mapper;
2+
3+
import cn.iocoder.springboot.lab12.mybatis.dataobject.UserProfileDO;
4+
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
5+
import org.springframework.stereotype.Repository;
6+
7+
@Repository
8+
public interface UserProfileMapper extends BaseMapper<UserProfileDO> {
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package cn.iocoder.springboot.lab12.mybatis.vo;
2+
3+
import com.baomidou.mybatisplus.annotation.TableLogic;
4+
5+
import java.util.Date;
6+
7+
public class UserDetailVO {
8+
9+
/**
10+
* 用户编号
11+
*/
12+
private Integer id;
13+
/**
14+
* 账号
15+
*/
16+
private String username;
17+
/**
18+
* 密码(明文)
19+
*
20+
* ps:生产环境下,千万不要明文噢
21+
*/
22+
private String password;
23+
/**
24+
* 性别
25+
*/
26+
private Integer gender;
27+
/**
28+
* 创建时间
29+
*/
30+
private Date createTime;
31+
/**
32+
* 是否删除
33+
*/
34+
@TableLogic
35+
private Integer deleted;
36+
/**
37+
* 租户编号
38+
*/
39+
private Integer tenantId;
40+
41+
public Integer getId() {
42+
return id;
43+
}
44+
45+
public UserDetailVO setId(Integer id) {
46+
this.id = id;
47+
return this;
48+
}
49+
50+
public String getUsername() {
51+
return username;
52+
}
53+
54+
public UserDetailVO setUsername(String username) {
55+
this.username = username;
56+
return this;
57+
}
58+
59+
public String getPassword() {
60+
return password;
61+
}
62+
63+
public UserDetailVO setPassword(String password) {
64+
this.password = password;
65+
return this;
66+
}
67+
68+
public Integer getGender() {
69+
return gender;
70+
}
71+
72+
public UserDetailVO setGender(Integer gender) {
73+
this.gender = gender;
74+
return this;
75+
}
76+
77+
public Date getCreateTime() {
78+
return createTime;
79+
}
80+
81+
public UserDetailVO setCreateTime(Date createTime) {
82+
this.createTime = createTime;
83+
return this;
84+
}
85+
86+
public Integer getDeleted() {
87+
return deleted;
88+
}
89+
90+
public UserDetailVO setDeleted(Integer deleted) {
91+
this.deleted = deleted;
92+
return this;
93+
}
94+
95+
public Integer getTenantId() {
96+
return tenantId;
97+
}
98+
99+
public UserDetailVO setTenantId(Integer tenantId) {
100+
this.tenantId = tenantId;
101+
return this;
102+
}
103+
104+
@Override
105+
public String toString() {
106+
return "UserDetailVO{" +
107+
"id=" + id +
108+
", username='" + username + '\'' +
109+
", password='" + password + '\'' +
110+
", gender=" + gender +
111+
", createTime=" + createTime +
112+
", deleted=" + deleted +
113+
", tenantId=" + tenantId +
114+
'}';
115+
}
116+
}

‎lab-12-mybatis/lab-12-mybatis-plus-tenant/src/main/resources/mapper/UserMapper.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,19 @@
1616
</foreach>
1717
</select>
1818

19+
<select id="selectListA" resultType="cn.iocoder.springboot.lab12.mybatis.vo.UserDetailVO">
20+
SELECT
21+
u.id, u.username, u.password, u.create_time, p.gender
22+
FROM users u
23+
JOIN user_profile p
24+
ON u.id = p.user_id
25+
</select>
26+
27+
<select id="selectListB" resultType="cn.iocoder.springboot.lab12.mybatis.vo.UserDetailVO">
28+
SELECT
29+
u.id, u.username, u.password, u.create_time, p.gender
30+
FROM users u, user_profile p
31+
WHERE u.id = p.user_id
32+
</select>
33+
1934
</mapper>
Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
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;
1+
CREATE TABLE `users`
2+
(
3+
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户编号',
4+
`username` varchar(64) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '账号',
5+
`password` varchar(32) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '密码',
6+
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
7+
`deleted` bit(1) DEFAULT NULL COMMENT '是否删除。0-未删除;1-删除',
8+
`tenant_id` int(11) NOT NULL COMMENT '租户编号',
9+
PRIMARY KEY (`id`)
10+
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
11+
12+
CREATE TABLE `user_profile`
13+
(
14+
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '编号',
15+
`user_id` int(11) NOT NULL COMMENT '用户编号',
16+
`gender` int(11) NOT NULL COMMENT '性别',
17+
`deleted` bit(1) DEFAULT NULL COMMENT '是否删除。0-未删除;1-删除',
18+
`tenant_id` int(11) NOT NULL COMMENT '租户编号',
19+
PRIMARY KEY (`id`)
20+
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

‎lab-12-mybatis/lab-12-mybatis-plus-tenant/src/test/java/cn/iocoder/springboot/lab12/mybatis/mapper/UserMapperTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import cn.iocoder.springboot.lab12.mybatis.Application;
44
import cn.iocoder.springboot.lab12.mybatis.dataobject.UserDO;
5+
import cn.iocoder.springboot.lab12.mybatis.dataobject.UserProfileDO;
6+
import cn.iocoder.springboot.lab12.mybatis.vo.UserDetailVO;
7+
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
58
import com.baomidou.mybatisplus.core.metadata.IPage;
69
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
710
import org.junit.Test;
@@ -18,6 +21,27 @@ public class UserMapperTest {
1821

1922
@Autowired
2023
private UserMapper userMapper;
24+
@Autowired
25+
private UserProfileMapper userProfileMapper;
26+
27+
@Test
28+
public void initTestData() {
29+
// 清理数据
30+
userMapper.delete(new QueryWrapper<>());
31+
userProfileMapper.delete(new QueryWrapper<>());
32+
// 插入一个用户
33+
UserDO userDO = new UserDO().setUsername(UUID.randomUUID().toString())
34+
.setPassword("nicai").setCreateTime(new Date())
35+
.setDeleted(0); // 一般情况下,是否删除,可以全局枚举下。
36+
userMapper.insert(userDO);
37+
// 插入该用户的拓展信息
38+
UserProfileDO userProfileDO = new UserProfileDO();
39+
userProfileDO.setUserId(userDO.getId());
40+
userProfileDO.setGender(1);
41+
userProfileDO.setTenantId(10); // TODO 全局写死
42+
userProfileDO.setDeleted(0); // 一般情况下,是否删除,可以全局枚举下。
43+
userProfileMapper.insert(userProfileDO);
44+
}
2145

2246
@Test
2347
public void testInsert() {
@@ -64,4 +88,16 @@ public void testSelectPageByCreateTime() {
6488
System.out.println("users:" + page.getRecords().size());
6589
}
6690

91+
@Test
92+
public void testSelectListA() {
93+
List<UserDetailVO> list = userMapper.selectListA();
94+
System.out.println(list);
95+
}
96+
97+
@Test
98+
public void testSelectListB() {
99+
List<UserDetailVO> list = userMapper.selectListB();
100+
System.out.println(list);
101+
}
102+
67103
}

0 commit comments

Comments
(0)

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