Skip to content

Commit e3511cc

Browse files
committed
✨ spring-boot-demo-sharding-jdbc 完成
1 parent 74cb8fa commit e3511cc

File tree

9 files changed

+367
-42
lines changed

9 files changed

+367
-42
lines changed
Lines changed: 69 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,76 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
33
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4-
<modelVersion>4.0.0</modelVersion>
4+
<modelVersion>4.0.0</modelVersion>
55

6-
<artifactId>spring-boot-demo-sharding-jdbc</artifactId>
6+
<artifactId>spring-boot-demo-sharding-jdbc</artifactId>
7+
<version>1.0.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>spring-boot-demo-sharding-jdbc</name>
11+
<description>Demo project for Spring Boot</description>
12+
13+
<parent>
14+
<groupId>com.xkcoding</groupId>
15+
<artifactId>spring-boot-demo</artifactId>
716
<version>1.0.0-SNAPSHOT</version>
8-
<packaging>jar</packaging>
9-
10-
<name>spring-boot-demo-sharding-jdbc</name>
11-
<description>Demo project for Spring Boot</description>
12-
13-
<parent>
14-
<groupId>com.xkcoding</groupId>
15-
<artifactId>spring-boot-demo</artifactId>
16-
<version>1.0.0-SNAPSHOT</version>
17-
</parent>
18-
19-
<properties>
20-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
21-
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
22-
<java.version>1.8</java.version>
23-
</properties>
24-
25-
<dependencies>
26-
<dependency>
27-
<groupId>org.springframework.boot</groupId>
28-
<artifactId>spring-boot-starter</artifactId>
29-
</dependency>
30-
31-
<dependency>
32-
<groupId>org.springframework.boot</groupId>
33-
<artifactId>spring-boot-starter-test</artifactId>
34-
<scope>test</scope>
35-
</dependency>
36-
</dependencies>
37-
38-
<build>
39-
<finalName>spring-boot-demo-sharding-jdbc</finalName>
40-
<plugins>
41-
<plugin>
42-
<groupId>org.springframework.boot</groupId>
43-
<artifactId>spring-boot-maven-plugin</artifactId>
44-
</plugin>
45-
</plugins>
46-
</build>
17+
</parent>
18+
19+
<properties>
20+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
21+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
22+
<java.version>1.8</java.version>
23+
</properties>
24+
25+
<dependencies>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter</artifactId>
29+
</dependency>
30+
31+
<dependency>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-starter-test</artifactId>
34+
<scope>test</scope>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>com.baomidou</groupId>
39+
<artifactId>mybatis-plus-boot-starter</artifactId>
40+
<version>3.1.0</version>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>mysql</groupId>
45+
<artifactId>mysql-connector-java</artifactId>
46+
</dependency>
47+
48+
<dependency>
49+
<groupId>io.shardingsphere</groupId>
50+
<artifactId>sharding-jdbc-core</artifactId>
51+
<version>3.1.0</version>
52+
</dependency>
53+
54+
<dependency>
55+
<groupId>cn.hutool</groupId>
56+
<artifactId>hutool-all</artifactId>
57+
</dependency>
58+
59+
<dependency>
60+
<groupId>org.projectlombok</groupId>
61+
<artifactId>lombok</artifactId>
62+
<optional>true</optional>
63+
</dependency>
64+
</dependencies>
65+
66+
<build>
67+
<finalName>spring-boot-demo-sharding-jdbc</finalName>
68+
<plugins>
69+
<plugin>
70+
<groupId>org.springframework.boot</groupId>
71+
<artifactId>spring-boot-maven-plugin</artifactId>
72+
</plugin>
73+
</plugins>
74+
</build>
4775

4876
</project>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
USE `spring-boot-demo`;
2+
DROP TABLE IF EXISTS `t_order_0`;
3+
CREATE TABLE `t_order_0`
4+
(
5+
`id` BIGINT NOT NULL COMMENT '主键',
6+
`user_id` BIGINT NOT NULL COMMENT '用户id',
7+
`order_id` BIGINT NOT NULL COMMENT '订单id',
8+
`remark` VARCHAR(200) DEFAULT '' COMMENT '备注',
9+
primary key (`id`)
10+
) ENGINE = InnoDB
11+
DEFAULT CHARSET = utf8 COMMENT ='Spring Boot Demo 分库分表 系列示例表0';
12+
13+
DROP TABLE IF EXISTS `t_order_1`;
14+
CREATE TABLE `t_order_1`
15+
(
16+
`id` BIGINT NOT NULL COMMENT '主键',
17+
`user_id` BIGINT NOT NULL COMMENT '用户id',
18+
`order_id` BIGINT NOT NULL COMMENT '订单id',
19+
`remark` VARCHAR(200) DEFAULT '' COMMENT '备注',
20+
primary key (`id`)
21+
) ENGINE = InnoDB
22+
DEFAULT CHARSET = utf8 COMMENT ='Spring Boot Demo 分库分表 系列示例表1';
23+
24+
DROP TABLE IF EXISTS `t_order_2`;
25+
CREATE TABLE `t_order_2`
26+
(
27+
`id` BIGINT NOT NULL COMMENT '主键',
28+
`user_id` BIGINT NOT NULL COMMENT '用户id',
29+
`order_id` BIGINT NOT NULL COMMENT '订单id',
30+
`remark` VARCHAR(200) DEFAULT '' COMMENT '备注',
31+
primary key (`id`)
32+
) ENGINE = InnoDB
33+
DEFAULT CHARSET = utf8 COMMENT ='Spring Boot Demo 分库分表 系列示例表2';
34+
35+
USE `spring-boot-demo-2`;
36+
37+
DROP TABLE IF EXISTS `t_order_0`;
38+
CREATE TABLE `t_order_0`
39+
(
40+
`id` BIGINT NOT NULL COMMENT '主键',
41+
`user_id` BIGINT NOT NULL COMMENT '用户id',
42+
`order_id` BIGINT NOT NULL COMMENT '订单id',
43+
`remark` VARCHAR(200) DEFAULT '' COMMENT '备注',
44+
primary key (`id`)
45+
) ENGINE = InnoDB
46+
DEFAULT CHARSET = utf8 COMMENT ='Spring Boot Demo 分库分表 系列示例表0';
47+
48+
DROP TABLE IF EXISTS `t_order_1`;
49+
CREATE TABLE `t_order_1`
50+
(
51+
`id` BIGINT NOT NULL COMMENT '主键',
52+
`user_id` BIGINT NOT NULL COMMENT '用户id',
53+
`order_id` BIGINT NOT NULL COMMENT '订单id',
54+
`remark` VARCHAR(200) DEFAULT '' COMMENT '备注',
55+
primary key (`id`)
56+
) ENGINE = InnoDB
57+
DEFAULT CHARSET = utf8 COMMENT ='Spring Boot Demo 分库分表 系列示例表1';
58+
59+
DROP TABLE IF EXISTS `t_order_2`;
60+
CREATE TABLE `t_order_2`
61+
(
62+
`id` BIGINT NOT NULL COMMENT '主键',
63+
`user_id` BIGINT NOT NULL COMMENT '用户id',
64+
`order_id` BIGINT NOT NULL COMMENT '订单id',
65+
`remark` VARCHAR(200) DEFAULT '' COMMENT '备注',
66+
primary key (`id`)
67+
) ENGINE = InnoDB
68+
DEFAULT CHARSET = utf8 COMMENT ='Spring Boot Demo 分库分表 系列示例表2';

spring-boot-demo-sharding-jdbc/src/main/java/com/xkcoding/sharding/jdbc/SpringBootDemoShardingJdbcApplication.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.xkcoding.sharding.jdbc;
22

3+
import org.mybatis.spring.annotation.MapperScan;
34
import org.springframework.boot.SpringApplication;
45
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
7+
import org.springframework.transaction.annotation.EnableTransactionManagement;
58

69
/**
710
* <p>
@@ -17,6 +20,8 @@
1720
* @modified: yangkai.shen
1821
*/
1922
@SpringBootApplication
23+
@EnableTransactionManagement(proxyTargetClass = true)
24+
@MapperScan("com.xkcoding.sharding.jdbc.mapper")
2025
public class SpringBootDemoShardingJdbcApplication {
2126

2227
public static void main(String[] args) {
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.xkcoding.sharding.jdbc.config;
2+
3+
import com.zaxxer.hikari.HikariDataSource;
4+
import io.shardingsphere.api.config.rule.ShardingRuleConfiguration;
5+
import io.shardingsphere.api.config.rule.TableRuleConfiguration;
6+
import io.shardingsphere.api.config.strategy.InlineShardingStrategyConfiguration;
7+
import io.shardingsphere.api.config.strategy.NoneShardingStrategyConfiguration;
8+
import io.shardingsphere.core.keygen.DefaultKeyGenerator;
9+
import io.shardingsphere.shardingjdbc.api.ShardingDataSourceFactory;
10+
import org.springframework.beans.factory.annotation.Qualifier;
11+
import org.springframework.context.annotation.Bean;
12+
import org.springframework.context.annotation.Configuration;
13+
import org.springframework.context.annotation.Primary;
14+
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
15+
16+
import javax.sql.DataSource;
17+
import java.sql.SQLException;
18+
import java.util.HashMap;
19+
import java.util.Map;
20+
import java.util.Properties;
21+
import java.util.concurrent.ConcurrentHashMap;
22+
23+
/**
24+
* datetime 2018/11/28 10:46
25+
*
26+
* @author sin5
27+
*/
28+
@Configuration
29+
public class DataSourceShardingConfig {
30+
/**
31+
* 需要手动配置事务管理器
32+
*/
33+
@Bean
34+
public DataSourceTransactionManager transactionManager(@Qualifier("dataSource") DataSource dataSource) {
35+
return new DataSourceTransactionManager(dataSource);
36+
}
37+
38+
@Bean(name = "dataSource")
39+
@Primary
40+
public DataSource dataSource() throws SQLException {
41+
ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
42+
// 设置分库策略
43+
shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new InlineShardingStrategyConfiguration("user_id", "ds${user_id % 2}"));
44+
// 设置规则适配的表
45+
shardingRuleConfig.getBindingTableGroups().add("t_order");
46+
// 设置分表策略
47+
shardingRuleConfig.getTableRuleConfigs().add(orderTableRule());
48+
shardingRuleConfig.setDefaultDataSourceName("ds0");
49+
shardingRuleConfig.setDefaultTableShardingStrategyConfig(new NoneShardingStrategyConfiguration());
50+
51+
Properties properties = new Properties();
52+
properties.setProperty("sql.show", "true");
53+
54+
return ShardingDataSourceFactory.createDataSource(dataSourceMap(), shardingRuleConfig, new ConcurrentHashMap<>(16), properties);
55+
}
56+
57+
private TableRuleConfiguration orderTableRule() {
58+
TableRuleConfiguration tableRule = new TableRuleConfiguration();
59+
// 设置逻辑表名
60+
tableRule.setLogicTable("t_order");
61+
// ds${0..1}.t_order_${0..2} 也可以写成 ds$->{0..1}.t_order_$->{0..1}
62+
tableRule.setActualDataNodes("ds${0..1}.t_order_${0..2}");
63+
tableRule.setTableShardingStrategyConfig(new InlineShardingStrategyConfiguration("order_id", "t_order_$->{order_id % 3}"));
64+
tableRule.setKeyGenerator(new DefaultKeyGenerator());
65+
tableRule.setKeyGeneratorColumnName("order_id");
66+
return tableRule;
67+
}
68+
69+
private Map<String, DataSource> dataSourceMap() {
70+
Map<String, DataSource> dataSourceMap = new HashMap<>(16);
71+
72+
// 配置第一个数据源
73+
HikariDataSource ds0 = new HikariDataSource();
74+
ds0.setDriverClassName("com.mysql.cj.jdbc.Driver");
75+
ds0.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/spring-boot-demo?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=GMT%2B8");
76+
ds0.setUsername("root");
77+
ds0.setPassword("root");
78+
79+
// 配置第二个数据源
80+
HikariDataSource ds1 = new HikariDataSource();
81+
ds1.setDriverClassName("com.mysql.cj.jdbc.Driver");
82+
ds1.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/spring-boot-demo-2?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&failOverReadOnly=false&serverTimezone=GMT%2B8");
83+
ds1.setUsername("root");
84+
ds1.setPassword("root");
85+
86+
dataSourceMap.put("ds0", ds0);
87+
dataSourceMap.put("ds1", ds1);
88+
return dataSourceMap;
89+
}
90+
91+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.xkcoding.sharding.jdbc.mapper;
2+
3+
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
4+
import com.xkcoding.sharding.jdbc.model.Order;
5+
import org.springframework.stereotype.Component;
6+
7+
/**
8+
* <p>
9+
* 订单表 Mapper
10+
* </p>
11+
*
12+
* @package: com.xkcoding.sharding.jdbc.mapper
13+
* @description: 订单表 Mapper
14+
* @author: yangkai.shen
15+
* @date: Created in 2019-03-26 13:38
16+
* @copyright: Copyright (c) 2019
17+
* @version: V1.0
18+
* @modified: yangkai.shen
19+
*/
20+
@Component
21+
public interface OrderMapper extends BaseMapper<Order> {
22+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.xkcoding.sharding.jdbc.model;
2+
3+
import com.baomidou.mybatisplus.annotation.TableName;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Builder;
6+
import lombok.Data;
7+
import lombok.NoArgsConstructor;
8+
9+
/**
10+
* <p>
11+
* 订单表
12+
* </p>
13+
*
14+
* @package: com.xkcoding.sharding.jdbc.model
15+
* @description: 订单表
16+
* @author: yangkai.shen
17+
* @date: Created in 2019-03-26 13:35
18+
* @copyright: Copyright (c) 2019
19+
* @version: V1.0
20+
* @modified: yangkai.shen
21+
*/
22+
@Data
23+
@NoArgsConstructor
24+
@AllArgsConstructor
25+
@Builder
26+
@TableName(value = "t_order")
27+
public class Order {
28+
/**
29+
* 主键
30+
*/
31+
private Long id;
32+
/**
33+
* 用户id
34+
*/
35+
private Long userId;
36+
37+
/**
38+
* 订单id
39+
*/
40+
private Long orderId;
41+
/**
42+
* 备注
43+
*/
44+
private String remark;
45+
}

spring-boot-demo-sharding-jdbc/src/main/resources/application.properties

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
mybatis-plus:
2+
global-config:
3+
banner: false

0 commit comments

Comments
 (0)