forked from yudaocode/SpringBoot-Labs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
346 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.1.3.RELEASE</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>lab-12-mybatis-plus-tenant</artifactId> | ||
|
||
<dependencies> | ||
<!-- 实现对数据库连接池的自动化配置 --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-jdbc</artifactId> | ||
</dependency> | ||
<dependency> <!-- 本示例,我们使用 MySQL --> | ||
<groupId>mysql</groupId> | ||
<artifactId>mysql-connector-java</artifactId> | ||
<version>5.1.48</version> | ||
</dependency> | ||
|
||
<!-- 实现对 MyBatis Plus 的自动化配置 --> | ||
<dependency> | ||
<groupId>com.baomidou</groupId> | ||
<artifactId>mybatis-plus-boot-starter</artifactId> | ||
<version>3.4.1</version> | ||
</dependency> | ||
|
||
<!-- 方便等会写单元测试 --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
</project> |
9 changes: 9 additions & 0 deletions
9
...12-mybatis-plus-tenant/src/main/java/cn/iocoder/springboot/lab12/mybatis/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package cn.iocoder.springboot.lab12.mybatis; | ||
|
||
import org.mybatis.spring.annotation.MapperScan; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
@MapperScan(basePackages = "cn.iocoder.springboot.lab12.mybatis.mapper") | ||
public class Application { | ||
} |
40 changes: 40 additions & 0 deletions
40
...us-tenant/src/main/java/cn/iocoder/springboot/lab12/mybatis/config/MybatisPlusConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package cn.iocoder.springboot.lab12.mybatis.config; | ||
|
||
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; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class MybatisPlusConfig { | ||
|
||
/** | ||
* 新多租户插件配置,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存万一出现问题 | ||
*/ | ||
@Bean | ||
public MybatisPlusInterceptor mybatisPlusInterceptor() { | ||
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); | ||
interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new TenantLineHandler() { | ||
|
||
@Override | ||
public Expression getTenantId() { | ||
return new LongValue(10); | ||
} | ||
|
||
// 这是 default 方法,默认返回 false 表示所有表都需要拼多租户条件 | ||
@Override | ||
public boolean ignoreTable(String tableName) { | ||
return false; | ||
} | ||
|
||
})); | ||
// 如果用了分页插件注意先 add TenantLineInnerInterceptor 再 add PaginationInnerInterceptor | ||
// 用了分页插件必须设置 MybatisConfiguration#useDeprecatedExecutor = false | ||
// interceptor.addInnerInterceptor(new PaginationInnerInterceptor()); | ||
return interceptor; | ||
} | ||
|
||
} |
96 changes: 96 additions & 0 deletions
96
...atis-plus-tenant/src/main/java/cn/iocoder/springboot/lab12/mybatis/dataobject/UserDO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package cn.iocoder.springboot.lab12.mybatis.dataobject; | ||
|
||
import com.baomidou.mybatisplus.annotation.TableLogic; | ||
import com.baomidou.mybatisplus.annotation.TableName; | ||
|
||
import java.util.Date; | ||
|
||
/** | ||
* 用户 DO | ||
*/ | ||
@TableName(value = "users") | ||
public class UserDO { | ||
|
||
/** | ||
* 用户编号 | ||
*/ | ||
private Integer id; | ||
/** | ||
* 账号 | ||
*/ | ||
private String username; | ||
/** | ||
* 密码(明文) | ||
* | ||
* ps:生产环境下,千万不要明文噢 | ||
*/ | ||
private String password; | ||
/** | ||
* 创建时间 | ||
*/ | ||
private Date createTime; | ||
/** | ||
* 是否删除 | ||
*/ | ||
@TableLogic | ||
private Integer deleted; | ||
/** | ||
* 租户编号 | ||
*/ | ||
private Integer tenantId; | ||
|
||
public Integer getId() { | ||
return id; | ||
} | ||
|
||
public UserDO setId(Integer id) { | ||
this.id = id; | ||
return this; | ||
} | ||
|
||
public String getUsername() { | ||
return username; | ||
} | ||
|
||
public UserDO setUsername(String username) { | ||
this.username = username; | ||
return this; | ||
} | ||
|
||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
public UserDO setPassword(String password) { | ||
this.password = password; | ||
return this; | ||
} | ||
|
||
public Date getCreateTime() { | ||
return createTime; | ||
} | ||
|
||
public UserDO setCreateTime(Date createTime) { | ||
this.createTime = createTime; | ||
return this; | ||
} | ||
|
||
public Integer getDeleted() { | ||
return deleted; | ||
} | ||
|
||
public UserDO setDeleted(Integer deleted) { | ||
this.deleted = deleted; | ||
return this; | ||
} | ||
|
||
public Integer getTenantId() { | ||
return tenantId; | ||
} | ||
|
||
public UserDO setTenantId(Integer tenantId) { | ||
this.tenantId = tenantId; | ||
return this; | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
...atis-plus-tenant/src/main/java/cn/iocoder/springboot/lab12/mybatis/mapper/UserMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package cn.iocoder.springboot.lab12.mybatis.mapper; | ||
|
||
import cn.iocoder.springboot.lab12.mybatis.dataobject.UserDO; | ||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | ||
import com.baomidou.mybatisplus.core.mapper.BaseMapper; | ||
import com.baomidou.mybatisplus.core.metadata.IPage; | ||
import org.apache.ibatis.annotations.Param; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.Collection; | ||
import java.util.Date; | ||
import java.util.List; | ||
|
||
@Repository | ||
public interface UserMapper extends BaseMapper<UserDO> { | ||
|
||
default UserDO selectByUsername(@Param("username") String username) { | ||
return selectOne(new QueryWrapper<UserDO>().eq("username", username)); | ||
} | ||
|
||
List<UserDO> selectByIds(@Param("ids") Collection<Integer> ids); | ||
|
||
default IPage<UserDO> selectPageByCreateTime(IPage<UserDO> page, @Param("createTime") Date createTime) { | ||
return selectPage(page, | ||
new QueryWrapper<UserDO>().gt("create_time", createTime) | ||
// new QueryWrapper<UserDO>().like("username", "46683d9d") | ||
); | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
lab-12-mybatis/lab-12-mybatis-plus-tenant/src/main/resources/application.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
spring: | ||
# datasource 数据源配置内容 | ||
datasource: | ||
url: jdbc:mysql://127.0.0.1:3306/testb5f4?useSSL=false&useUnicode=true&characterEncoding=UTF-8 | ||
driver-class-name: com.mysql.jdbc.Driver | ||
username: root | ||
password: 123456 | ||
|
||
# mybatis-plus 配置内容 | ||
mybatis-plus: | ||
configuration: | ||
map-underscore-to-camel-case: true # 虽然默认为 true ,但是还是显示去指定下。 | ||
global-config: | ||
db-config: | ||
id-type: auto # ID 主键自增 | ||
logic-delete-value: 1 # 逻辑已删除值(默认为 1) | ||
logic-not-delete-value: 0 # 逻辑未删除值(默认为 0) | ||
mapper-locations: classpath*:mapper/*.xml | ||
type-aliases-package: cn.iocoder.springboot.lab12.mybatis.dataobject | ||
|
||
# logging | ||
logging: | ||
level: | ||
# dao 开启 debug 模式 mybatis 输入 sql | ||
cn: | ||
iocoder: | ||
springboot: | ||
lab12: | ||
mybatis: | ||
mapper: debug |
19 changes: 19 additions & 0 deletions
19
lab-12-mybatis/lab-12-mybatis-plus-tenant/src/main/resources/mapper/UserMapper.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> | ||
<mapper namespace="cn.iocoder.springboot.lab12.mybatis.mapper.UserMapper"> | ||
|
||
<sql id="FIELDS"> | ||
id, username, password, create_time | ||
</sql> | ||
|
||
<select id="selectByIds" resultType="UserDO"> | ||
SELECT | ||
<include refid="FIELDS" /> | ||
FROM users | ||
WHERE id IN | ||
<foreach item="id" collection="ids" separator="," open="(" close=")" index=""> | ||
#{id} | ||
</foreach> | ||
</select> | ||
|
||
</mapper> |
10 changes: 10 additions & 0 deletions
10
lab-12-mybatis/lab-12-mybatis-plus-tenant/src/main/resources/sql/users.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
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; |
67 changes: 67 additions & 0 deletions
67
...-plus-tenant/src/test/java/cn/iocoder/springboot/lab12/mybatis/mapper/UserMapperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package cn.iocoder.springboot.lab12.mybatis.mapper; | ||
|
||
import cn.iocoder.springboot.lab12.mybatis.Application; | ||
import cn.iocoder.springboot.lab12.mybatis.dataobject.UserDO; | ||
import com.baomidou.mybatisplus.core.metadata.IPage; | ||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
import java.util.*; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest(classes = Application.class) | ||
public class UserMapperTest { | ||
|
||
@Autowired | ||
private UserMapper userMapper; | ||
|
||
@Test | ||
public void testInsert() { | ||
UserDO user = new UserDO().setUsername(UUID.randomUUID().toString()) | ||
.setPassword("nicai").setCreateTime(new Date()) | ||
.setDeleted(0); // 一般情况下,是否删除,可以全局枚举下。 | ||
userMapper.insert(user); | ||
} | ||
|
||
@Test | ||
public void testUpdateById() { | ||
UserDO updateUser = new UserDO().setId(1) | ||
.setPassword("wobucai"); | ||
userMapper.updateById(updateUser); | ||
} | ||
|
||
@Test | ||
public void testDeleteById() { | ||
userMapper.deleteById(2); | ||
} | ||
|
||
@Test | ||
public void testSelectById() { | ||
userMapper.selectById(1); | ||
} | ||
|
||
@Test | ||
public void testSelectByUsername() { | ||
UserDO userDO = userMapper.selectByUsername("yunai"); | ||
System.out.println(userDO); | ||
} | ||
|
||
@Test | ||
public void testSelectByIds() { | ||
List<UserDO> users = userMapper.selectByIds(Arrays.asList(1, 3)); | ||
System.out.println("users:" + users.size()); | ||
} | ||
|
||
@Test | ||
public void testSelectPageByCreateTime() { | ||
IPage<UserDO> page = new Page<>(1, 10); | ||
Date createTime = new Date(2018 - 1990, Calendar.FEBRUARY, 24); // 临时 Demo ,实际不建议这么写 | ||
page = userMapper.selectPageByCreateTime(page, createTime); | ||
System.out.println("users:" + page.getRecords().size()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters