Skip to content

Commit

Permalink
增加 mybatis plus 多租户的示例
Browse files Browse the repository at this point in the history
  • Loading branch information
YunaiV committed Dec 4, 2021
1 parent 5be6fe3 commit b96e0a1
Show file tree
Hide file tree
Showing 7 changed files with 279 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package cn.iocoder.springboot.lab12.mybatis.dataobject;

import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;

/**
* 用户拓展 DO
*/
@TableName(value = "user_profile")
public class UserProfileDO {

/**
* 编号
*/
private Integer id;
/**
* 用户编号
*/
private Integer userId;
/**
* 性别
*/
private Integer gender;
/**
* 是否删除
*/
@TableLogic
private Integer deleted;
/**
* 租户编号
*/
private Integer tenantId;

public Integer getId() {
return id;
}

public UserProfileDO setId(Integer id) {
this.id = id;
return this;
}

public Integer getGender() {
return gender;
}

public UserProfileDO setGender(Integer gender) {
this.gender = gender;
return this;
}

public Integer getDeleted() {
return deleted;
}

public UserProfileDO setDeleted(Integer deleted) {
this.deleted = deleted;
return this;
}

public Integer getTenantId() {
return tenantId;
}

public UserProfileDO setTenantId(Integer tenantId) {
this.tenantId = tenantId;
return this;
}

public Integer getUserId() {
return userId;
}

public UserProfileDO setUserId(Integer userId) {
this.userId = userId;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cn.iocoder.springboot.lab12.mybatis.mapper;

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

List<UserDetailVO> selectListA();

List<UserDetailVO> selectListB();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package cn.iocoder.springboot.lab12.mybatis.mapper;

import cn.iocoder.springboot.lab12.mybatis.dataobject.UserProfileDO;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.springframework.stereotype.Repository;

@Repository
public interface UserProfileMapper extends BaseMapper<UserProfileDO> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package cn.iocoder.springboot.lab12.mybatis.vo;

import com.baomidou.mybatisplus.annotation.TableLogic;

import java.util.Date;

public class UserDetailVO {

/**
* 用户编号
*/
private Integer id;
/**
* 账号
*/
private String username;
/**
* 密码(明文)
*
* ps:生产环境下,千万不要明文噢
*/
private String password;
/**
* 性别
*/
private Integer gender;
/**
* 创建时间
*/
private Date createTime;
/**
* 是否删除
*/
@TableLogic
private Integer deleted;
/**
* 租户编号
*/
private Integer tenantId;

public Integer getId() {
return id;
}

public UserDetailVO setId(Integer id) {
this.id = id;
return this;
}

public String getUsername() {
return username;
}

public UserDetailVO setUsername(String username) {
this.username = username;
return this;
}

public String getPassword() {
return password;
}

public UserDetailVO setPassword(String password) {
this.password = password;
return this;
}

public Integer getGender() {
return gender;
}

public UserDetailVO setGender(Integer gender) {
this.gender = gender;
return this;
}

public Date getCreateTime() {
return createTime;
}

public UserDetailVO setCreateTime(Date createTime) {
this.createTime = createTime;
return this;
}

public Integer getDeleted() {
return deleted;
}

public UserDetailVO setDeleted(Integer deleted) {
this.deleted = deleted;
return this;
}

public Integer getTenantId() {
return tenantId;
}

public UserDetailVO setTenantId(Integer tenantId) {
this.tenantId = tenantId;
return this;
}

@Override
public String toString() {
return "UserDetailVO{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", gender=" + gender +
", createTime=" + createTime +
", deleted=" + deleted +
", tenantId=" + tenantId +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,19 @@
</foreach>
</select>

<select id="selectListA" resultType="cn.iocoder.springboot.lab12.mybatis.vo.UserDetailVO">
SELECT
u.id, u.username, u.password, u.create_time, p.gender
FROM users u
JOIN user_profile p
ON u.id = p.user_id
</select>

<select id="selectListB" resultType="cn.iocoder.springboot.lab12.mybatis.vo.UserDetailVO">
SELECT
u.id, u.username, u.password, u.create_time, p.gender
FROM users u, user_profile p
WHERE u.id = p.user_id
</select>

</mapper>
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户编号',
`username` varchar(64) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '账号',
`password` varchar(32) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '密码',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`deleted` bit(1) DEFAULT NULL COMMENT '是否删除。0-未删除;1-删除',
`tenant_id` int(11) NOT NULL COMMENT '租户编号',
PRIMARY KEY (`id`),
UNIQUE KEY `idx_username` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
CREATE TABLE `users`
(
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户编号',
`username` varchar(64) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '账号',
`password` varchar(32) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '密码',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`deleted` bit(1) DEFAULT NULL COMMENT '是否删除。0-未删除;1-删除',
`tenant_id` int(11) NOT NULL COMMENT '租户编号',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

CREATE TABLE `user_profile`
(
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '编号',
`user_id` int(11) NOT NULL COMMENT '用户编号',
`gender` int(11) NOT NULL COMMENT '性别',
`deleted` bit(1) DEFAULT NULL COMMENT '是否删除。0-未删除;1-删除',
`tenant_id` int(11) NOT NULL COMMENT '租户编号',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

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

@Autowired
private UserMapper userMapper;
@Autowired
private UserProfileMapper userProfileMapper;

@Test
public void initTestData() {
// 清理数据
userMapper.delete(new QueryWrapper<>());
userProfileMapper.delete(new QueryWrapper<>());
// 插入一个用户
UserDO userDO = new UserDO().setUsername(UUID.randomUUID().toString())
.setPassword("nicai").setCreateTime(new Date())
.setDeleted(0); // 一般情况下,是否删除,可以全局枚举下。
userMapper.insert(userDO);
// 插入该用户的拓展信息
UserProfileDO userProfileDO = new UserProfileDO();
userProfileDO.setUserId(userDO.getId());
userProfileDO.setGender(1);
userProfileDO.setTenantId(10); // TODO 全局写死
userProfileDO.setDeleted(0); // 一般情况下,是否删除,可以全局枚举下。
userProfileMapper.insert(userProfileDO);
}

@Test
public void testInsert() {
Expand Down Expand Up @@ -64,4 +88,16 @@ public void testSelectPageByCreateTime() {
System.out.println("users:" + page.getRecords().size());
}

@Test
public void testSelectListA() {
List<UserDetailVO> list = userMapper.selectListA();
System.out.println(list);
}

@Test
public void testSelectListB() {
List<UserDetailVO> list = userMapper.selectListB();
System.out.println(list);
}

}

0 comments on commit b96e0a1

Please sign in to comment.