Skip to content

Commit 7061c8d

Browse files
chengxy-ndsuf203737
authored andcommitted
shardingsphere 默认分片策略
1 parent 84df570 commit 7061c8d

File tree

46 files changed

+1388
-732
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1388
-732
lines changed

pom.xml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
<packaging>pom</packaging>
1515

1616
<modules>
17+
<!-- springboot2.0实战 -->
1718
<module>springboot101</module>
19+
<!-- shardingsphere5.x实战 -->
1820
<module>shardingsphere101</module>
1921
</modules>
2022

@@ -67,12 +69,6 @@
6769
<version>1.2.83</version>
6870
</dependency>
6971

70-
<!-- thymeleaf 页面模版 -->
71-
<dependency>
72-
<groupId>org.springframework.boot</groupId>
73-
<artifactId>spring-boot-starter-thymeleaf</artifactId>
74-
</dependency>
75-
7672
</dependencies>
7773

7874
<build>

shardingsphere101/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
快速实现分库分表的两种方式
1+
本项目的主要内容:
22

3-
文件链接:[SpringBoot 2 种方式快速实现分库分表,轻松拿捏!](https://mp.weixin.qq.com/s/XdlJK170YZEwof6DzZlbZg)
3+
1、默认的分库分表策略
4+
2、广播表
5+
3、绑定表的使用

shardingsphere101/pom.xml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,21 @@
1717
<properties>
1818
<java.version>8</java.version>
1919
<shardingsphere.version>5.2.0</shardingsphere.version>
20+
<mybatis.plus.version>3.5.3.1</mybatis.plus.version>
2021
</properties>
2122

2223
<modules>
2324
<module>shardingsphere-2fastcode</module>
24-
<module>shardingsphere-default</module>
25+
<module>shardingsphere-default-strategy</module>
2526
</modules>
2627

2728
<dependencies>
2829

30+
<!-- MyBatis Plus -->
2931
<dependency>
30-
<groupId>org.springframework.boot</groupId>
31-
<artifactId>spring-boot-starter-data-jpa</artifactId>
32+
<groupId>com.baomidou</groupId>
33+
<artifactId>mybatis-plus-boot-starter</artifactId>
34+
<version>${mybatis.plus.version}</version>
3235
</dependency>
3336

3437
<!-- 必须引入的包 mysql -->

shardingsphere101/shardingsphere-2fastcode/pom.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
<name>shardingsphere-2fastcode</name>
1313

1414
<dependencies>
15-
16-
15+
<dependency>
16+
<groupId>org.springframework.boot</groupId>
17+
<artifactId>spring-boot-starter-data-jpa</artifactId>
18+
</dependency>
1719
</dependencies>
1820

1921
<build>

shardingsphere101/shardingsphere-2fastcode/src/main/java/com/shardingsphere_101/conf/ShardingConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ public DataSource getShardingDataSource() throws SQLException {
3232
// 分片rules规则配置
3333
ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
3434

35+
shardingRuleConfig.setBroadcastTables(Collections.singletonList("t_city_dict"));
3536
shardingRuleConfig.setShardingAlgorithms(getShardingAlgorithms());
36-
3737
// 配置 t_order 表分片规则
3838
ShardingTableRuleConfiguration orderTableRuleConfig = new ShardingTableRuleConfiguration("t_order", "db${0..1}.t_order_${0..2}");
3939
orderTableRuleConfig.setTableShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "table-inline"));
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

shardingsphere101/shardingsphere-default/pom.xml renamed to shardingsphere101/shardingsphere-default-strategy/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
<artifactId>shardingsphere101</artifactId>
88
<version>0.0.1-SNAPSHOT</version>
99
</parent>
10-
<artifactId>shardingsphere-default</artifactId>
10+
<artifactId>shardingsphere-default-strategy</artifactId>
1111
<version>0.0.1-SNAPSHOT</version>
12-
<name>shardingsphere-default</name>
12+
<name>shardingsphere-default-strategy</name>
1313

1414
<dependencies>
1515

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
CREATE TABLE t_city_dict (
2+
id BIGINT, -- 城市ID
3+
city_name VARCHAR(255) NOT NULL, -- 城市名称
4+
province_name VARCHAR(255) NOT NULL, -- 省份名称
5+
country_name VARCHAR(255) NOT NULL, -- 国家名称
6+
population INT, -- 城市人口数量
7+
area FLOAT, -- 城市面积
8+
timezone VARCHAR(255), -- 所属时区
9+
postal_code VARCHAR(10), -- 邮政编码
10+
created_date datetime DEFAULT CURRENT_TIMESTAMP,
11+
updated_date datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
12+
PRIMARY KEY (`id`)
13+
) ;
14+
15+
16+
CREATE TABLE t_order_0 (
17+
order_id INT PRIMARY KEY,
18+
order_number VARCHAR(20),
19+
customer_id INT,
20+
order_date DATE,
21+
total_amount DECIMAL(10, 2)
22+
);
23+
24+
25+
CREATE TABLE t_order_1 (
26+
order_id INT PRIMARY KEY,
27+
order_number VARCHAR(20),
28+
customer_id INT,
29+
order_date DATE,
30+
total_amount DECIMAL(10, 2)
31+
);
32+
33+
CREATE TABLE t_order_2 (
34+
order_id INT PRIMARY KEY,
35+
order_number VARCHAR(20),
36+
customer_id INT,
37+
order_date DATE,
38+
total_amount DECIMAL(10, 2)
39+
);
40+
41+
-- 创建 t_order_item 表
42+
CREATE TABLE t_order_item_0 (
43+
item_id INT PRIMARY KEY,
44+
order_id INT,
45+
product_id INT,
46+
quantity INT,
47+
unit_price DECIMAL(8, 2)
48+
);
49+
50+
CREATE TABLE t_order_item_1 (
51+
item_id INT PRIMARY KEY,
52+
order_id INT,
53+
product_id INT,
54+
quantity INT,
55+
unit_price DECIMAL(8, 2)
56+
);
57+
58+
CREATE TABLE t_order_item_2 (
59+
item_id INT PRIMARY KEY,
60+
order_id INT,
61+
product_id INT,
62+
quantity INT,
63+
unit_price DECIMAL(8, 2)
64+
);
65+
66+

shardingsphere101/shardingsphere-default/src/main/java/com/shardingsphere_101/Application.java renamed to shardingsphere101/shardingsphere-default-strategy/src/main/java/com/shardingsphere_101/StrategyApplication.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
55

66
@SpringBootApplication
7-
public class Application {
7+
public class
8+
StrategyApplication {
89

910
public static void main(String[] args) {
10-
SpringApplication.run(Application.class, args);
11+
SpringApplication.run(StrategyApplication.class, args);
1112
}
1213

1314
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.shardingsphere_101.controller;
2+
3+
import com.shardingsphere_101.dao.OrderItemMapper;
4+
import com.shardingsphere_101.dao.OrderMapper;
5+
import com.shardingsphere_101.entity.Order;
6+
import com.shardingsphere_101.entity.OrderItem;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.RestController;
9+
10+
import javax.annotation.Resource;
11+
import java.math.BigDecimal;
12+
import java.util.Date;
13+
14+
/**
15+
* 公众号:程序员小富
16+
*
17+
* @author Jiahai
18+
*/
19+
@RestController
20+
@RequestMapping("/order")
21+
public class OrderController {
22+
23+
@Resource
24+
private OrderMapper orderMapper;
25+
26+
@Resource
27+
private OrderItemMapper orderItemMapper;
28+
29+
30+
@RequestMapping("/test1")
31+
public String test1() {
32+
for (int i = 0; i < 3; i++) {
33+
Order order = new Order();
34+
order.setOrderNumber("WIN000" + i);
35+
order.setCustomerId((long) i);
36+
order.setOrderDate(new Date());
37+
order.setTotalAmount(new BigDecimal("0" + i));
38+
orderMapper.insert(order);
39+
OrderItem orderItem = new OrderItem();
40+
orderItem.setOrderId(order.getOrderId());
41+
orderItem.setProductId(i);
42+
orderItem.setQuantity(i);
43+
orderItem.setUnitPrice(new BigDecimal("0" + i));
44+
orderItemMapper.insert(orderItem);
45+
}
46+
return "ok";
47+
}
48+
}

0 commit comments

Comments
 (0)