mirror of
https://gitee.com/yudaocode/SpringBoot-Labs.git
synced 2026-09-03 05:53:54 +08:00
增加分库分表示例
This commit is contained in:
@@ -2,10 +2,8 @@ package cn.iocoder.springboot.lab18.shardingdatasource;
|
||||
|
||||
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
|
||||
@MapperScan(basePackages = "cn.iocoder.springboot.lab18.shardingdatasource.mapper")
|
||||
public class Application {
|
||||
}
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package cn.iocoder.springboot.lab18.shardingdatasource.dataobject;
|
||||
|
||||
/**
|
||||
* 订单配置 DO
|
||||
*/
|
||||
public class OrderConfigDO {
|
||||
|
||||
/**
|
||||
* 编号
|
||||
*/
|
||||
private Integer id;
|
||||
/**
|
||||
* 支付超时时间
|
||||
*
|
||||
* 单位:分钟
|
||||
*/
|
||||
private Integer payTimeout;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public OrderConfigDO setId(Integer id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Integer getPayTimeout() {
|
||||
return payTimeout;
|
||||
}
|
||||
|
||||
public OrderConfigDO setPayTimeout(Integer payTimeout) {
|
||||
this.payTimeout = payTimeout;
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -8,17 +8,17 @@ public class OrderDO {
|
||||
/**
|
||||
* 订单编号
|
||||
*/
|
||||
private Integer id;
|
||||
private Long id;
|
||||
/**
|
||||
* 用户编号
|
||||
*/
|
||||
private Integer userId;
|
||||
|
||||
public Integer getId() {
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public OrderDO setId(Integer id) {
|
||||
public OrderDO setId(Long id) {
|
||||
this.id = id;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package cn.iocoder.springboot.lab18.shardingdatasource.mapper;
|
||||
|
||||
import cn.iocoder.springboot.lab18.shardingdatasource.dataobject.OrderConfigDO;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface OrderConfigMapper {
|
||||
|
||||
OrderConfigDO selectById(@Param("id") Integer id);
|
||||
|
||||
}
|
||||
@@ -4,9 +4,15 @@ import cn.iocoder.springboot.lab18.shardingdatasource.dataobject.OrderDO;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface OrderMapper {
|
||||
|
||||
OrderDO selectById(@Param("id") Integer id);
|
||||
|
||||
List<OrderDO> selectListByUserId(@Param("userId") Integer userId);
|
||||
|
||||
void insert(OrderDO order);
|
||||
|
||||
}
|
||||
|
||||
@@ -3,39 +3,47 @@ spring:
|
||||
shardingsphere:
|
||||
datasource:
|
||||
# 所有数据源的名字
|
||||
names: ds-orders-00, ds-orders-01
|
||||
names: ds-orders-0, ds-orders-1
|
||||
# 订单 orders 数据源配置 00
|
||||
ds-orders-00:
|
||||
ds-orders-0:
|
||||
type: com.zaxxer.hikari.HikariDataSource # 使用 Hikari 数据库连接池
|
||||
driver-class-name: com.mysql.jdbc.Driver
|
||||
jdbc-url: jdbc:mysql://127.0.0.1:3306/test_orders?useSSL=false&useUnicode=true&characterEncoding=UTF-8
|
||||
jdbc-url: jdbc:mysql://127.0.0.1:3306/lab18_orders_0?useSSL=false&useUnicode=true&characterEncoding=UTF-8
|
||||
username: root
|
||||
password:
|
||||
# 订单 orders 数据源配置 01
|
||||
ds-orders-01:
|
||||
ds-orders-1:
|
||||
type: com.zaxxer.hikari.HikariDataSource # 使用 Hikari 数据库连接池
|
||||
driver-class-name: com.mysql.jdbc.Driver
|
||||
jdbc-url: jdbc:mysql://47.112.193.81:3306/testb5f4?useSSL=false&useUnicode=true&characterEncoding=UTF-8
|
||||
username: testb5f4
|
||||
password: F4df4db0ed86@11
|
||||
jdbc-url: jdbc:mysql://127.0.0.1:3306/lab18_orders_1?useSSL=false&useUnicode=true&characterEncoding=UTF-8
|
||||
username: root
|
||||
password:
|
||||
# 分片规则
|
||||
sharding:
|
||||
tables:
|
||||
# orders 表配置
|
||||
orders:
|
||||
actualDataNodes: ds-$->{0..1}.orders_$->{0..7} # 映射到 ds-orders 数据源的 orders 表
|
||||
# actualDataNodes: ds-orders-$->{0..1}.orders_$->{0..4} # 映射到 ds-orders 数据源的 orders 表
|
||||
# actualDataNodes: ds-orders-0.orders_0, ds-orders-0.orders_2, ds-orders-0.orders_4, ds-orders-0.orders_6, ds-orders-1.orders_1, ds-orders-1.orders_3, ds-orders-1.orders_5, ds-orders-1.orders_7
|
||||
actualDataNodes: ds-orders-0.orders_$->{[0,2,4,6]}, ds-orders-1.orders_$->{[1,3,5,7]} # 映射到 ds-orders-0 和 ds-orders-1 数据源的 orders 表们
|
||||
key-generator: # 主键生成策略
|
||||
column: id
|
||||
type: SNOWFLAKE
|
||||
database-strategy:
|
||||
inline:
|
||||
algorithm-expression: orders_$->{user_id % 2}
|
||||
algorithm-expression: ds-orders-$->{user_id % 2}
|
||||
sharding-column: user_id
|
||||
table-strategy:
|
||||
inline:
|
||||
algorithm-expression: orders_$->{user_id % 8}
|
||||
sharding-column: user_id
|
||||
|
||||
# order_config 表配置
|
||||
order_config:
|
||||
actualDataNodes: ds-orders-0.order_config # 仅映射到 ds-orders-0 数据源的 order_config 表
|
||||
# 拓展属性配置
|
||||
props:
|
||||
sql:
|
||||
show: true # 打印 SQL
|
||||
|
||||
# mybatis 配置内容
|
||||
mybatis:
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
<?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">
|
||||
<mapper namespace="cn.iocoder.springboot.lab18.shardingdatasource.mapper.OrderConfigMapper">
|
||||
|
||||
<sql id="FIELDS">
|
||||
id, username
|
||||
id, pay_timeout
|
||||
</sql>
|
||||
|
||||
<select id="selectById" parameterType="Integer" resultType="UserDO">
|
||||
<select id="selectById" parameterType="Integer" resultType="OrderConfigDO">
|
||||
SELECT
|
||||
<include refid="FIELDS" />
|
||||
FROM users
|
||||
FROM order_config
|
||||
WHERE id = #{id}
|
||||
</select>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?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">
|
||||
<mapper namespace="cn.iocoder.springboot.lab18.shardingdatasource.mapper.OrderMapper">
|
||||
|
||||
<sql id="FIELDS">
|
||||
id, user_id
|
||||
@@ -13,4 +13,19 @@
|
||||
WHERE id = #{id}
|
||||
</select>
|
||||
|
||||
<select id="selectListByUserId" parameterType="Integer" resultType="OrderDO">
|
||||
SELECT
|
||||
<include refid="FIELDS" />
|
||||
FROM orders
|
||||
WHERE user_id = #{userId}
|
||||
</select>
|
||||
|
||||
<insert id="insert" parameterType="OrderDO" useGeneratedKeys="true" keyProperty="id">
|
||||
INSERT INTO orders (
|
||||
user_id
|
||||
) VALUES (
|
||||
#{userId}
|
||||
)
|
||||
</insert>
|
||||
|
||||
</mapper>
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
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,56 @@
|
||||
SET NAMES utf8mb4;
|
||||
SET FOREIGN_KEY_CHECKS = 0;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for orders_0
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `orders_0`;
|
||||
CREATE TABLE `orders_0` (
|
||||
`id` bigint(11) NOT NULL AUTO_INCREMENT COMMENT '订单编号',
|
||||
`user_id` int(16) DEFAULT NULL COMMENT '用户编号',
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='订单表';
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for orders_2
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `orders_2`;
|
||||
CREATE TABLE `orders_2` (
|
||||
`id` bigint(11) NOT NULL AUTO_INCREMENT COMMENT '订单编号',
|
||||
`user_id` int(16) DEFAULT NULL COMMENT '用户编号',
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='订单表';
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for orders_4
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `orders_4`;
|
||||
CREATE TABLE `orders_4` (
|
||||
`id` bigint(11) NOT NULL AUTO_INCREMENT COMMENT '订单编号',
|
||||
`user_id` int(16) DEFAULT NULL COMMENT '用户编号',
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='订单表';
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for orders_6
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `orders_6`;
|
||||
CREATE TABLE `orders_6` (
|
||||
`id` bigint(11) NOT NULL AUTO_INCREMENT COMMENT '订单编号',
|
||||
`user_id` int(16) DEFAULT NULL COMMENT '用户编号',
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='订单表';
|
||||
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for order_config
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `order_config`;
|
||||
CREATE TABLE `order_config` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '编号',
|
||||
`pay_timeout` int(11) DEFAULT NULL COMMENT '支付超时时间;单位:分钟',
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4;
|
||||
|
||||
SET FOREIGN_KEY_CHECKS = 1;
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
SET NAMES utf8mb4;
|
||||
SET FOREIGN_KEY_CHECKS = 0;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for orders_1
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `orders_1`;
|
||||
CREATE TABLE `orders_1` (
|
||||
`id` bigint(11) NOT NULL AUTO_INCREMENT COMMENT '订单编号',
|
||||
`user_id` int(16) DEFAULT NULL COMMENT '用户编号',
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=400675304294580226 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='订单表';
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for orders_3
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `orders_3`;
|
||||
CREATE TABLE `orders_3` (
|
||||
`id` bigint(11) NOT NULL AUTO_INCREMENT COMMENT '订单编号',
|
||||
`user_id` int(16) DEFAULT NULL COMMENT '用户编号',
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='订单表';
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for orders_5
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `orders_5`;
|
||||
CREATE TABLE `orders_5` (
|
||||
`id` bigint(11) NOT NULL AUTO_INCREMENT COMMENT '订单编号',
|
||||
`user_id` int(16) DEFAULT NULL COMMENT '用户编号',
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='订单表';
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for orders_7
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `orders_7`;
|
||||
CREATE TABLE `orders_7` (
|
||||
`id` bigint(11) NOT NULL AUTO_INCREMENT COMMENT '订单编号',
|
||||
`user_id` int(16) DEFAULT NULL COMMENT '用户编号',
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='订单表';
|
||||
|
||||
SET FOREIGN_KEY_CHECKS = 1;
|
||||
@@ -0,0 +1,24 @@
|
||||
package cn.iocoder.springboot.lab18.shardingdatasource.mapper;
|
||||
|
||||
import cn.iocoder.springboot.lab18.shardingdatasource.Application;
|
||||
import cn.iocoder.springboot.lab18.shardingdatasource.dataobject.OrderConfigDO;
|
||||
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 OrderConfigMapperTest {
|
||||
|
||||
@Autowired
|
||||
private OrderConfigMapper orderConfigMapper;
|
||||
|
||||
@Test
|
||||
public void testSelectById() {
|
||||
OrderConfigDO orderConfig = orderConfigMapper.selectById(1);
|
||||
System.out.println(orderConfig);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -8,6 +8,8 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = Application.class)
|
||||
public class OrderMapperTest {
|
||||
@@ -21,4 +23,17 @@ public class OrderMapperTest {
|
||||
System.out.println(order);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSelectListByUserId() {
|
||||
List<OrderDO> orders = orderMapper.selectListByUserId(1);
|
||||
System.out.println(orders.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInsert() {
|
||||
OrderDO order = new OrderDO();
|
||||
order.setUserId(1);
|
||||
orderMapper.insert(order);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
50
lab-18/lab-18-sharding-datasource-02/pom.xml
Normal file
50
lab-18/lab-18-sharding-datasource-02/pom.xml
Normal file
@@ -0,0 +1,50 @@
|
||||
<?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-18-sharding-datasource-02</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>
|
||||
|
||||
<!-- 实现对 Sharding-JDBC 的自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>org.apache.shardingsphere</groupId>
|
||||
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
|
||||
<version>4.0.0-RC2</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 实现对 MyBatis Plus 的自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||
<version>3.2.0</version>
|
||||
</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.lab18.shardingdatasource;
|
||||
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
@MapperScan(basePackages = "cn.iocoder.springboot.lab18.shardingdatasource.mapper")
|
||||
public class Application {
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package cn.iocoder.springboot.lab18.shardingdatasource.dataobject;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
|
||||
/**
|
||||
* 订单 DO
|
||||
*/
|
||||
@TableName(value = "orders")
|
||||
public class OrderDO {
|
||||
|
||||
/**
|
||||
* 订单编号
|
||||
*/
|
||||
private Long id;
|
||||
/**
|
||||
* 用户编号
|
||||
*/
|
||||
private Integer userId;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public OrderDO setId(Long 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,10 @@
|
||||
package cn.iocoder.springboot.lab18.shardingdatasource.mapper;
|
||||
|
||||
import cn.iocoder.springboot.lab18.shardingdatasource.dataobject.OrderDO;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface OrderMapper extends BaseMapper<OrderDO> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package cn.iocoder.springboot.lab18.shardingdatasource.service;
|
||||
|
||||
import cn.iocoder.springboot.lab18.shardingdatasource.dataobject.OrderDO;
|
||||
import cn.iocoder.springboot.lab18.shardingdatasource.mapper.OrderMapper;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
public class OrderService {
|
||||
|
||||
@Autowired
|
||||
private OrderMapper orderMapper;
|
||||
|
||||
@Transactional
|
||||
public void add(OrderDO order) {
|
||||
// 这里先假模假样的读取一下。读取从库
|
||||
OrderDO exists = orderMapper.selectById(1);
|
||||
System.out.println(exists);
|
||||
|
||||
// 插入订单
|
||||
orderMapper.insert(order);
|
||||
|
||||
// 这里先假模假样的读取一下。读取主库
|
||||
exists = orderMapper.selectById(1);
|
||||
System.out.println(exists);
|
||||
}
|
||||
|
||||
public OrderDO findById(Integer id) {
|
||||
return orderMapper.selectById(id);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
spring:
|
||||
# ShardingSphere 配置项
|
||||
shardingsphere:
|
||||
# 数据源配置
|
||||
datasource:
|
||||
# 所有数据源的名字
|
||||
names: ds-master, ds-slave-1, ds-slave-2
|
||||
# 订单 orders 主库的数据源配置
|
||||
ds-master:
|
||||
type: com.zaxxer.hikari.HikariDataSource # 使用 Hikari 数据库连接池
|
||||
driver-class-name: com.mysql.jdbc.Driver
|
||||
jdbc-url: jdbc:mysql://127.0.0.1:3306/test_orders?useSSL=false&useUnicode=true&characterEncoding=UTF-8
|
||||
username: root
|
||||
password:
|
||||
# 订单 orders 从库数据源配置
|
||||
ds-slave-1:
|
||||
type: com.zaxxer.hikari.HikariDataSource # 使用 Hikari 数据库连接池
|
||||
driver-class-name: com.mysql.jdbc.Driver
|
||||
jdbc-url: jdbc:mysql://127.0.0.1:3306/test_orders_01?useSSL=false&useUnicode=true&characterEncoding=UTF-8
|
||||
username: root
|
||||
password:
|
||||
# 订单 orders 从库数据源配置
|
||||
ds-slave-2:
|
||||
type: com.zaxxer.hikari.HikariDataSource # 使用 Hikari 数据库连接池
|
||||
driver-class-name: com.mysql.jdbc.Driver
|
||||
jdbc-url: jdbc:mysql://127.0.0.1:3306/test_orders_02?useSSL=false&useUnicode=true&characterEncoding=UTF-8
|
||||
username: root
|
||||
password:
|
||||
# 读写分离配置,对应 YamlMasterSlaveRuleConfiguration 配置类
|
||||
masterslave:
|
||||
name: ms # 名字,任意,需要保证唯一
|
||||
master-data-source-name: ds-master # 主库数据源
|
||||
slave-data-source-names: ds-slave-1, ds-slave-2 # 从库数据源
|
||||
# 拓展属性配置
|
||||
props:
|
||||
sql:
|
||||
show: true # 打印 SQL
|
||||
|
||||
# mybatis-plus 配置内容
|
||||
mybatis-plus:
|
||||
configuration:
|
||||
map-underscore-to-camel-case: true # 虽然默认为 true ,但是还是显示去指定下。
|
||||
global-config:
|
||||
db-config:
|
||||
id-type: none # 虽然 MyBatis Plus 也提供 ID 生成策略,但是还是使用 Sharding-JDBC 的
|
||||
logic-delete-value: 1 # 逻辑已删除值(默认为 1)
|
||||
logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)
|
||||
mapper-locations: classpath*:mapper/*.xml
|
||||
type-aliases-package: cn.iocoder.springboot.lab18.shardingdatasource.dataobject
|
||||
@@ -0,0 +1,45 @@
|
||||
package cn.iocoder.springboot.lab18.shardingdatasource.mapper;
|
||||
|
||||
import cn.iocoder.springboot.lab18.shardingdatasource.Application;
|
||||
import cn.iocoder.springboot.lab18.shardingdatasource.dataobject.OrderDO;
|
||||
import org.apache.shardingsphere.api.hint.HintManager;
|
||||
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() { // 测试从库的负载均衡
|
||||
for (int i = 0; i < 10; i++) {
|
||||
OrderDO order = orderMapper.selectById(1);
|
||||
System.out.println(order);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSelectById02() { // 测试强制访问主库
|
||||
try (HintManager hintManager = HintManager.getInstance()) {
|
||||
// 设置强制访问主库
|
||||
hintManager.setMasterRouteOnly();
|
||||
// 执行查询
|
||||
OrderDO order = orderMapper.selectById(1);
|
||||
System.out.println(order);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInsert() { // 插入
|
||||
OrderDO order = new OrderDO();
|
||||
order.setUserId(10);
|
||||
orderMapper.insert(order);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package cn.iocoder.springboot.lab18.shardingdatasource.service;
|
||||
|
||||
import cn.iocoder.springboot.lab18.shardingdatasource.Application;
|
||||
import cn.iocoder.springboot.lab18.shardingdatasource.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 OrderServiceTest {
|
||||
|
||||
@Autowired
|
||||
private OrderService orderService;
|
||||
|
||||
@Test
|
||||
public void testAdd() {
|
||||
OrderDO order = new OrderDO();
|
||||
order.setUserId(20);
|
||||
orderService.add(order);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindById() {
|
||||
OrderDO order = orderService.findById(1);
|
||||
System.out.println(order);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -13,6 +13,7 @@
|
||||
<packaging>pom</packaging>
|
||||
<modules>
|
||||
<module>lab-18-sharding-datasource-01</module>
|
||||
<module>lab-18-sharding-datasource-02</module>
|
||||
</modules>
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user