ShardingSphere-JDBC的分库

本文最后更新于:February 5, 2023 pm

积土成山,风雨兴焉;积水成渊,蛟龙生焉;积善成德,而神明自得,圣心备焉。故不积跬步,无以至千里,不积小流无以成江海。齐骥一跃,不能十步,驽马十驾,功不在舍。面对悬崖峭壁,一百年也看不出一条裂缝来,但用斧凿,能进一寸进一寸,能进一尺进一尺,不断积累,飞跃必来,突破随之。

目录

分库

依赖:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
<version>5.1.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>

yaml配置:

这里的type根据个人情况可以选择Druid,但我自己测试了多种使用Druid的方式均不能启动成功,所以,这里就使用默认的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

spring:
application:
name: start
shardingsphere:
datasource:
names: db0,db1
db0:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
url: jdbc:mysql://127.0.0.1:3306/jwgl?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8
db1:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
url: jdbc:mysql://127.0.0.2:3306/jwglaly?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8

mybatis-plus:
mapper-locations: classpath:mapper/*.xml
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
server:
port: 8080

📢:url的具体内容根据自己的情况进行修改!两个不同数据库即可。

启动项目成功后,可以看见以下内容:

1
2
3
4
5
2023-02-05 17:07:21.105  INFO 2455 --- [  restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1547 ms
2023-02-05 17:07:26.402 INFO 2455 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2023-02-05 17:07:26.513 INFO 2455 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2023-02-05 17:07:26.521 INFO 2455 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-2 - Starting...
2023-02-05 17:07:26.846 INFO 2455 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-2 - Start completed.

实体类

根据数据库字段增加实体类。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.tothefor.start.pojo;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

/**
* @Author DragonOne
* @Date 2023/2/5 17:20
* @墨水记忆 www.tothefor.com
*/

@Data
@Builder
@AllArgsConstructor
public class XXB {
private Integer jsh;
private String jsxm;
private String bmh;
}

Mapper

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.tothefor.start.mapper;

import com.tothefor.start.pojo.Xxb;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

/**
* @Author DragonOne
* @Date 2023/2/5 17:22
* @墨水记忆 www.tothefor.com
*/

@Mapper
public interface XxbMapper {
@Select("select * from jsjbxxb where jsh=#{jsh}")
Xxb get(String jsh);

@Insert("insert into jsjbxxb(jsh,jsxm,bmh) values (#{jsh},#{jsxm},#{bmh})")
int add(Xxb xxb);
}

增加分库策略配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

spring:
application:
name: start
shardingsphere:
datasource:
# 有几个数据库源就配置几个,自定义。但推荐是名称+数字的形式
names: db0,db1
# 为每一个数据源进行配置
db0:
# 数据库源类型,默认HikariDataSource
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
url: jdbc:mysql://127.0.0.1:3306/jwgl?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8
db1:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
url: jdbc:mysql://127.0.0.2:3306/jwglaly?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8

rules:
sharding:
tables:
# 填写表的名称,程序中对该表的操作,都将会采用下面的路由策略
# jsjbxxb即为要操作的表
jsjbxxb:
# 填写实际的路由节点,即哪一个数据库的jsjbxxb
# 也可以使用表达式:db$->{0..1}.jsjbxxb 简易理解为:db{}.jsjbxxb 。即通过表达式指定哪一个库
actual-data-nodes: db0.jsjbxxb,db1.jsjbxxb
# 分配策略
database-strategy:
# 这里是标准策略,也可以是其他策略,见官网
standard:
# 参与策略的字段
sharding-colum: jsh
# 下面我们自定义的算法
sharding-algorithm-name: my-alg
sharding-algorithm:
# 自定义算法名称
my-alg:
# 算法类型,官方内置多种
type: MOD
props:
sharding-count: 2

props:
# 开启日志方便观察
sql-show: true

mybatis-plus:
mapper-locations: classpath:mapper/*.xml
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
server:
port: 8080