mirror of
https://gitee.com/yudaocode/SpringBoot-Labs.git
synced 2026-09-03 05:53:54 +08:00
增加多数据源示例
This commit is contained in:
56
lab-17/lab-17-dynamic-datasource-baomidou-01/pom.xml
Normal file
56
lab-17/lab-17-dynamic-datasource-baomidou-01/pom.xml
Normal file
@@ -0,0 +1,56 @@
|
||||
<?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-17-dynamic-datasource-baomidou-01</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 的自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>org.mybatis.spring.boot</groupId>
|
||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||
<version>2.1.1</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 实现对 dynamic-datasource 的自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
|
||||
<version>2.5.7</version>
|
||||
</dependency>
|
||||
|
||||
<!-- TODO 测试 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-actuator</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 方便等会写单元测试 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,11 @@
|
||||
package cn.iocoder.springboot.lab17.dynamicdatasource;
|
||||
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.EnableAspectJAutoProxy;
|
||||
|
||||
@SpringBootApplication
|
||||
@MapperScan(basePackages = "cn.iocoder.springboot.lab17.dynamicdatasource.mapper")
|
||||
@EnableAspectJAutoProxy(exposeProxy = true) // http://www.voidcn.com/article/p-zddcuyii-bpt.html
|
||||
public class Application {
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package cn.iocoder.springboot.lab17.dynamicdatasource.constant;
|
||||
|
||||
/**
|
||||
* 数据库枚举类
|
||||
*/
|
||||
public class DBConstants {
|
||||
|
||||
/**
|
||||
* 数据源分组 - 订单库
|
||||
*/
|
||||
public static final String DATASOURCE_ORDERS = "orders";
|
||||
|
||||
/**
|
||||
* 数据源分组 - 用户库
|
||||
*/
|
||||
public static final String DATASOURCE_USERS = "users";
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package cn.iocoder.springboot.lab17.dynamicdatasource.dataobject;
|
||||
|
||||
/**
|
||||
* 订单 DO
|
||||
*/
|
||||
public class OrderDO {
|
||||
|
||||
/**
|
||||
* 订单编号
|
||||
*/
|
||||
private Integer id;
|
||||
/**
|
||||
* 用户编号
|
||||
*/
|
||||
private Integer userId;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public OrderDO setId(Integer id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Integer getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public OrderDO setUserId(Integer userId) {
|
||||
this.userId = userId;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "OrderDO{" +
|
||||
"id=" + id +
|
||||
", userId=" + userId +
|
||||
'}';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package cn.iocoder.springboot.lab17.dynamicdatasource.dataobject;
|
||||
|
||||
/**
|
||||
* 用户 DO
|
||||
*/
|
||||
public class UserDO {
|
||||
|
||||
/**
|
||||
* 用户编号
|
||||
*/
|
||||
private Integer id;
|
||||
/**
|
||||
* 账号
|
||||
*/
|
||||
private String username;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "UserDO{" +
|
||||
"id=" + id +
|
||||
", username='" + username + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package cn.iocoder.springboot.lab17.dynamicdatasource.mapper;
|
||||
|
||||
import cn.iocoder.springboot.lab17.dynamicdatasource.constant.DBConstants;
|
||||
import cn.iocoder.springboot.lab17.dynamicdatasource.dataobject.OrderDO;
|
||||
import com.baomidou.dynamic.datasource.annotation.DS;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
@DS(DBConstants.DATASOURCE_ORDERS)
|
||||
public interface OrderMapper {
|
||||
|
||||
OrderDO selectById(@Param("id") Integer id);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package cn.iocoder.springboot.lab17.dynamicdatasource.mapper;
|
||||
|
||||
import cn.iocoder.springboot.lab17.dynamicdatasource.constant.DBConstants;
|
||||
import cn.iocoder.springboot.lab17.dynamicdatasource.dataobject.UserDO;
|
||||
import com.baomidou.dynamic.datasource.annotation.DS;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
@DS(DBConstants.DATASOURCE_USERS)
|
||||
public interface UserMapper {
|
||||
|
||||
UserDO selectById(@Param("id") Integer id);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package cn.iocoder.springboot.lab17.dynamicdatasource.service;
|
||||
|
||||
import cn.iocoder.springboot.lab17.dynamicdatasource.constant.DBConstants;
|
||||
import cn.iocoder.springboot.lab17.dynamicdatasource.dataobject.OrderDO;
|
||||
import cn.iocoder.springboot.lab17.dynamicdatasource.dataobject.UserDO;
|
||||
import cn.iocoder.springboot.lab17.dynamicdatasource.mapper.OrderMapper;
|
||||
import cn.iocoder.springboot.lab17.dynamicdatasource.mapper.UserMapper;
|
||||
import com.baomidou.dynamic.datasource.annotation.DS;
|
||||
import org.springframework.aop.framework.AopContext;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
public class OrderService {
|
||||
|
||||
@Autowired
|
||||
private OrderMapper orderMapper;
|
||||
@Autowired
|
||||
private UserMapper userMapper;
|
||||
|
||||
private OrderService self() {
|
||||
return (OrderService) AopContext.currentProxy();
|
||||
}
|
||||
|
||||
public void method01() {
|
||||
// 查询订单
|
||||
OrderDO order = orderMapper.selectById(1);
|
||||
System.out.println(order);
|
||||
// 查询用户
|
||||
UserDO user = userMapper.selectById(1);
|
||||
System.out.println(user);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void method02() {
|
||||
// 查询订单
|
||||
OrderDO order = orderMapper.selectById(1);
|
||||
System.out.println(order);
|
||||
// 查询用户
|
||||
UserDO user = userMapper.selectById(1);
|
||||
System.out.println(user);
|
||||
}
|
||||
|
||||
public void method03() {
|
||||
// 查询订单
|
||||
self().method031();
|
||||
// 查询用户
|
||||
self().method032();
|
||||
}
|
||||
|
||||
@Transactional // 报错,因为此时获取的是 primary 对应的 DataSource ,即 users 。
|
||||
public void method031() {
|
||||
OrderDO order = orderMapper.selectById(1);
|
||||
System.out.println(order);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void method032() {
|
||||
UserDO user = userMapper.selectById(1);
|
||||
System.out.println(user);
|
||||
}
|
||||
|
||||
public void method04() {
|
||||
// 查询订单
|
||||
self().method041();
|
||||
// 查询用户
|
||||
self().method042();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@DS(DBConstants.DATASOURCE_ORDERS)
|
||||
public void method041() {
|
||||
OrderDO order = orderMapper.selectById(1);
|
||||
System.out.println(order);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@DS(DBConstants.DATASOURCE_USERS)
|
||||
public void method042() {
|
||||
UserDO user = userMapper.selectById(1);
|
||||
System.out.println(user);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@DS(DBConstants.DATASOURCE_ORDERS)
|
||||
public void method05() {
|
||||
// 查询订单
|
||||
OrderDO order = orderMapper.selectById(1);
|
||||
System.out.println(order);
|
||||
// 查询用户
|
||||
self().method052();
|
||||
}
|
||||
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
@DS(DBConstants.DATASOURCE_USERS)
|
||||
public void method052() {
|
||||
UserDO user = userMapper.selectById(1);
|
||||
System.out.println(user);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
spring:
|
||||
datasource:
|
||||
# datasource 数据源配置内容
|
||||
dynamic:
|
||||
primary: users # TODO 注释
|
||||
datasource:
|
||||
# 订单数据源配置
|
||||
orders:
|
||||
url: jdbc:mysql://127.0.0.1:3306/test_orders?useSSL=false&useUnicode=true&characterEncoding=UTF-8
|
||||
driver-class-name: com.mysql.jdbc.Driver
|
||||
username: root
|
||||
password:
|
||||
# 用户数据源配置
|
||||
users:
|
||||
url: jdbc:mysql://127.0.0.1:3306/test_users?useSSL=false&useUnicode=true&characterEncoding=UTF-8
|
||||
driver-class-name: com.mysql.jdbc.Driver
|
||||
username: root
|
||||
password:
|
||||
|
||||
# mybatis 配置内容
|
||||
mybatis:
|
||||
config-location: classpath:mybatis-config.xml # 配置 MyBatis 配置文件路径
|
||||
mapper-locations: classpath:mapper/*.xml # 配置 Mapper XML 地址
|
||||
type-aliases-package: cn.iocoder.springboot.lab17.dynamicdatasource.dataobject # 配置数据库实体包路径
|
||||
@@ -0,0 +1,16 @@
|
||||
<?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.lab17.dynamicdatasource.mapper.OrderMapper">
|
||||
|
||||
<sql id="FIELDS">
|
||||
id, user_id
|
||||
</sql>
|
||||
|
||||
<select id="selectById" parameterType="Integer" resultType="OrderDO">
|
||||
SELECT
|
||||
<include refid="FIELDS" />
|
||||
FROM orders
|
||||
WHERE id = #{id}
|
||||
</select>
|
||||
|
||||
</mapper>
|
||||
@@ -0,0 +1,16 @@
|
||||
<?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.lab17.dynamicdatasource.mapper.UserMapper">
|
||||
|
||||
<sql id="FIELDS">
|
||||
id, username
|
||||
</sql>
|
||||
|
||||
<select id="selectById" parameterType="Integer" resultType="UserDO">
|
||||
SELECT
|
||||
<include refid="FIELDS" />
|
||||
FROM users
|
||||
WHERE id = #{id}
|
||||
</select>
|
||||
|
||||
</mapper>
|
||||
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
|
||||
<configuration>
|
||||
|
||||
<settings>
|
||||
<!-- 使用驼峰命名法转换字段。 -->
|
||||
<setting name="mapUnderscoreToCamelCase" value="true"/>
|
||||
</settings>
|
||||
|
||||
<typeAliases>
|
||||
<typeAlias alias="Integer" type="java.lang.Integer"/>
|
||||
<typeAlias alias="Long" type="java.lang.Long"/>
|
||||
<typeAlias alias="HashMap" type="java.util.HashMap"/>
|
||||
<typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap"/>
|
||||
<typeAlias alias="ArrayList" type="java.util.ArrayList"/>
|
||||
<typeAlias alias="LinkedList" type="java.util.LinkedList"/>
|
||||
</typeAliases>
|
||||
|
||||
</configuration>
|
||||
@@ -0,0 +1,13 @@
|
||||
CREATE TABLE `users` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户编号',
|
||||
`username` varchar(64) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '账号',
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `idx_username` (`username`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
|
||||
|
||||
|
||||
|
||||
CREATE TABLE `orders` (
|
||||
`id` int(11) DEFAULT NULL COMMENT '订单编号',
|
||||
`user_id` int(16) DEFAULT NULL COMMENT '用户编号'
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='订单表';
|
||||
@@ -0,0 +1,24 @@
|
||||
package cn.iocoder.springboot.lab17.dynamicdatasource.mapper;
|
||||
|
||||
import cn.iocoder.springboot.lab17.dynamicdatasource.Application;
|
||||
import cn.iocoder.springboot.lab17.dynamicdatasource.dataobject.OrderDO;
|
||||
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;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = Application.class)
|
||||
public class OrderMapperTest {
|
||||
|
||||
@Autowired
|
||||
private OrderMapper orderMapper;
|
||||
|
||||
@Test
|
||||
public void testSelectById() {
|
||||
OrderDO order = orderMapper.selectById(1);
|
||||
System.out.println(order);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package cn.iocoder.springboot.lab17.dynamicdatasource.mapper;
|
||||
|
||||
import cn.iocoder.springboot.lab17.dynamicdatasource.Application;
|
||||
import cn.iocoder.springboot.lab17.dynamicdatasource.dataobject.UserDO;
|
||||
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;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = Application.class)
|
||||
public class UserMapperTest {
|
||||
|
||||
@Autowired
|
||||
private UserMapper userMapper;
|
||||
|
||||
@Test
|
||||
public void testSelectById() {
|
||||
UserDO user = userMapper.selectById(1);
|
||||
System.out.println(user);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package cn.iocoder.springboot.lab17.dynamicdatasource.service;
|
||||
|
||||
import cn.iocoder.springboot.lab17.dynamicdatasource.Application;
|
||||
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;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = Application.class)
|
||||
public class OrderServiceTest {
|
||||
|
||||
@Autowired
|
||||
private OrderService orderService;
|
||||
|
||||
@Test
|
||||
public void testMethod01() {
|
||||
orderService.method01();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMethod02() {
|
||||
orderService.method02();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMethod03() {
|
||||
orderService.method03();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMethod04() {
|
||||
orderService.method04();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMethod05() {
|
||||
orderService.method05();
|
||||
}
|
||||
|
||||
}
|
||||
15
lab-17/lab-17-dynamic-datasource-baomidou-02/pom.xml
Normal file
15
lab-17/lab-17-dynamic-datasource-baomidou-02/pom.xml
Normal file
@@ -0,0 +1,15 @@
|
||||
<?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>
|
||||
<artifactId>lab-17</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>lab-17-dynamic-datasource-baomidou-02</artifactId>
|
||||
|
||||
|
||||
</project>
|
||||
49
lab-17/lab-17-dynamic-datasource-mybatis/pom.xml
Normal file
49
lab-17/lab-17-dynamic-datasource-mybatis/pom.xml
Normal file
@@ -0,0 +1,49 @@
|
||||
<?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-17-dynamic-datasource-mybatis</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 的自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>org.mybatis.spring.boot</groupId>
|
||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||
<version>2.1.1</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 保证 Spring AOP 相关的依赖包 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-aspects</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 方便等会写单元测试 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,9 @@
|
||||
package cn.iocoder.springboot.lab17.dynamicdatasource;
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.EnableAspectJAutoProxy;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableAspectJAutoProxy(exposeProxy = true) // http://www.voidcn.com/article/p-zddcuyii-bpt.html
|
||||
public class Application {
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package cn.iocoder.springboot.lab17.dynamicdatasource.config;
|
||||
|
||||
import cn.iocoder.springboot.lab17.dynamicdatasource.constant.DBConstants;
|
||||
import org.apache.ibatis.session.SqlSessionFactory;
|
||||
import org.mybatis.spring.SqlSessionFactoryBean;
|
||||
import org.mybatis.spring.SqlSessionTemplate;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.jdbc.DataSourceBuilder;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
@Configuration
|
||||
@MapperScan(basePackages = "cn.iocoder.springboot.lab17.dynamicdatasource.mapper.orders", sqlSessionTemplateRef = "ordersSqlSessionTemplate")
|
||||
public class MyBatisOrdersConfig {
|
||||
|
||||
/**
|
||||
* 创建 orders 数据源
|
||||
*/
|
||||
@Bean(name = "ordersDataSource")
|
||||
@ConfigurationProperties(prefix = "spring.datasource.orders")
|
||||
public DataSource dataSource() {
|
||||
return DataSourceBuilder.create().build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建 MyBatis SqlSessionFactory
|
||||
*/
|
||||
@Bean(name = "ordersSqlSessionFactory")
|
||||
public SqlSessionFactory sqlSessionFactory() throws Exception {
|
||||
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
|
||||
// 设置 orders 数据源
|
||||
bean.setDataSource(this.dataSource());
|
||||
// 设置 entity 所在包
|
||||
bean.setTypeAliasesPackage("cn.iocoder.springboot.lab17.dynamicdatasource.dataobject");
|
||||
// 设置 config 路径
|
||||
bean.setConfigLocation(new PathMatchingResourcePatternResolver().getResource("classpath:mybatis-config.xml"));
|
||||
// 设置 mapper 路径
|
||||
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/orders/*.xml"));
|
||||
return bean.getObject();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建 MyBatis SqlSessionTemplate
|
||||
*/
|
||||
@Bean(name = "ordersSqlSessionTemplate")
|
||||
public SqlSessionTemplate sqlSessionTemplate() throws Exception {
|
||||
return new SqlSessionTemplate(this.sqlSessionFactory());
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建 orders 数据源的 TransactionManager 事务管理器
|
||||
*/
|
||||
@Bean(name = DBConstants.TX_MANAGER_ORDERS)
|
||||
public PlatformTransactionManager usersTransactionManager() {
|
||||
return new DataSourceTransactionManager(this.dataSource());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package cn.iocoder.springboot.lab17.dynamicdatasource.config;
|
||||
|
||||
import cn.iocoder.springboot.lab17.dynamicdatasource.constant.DBConstants;
|
||||
import org.apache.ibatis.session.SqlSessionFactory;
|
||||
import org.mybatis.spring.SqlSessionFactoryBean;
|
||||
import org.mybatis.spring.SqlSessionTemplate;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.jdbc.DataSourceBuilder;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
@Configuration
|
||||
@MapperScan(basePackages = "cn.iocoder.springboot.lab17.dynamicdatasource.mapper.users", sqlSessionTemplateRef = "usersSqlSessionTemplate")
|
||||
public class MyBatisUsersConfig {
|
||||
|
||||
/**
|
||||
* 创建 users 数据源
|
||||
*/
|
||||
@Bean(name = "usersDataSource")
|
||||
@ConfigurationProperties(prefix = "spring.datasource.users")
|
||||
public DataSource dataSource() {
|
||||
return DataSourceBuilder.create().build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建 MyBatis SqlSessionFactory
|
||||
*/
|
||||
@Bean(name = "sqlSessionFactory")
|
||||
public SqlSessionFactory sqlSessionFactory() throws Exception {
|
||||
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
|
||||
// 设置 users 数据源
|
||||
bean.setDataSource(this.dataSource());
|
||||
// 设置 entity 所在包
|
||||
bean.setTypeAliasesPackage("cn.iocoder.springboot.lab17.dynamicdatasource.dataobject");
|
||||
// 设置 config 路径
|
||||
bean.setConfigLocation(new PathMatchingResourcePatternResolver().getResource("classpath:mybatis-config.xml"));
|
||||
// 设置 mapper 路径
|
||||
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/users/*.xml"));
|
||||
return bean.getObject();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建 MyBatis SqlSessionTemplate
|
||||
*/
|
||||
@Bean(name = "usersSqlSessionTemplate")
|
||||
public SqlSessionTemplate sqlSessionTemplate() throws Exception {
|
||||
return new SqlSessionTemplate(this.sqlSessionFactory());
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建 users 数据源的 TransactionManager 事务管理器
|
||||
*/
|
||||
@Bean(name = DBConstants.TX_MANAGER_USERS)
|
||||
public PlatformTransactionManager ordersTransactionManager() {
|
||||
return new DataSourceTransactionManager(this.dataSource());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package cn.iocoder.springboot.lab17.dynamicdatasource.constant;
|
||||
|
||||
/**
|
||||
* 数据库枚举类
|
||||
*/
|
||||
public class DBConstants {
|
||||
|
||||
/**
|
||||
* 事务管理器 - 订单库
|
||||
*/
|
||||
public static final String TX_MANAGER_ORDERS = "ordersTransactionManager";
|
||||
|
||||
/**
|
||||
* 事务管理器 - 用户库
|
||||
*/
|
||||
public static final String TX_MANAGER_USERS = "usersTransactionManager";
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package cn.iocoder.springboot.lab17.dynamicdatasource.dataobject;
|
||||
|
||||
/**
|
||||
* 订单 DO
|
||||
*/
|
||||
public class OrderDO {
|
||||
|
||||
/**
|
||||
* 订单编号
|
||||
*/
|
||||
private Integer id;
|
||||
/**
|
||||
* 用户编号
|
||||
*/
|
||||
private Integer userId;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public OrderDO setId(Integer id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Integer getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public OrderDO setUserId(Integer userId) {
|
||||
this.userId = userId;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "OrderDO{" +
|
||||
"id=" + id +
|
||||
", userId=" + userId +
|
||||
'}';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package cn.iocoder.springboot.lab17.dynamicdatasource.dataobject;
|
||||
|
||||
/**
|
||||
* 用户 DO
|
||||
*/
|
||||
public class UserDO {
|
||||
|
||||
/**
|
||||
* 用户编号
|
||||
*/
|
||||
private Integer id;
|
||||
/**
|
||||
* 账号
|
||||
*/
|
||||
private String username;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "UserDO{" +
|
||||
"id=" + id +
|
||||
", username='" + username + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package cn.iocoder.springboot.lab17.dynamicdatasource.mapper.orders;
|
||||
|
||||
import cn.iocoder.springboot.lab17.dynamicdatasource.dataobject.OrderDO;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface OrderMapper {
|
||||
|
||||
OrderDO selectById(@Param("id") Integer id);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package cn.iocoder.springboot.lab17.dynamicdatasource.mapper.users;
|
||||
|
||||
import cn.iocoder.springboot.lab17.dynamicdatasource.dataobject.UserDO;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface UserMapper {
|
||||
|
||||
UserDO selectById(@Param("id") Integer id);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package cn.iocoder.springboot.lab17.dynamicdatasource.service;
|
||||
|
||||
import cn.iocoder.springboot.lab17.dynamicdatasource.constant.DBConstants;
|
||||
import cn.iocoder.springboot.lab17.dynamicdatasource.dataobject.OrderDO;
|
||||
import cn.iocoder.springboot.lab17.dynamicdatasource.dataobject.UserDO;
|
||||
import cn.iocoder.springboot.lab17.dynamicdatasource.mapper.orders.OrderMapper;
|
||||
import cn.iocoder.springboot.lab17.dynamicdatasource.mapper.users.UserMapper;
|
||||
import org.springframework.aop.framework.AopContext;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
public class OrderService {
|
||||
|
||||
@Autowired
|
||||
private OrderMapper orderMapper;
|
||||
@Autowired
|
||||
private UserMapper userMapper;
|
||||
|
||||
private OrderService self() {
|
||||
return (OrderService) AopContext.currentProxy();
|
||||
}
|
||||
|
||||
public void method01() {
|
||||
// 查询订单
|
||||
OrderDO order = orderMapper.selectById(1);
|
||||
System.out.println(order);
|
||||
// 查询用户
|
||||
UserDO user = userMapper.selectById(1);
|
||||
System.out.println(user);
|
||||
}
|
||||
|
||||
@Transactional // 报错,找不到事务管理器
|
||||
public void method02() {
|
||||
// 查询订单
|
||||
OrderDO order = orderMapper.selectById(1);
|
||||
System.out.println(order);
|
||||
// 查询用户
|
||||
UserDO user = userMapper.selectById(1);
|
||||
System.out.println(user);
|
||||
}
|
||||
|
||||
public void method03() {
|
||||
// 查询订单
|
||||
self().method031();
|
||||
// 查询用户
|
||||
self().method032();
|
||||
}
|
||||
|
||||
@Transactional(transactionManager = DBConstants.TX_MANAGER_ORDERS)
|
||||
public void method031() {
|
||||
OrderDO order = orderMapper.selectById(1);
|
||||
System.out.println(order);
|
||||
}
|
||||
|
||||
@Transactional(transactionManager = DBConstants.TX_MANAGER_USERS)
|
||||
public void method032() {
|
||||
UserDO user = userMapper.selectById(1);
|
||||
System.out.println(user);
|
||||
}
|
||||
|
||||
@Transactional(transactionManager = DBConstants.TX_MANAGER_ORDERS)
|
||||
public void method05() {
|
||||
// 查询订单
|
||||
OrderDO order = orderMapper.selectById(1);
|
||||
System.out.println(order);
|
||||
// 查询用户
|
||||
self().method052();
|
||||
}
|
||||
|
||||
@Transactional(transactionManager = DBConstants.TX_MANAGER_USERS,
|
||||
propagation = Propagation.REQUIRES_NEW)
|
||||
public void method052() {
|
||||
UserDO user = userMapper.selectById(1);
|
||||
System.out.println(user);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
spring:
|
||||
# datasource 数据源配置内容
|
||||
datasource:
|
||||
# 订单数据源配置
|
||||
orders:
|
||||
jdbc-url: jdbc:mysql://127.0.0.1:3306/test_orders?useSSL=false&useUnicode=true&characterEncoding=UTF-8
|
||||
driver-class-name: com.mysql.jdbc.Driver
|
||||
username: root
|
||||
password:
|
||||
# 用户数据源配置
|
||||
users:
|
||||
jdbc-url: jdbc:mysql://127.0.0.1:3306/test_users?useSSL=false&useUnicode=true&characterEncoding=UTF-8
|
||||
driver-class-name: com.mysql.jdbc.Driver
|
||||
username: root
|
||||
password:
|
||||
|
||||
# mybatis 配置内容
|
||||
#mybatis:
|
||||
# config-location: classpath:mybatis-config.xml # 配置 MyBatis 配置文件路径
|
||||
# type-aliases-package: cn.iocoder.springboot.lab17.dynamicdatasource.dataobject # 配置数据库实体包路径
|
||||
@@ -0,0 +1,16 @@
|
||||
<?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.lab17.dynamicdatasource.mapper.orders.OrderMapper">
|
||||
|
||||
<sql id="FIELDS">
|
||||
id, user_id
|
||||
</sql>
|
||||
|
||||
<select id="selectById" parameterType="Integer" resultType="OrderDO">
|
||||
SELECT
|
||||
<include refid="FIELDS" />
|
||||
FROM orders
|
||||
WHERE id = #{id}
|
||||
</select>
|
||||
|
||||
</mapper>
|
||||
@@ -0,0 +1,16 @@
|
||||
<?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.lab17.dynamicdatasource.mapper.users.UserMapper">
|
||||
|
||||
<sql id="FIELDS">
|
||||
id, username
|
||||
</sql>
|
||||
|
||||
<select id="selectById" parameterType="Integer" resultType="UserDO">
|
||||
SELECT
|
||||
<include refid="FIELDS" />
|
||||
FROM users
|
||||
WHERE id = #{id}
|
||||
</select>
|
||||
|
||||
</mapper>
|
||||
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
|
||||
<configuration>
|
||||
|
||||
<settings>
|
||||
<!-- 使用驼峰命名法转换字段。 -->
|
||||
<setting name="mapUnderscoreToCamelCase" value="true"/>
|
||||
</settings>
|
||||
|
||||
<typeAliases>
|
||||
<typeAlias alias="Integer" type="java.lang.Integer"/>
|
||||
<typeAlias alias="Long" type="java.lang.Long"/>
|
||||
<typeAlias alias="HashMap" type="java.util.HashMap"/>
|
||||
<typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap"/>
|
||||
<typeAlias alias="ArrayList" type="java.util.ArrayList"/>
|
||||
<typeAlias alias="LinkedList" type="java.util.LinkedList"/>
|
||||
</typeAliases>
|
||||
|
||||
</configuration>
|
||||
@@ -0,0 +1,13 @@
|
||||
CREATE TABLE `users` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户编号',
|
||||
`username` varchar(64) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '账号',
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `idx_username` (`username`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
|
||||
|
||||
|
||||
|
||||
CREATE TABLE `orders` (
|
||||
`id` int(11) DEFAULT NULL COMMENT '订单编号',
|
||||
`user_id` int(16) DEFAULT NULL COMMENT '用户编号'
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='订单表';
|
||||
@@ -0,0 +1,24 @@
|
||||
package cn.iocoder.springboot.lab17.dynamicdatasource.mapper.orders;
|
||||
|
||||
import cn.iocoder.springboot.lab17.dynamicdatasource.Application;
|
||||
import cn.iocoder.springboot.lab17.dynamicdatasource.dataobject.OrderDO;
|
||||
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;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = Application.class)
|
||||
public class OrderMapperTest {
|
||||
|
||||
@Autowired
|
||||
private OrderMapper orderMapper;
|
||||
|
||||
@Test
|
||||
public void testSelectById() {
|
||||
OrderDO order = orderMapper.selectById(1);
|
||||
System.out.println(order);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package cn.iocoder.springboot.lab17.dynamicdatasource.mapper.users;
|
||||
|
||||
import cn.iocoder.springboot.lab17.dynamicdatasource.Application;
|
||||
import cn.iocoder.springboot.lab17.dynamicdatasource.dataobject.UserDO;
|
||||
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;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = Application.class)
|
||||
public class UserMapperTest {
|
||||
|
||||
@Autowired
|
||||
private UserMapper userMapper;
|
||||
|
||||
@Test
|
||||
public void testSelectById() {
|
||||
UserDO user = userMapper.selectById(1);
|
||||
System.out.println(user);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
package cn.iocoder.springboot.lab17.dynamicdatasource;
|
||||
@@ -0,0 +1,37 @@
|
||||
package cn.iocoder.springboot.lab17.dynamicdatasource.service;
|
||||
|
||||
import cn.iocoder.springboot.lab17.dynamicdatasource.Application;
|
||||
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;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = Application.class)
|
||||
public class OrderServiceTest {
|
||||
|
||||
@Autowired
|
||||
private OrderService orderService;
|
||||
|
||||
@Test
|
||||
public void testMethod01() {
|
||||
orderService.method01();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMethod02() {
|
||||
orderService.method02();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMethod03() {
|
||||
orderService.method03();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMethod05() {
|
||||
orderService.method05();
|
||||
}
|
||||
|
||||
}
|
||||
21
lab-17/pom.xml
Normal file
21
lab-17/pom.xml
Normal file
@@ -0,0 +1,21 @@
|
||||
<?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>
|
||||
<artifactId>labs-parent</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>lab-17</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<modules>
|
||||
<module>lab-17-dynamic-datasource-baomidou-01</module>
|
||||
<module>lab-17-dynamic-datasource-baomidou-02</module>
|
||||
<module>lab-17-dynamic-datasource-mybatis</module>
|
||||
</modules>
|
||||
|
||||
|
||||
</project>
|
||||
Reference in New Issue
Block a user