使用sharding-jdbc實(shí)現(xiàn)水平分表的示例代碼
在mysql中新建數(shù)據(jù)庫(kù)sharding_db,新增兩張結(jié)構(gòu)一樣的表student_1和student_2。
CREATE TABLE `student_1` ( `ID` bigint(20) NOT NULL , `NAME` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL , `AGE` int(11) NOT NULL , `GENDER` varchar(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL , PRIMARY KEY (`ID`) );
此處未指定主鍵自增,因?yàn)閮蓮埍淼膇d不能重復(fù),所以只能從后端傳入id。
添加依賴(lài)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Druid連接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.20</version>
</dependency>
<!-- Mysql驅(qū)動(dòng)依賴(lài) -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- MybatisPlus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<!-- Sharding-JDBC -->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.0.0-RC1</version>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
編寫(xiě)配置文件
spring.main.allow-bean-definition-overriding=true
# 配置Sharding-JDBC的分片策略
# 配置數(shù)據(jù)源,給數(shù)據(jù)源起名g1,g2...此處可配置多數(shù)據(jù)源
spring.shardingsphere.datasource.names=g1
# 配置數(shù)據(jù)源具體內(nèi)容:連接池,驅(qū)動(dòng),地址,用戶(hù)名,密碼
# 由于上面配置數(shù)據(jù)源只有g(shù)1因此下面只配置g1.type,g1.driver-class-name,g1.url,g1.username,g1.password
spring.shardingsphere.datasource.g1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.g1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.g1.url=jdbc:mysql://localhost:3306/sharding_db?characterEncoding=utf-8&useUnicode=true&useSSL=false&serverTimezone=UTC
spring.shardingsphere.datasource.g1.username=root
spring.shardingsphere.datasource.g1.password=123456
# 配置表的分布,表的策略
spring.shardingsphere.sharding.tables.student.actual-data-nodes=g1.student_$->{1..2}
# 指定student表 主鍵gid 生成策略為 SNOWFLAKE
spring.shardingsphere.sharding.tables.student.key-generator.column=id
spring.shardingsphere.sharding.tables.student.key-generator.type=SNOWFLAKE
# 指定分片策略 約定id值是偶數(shù)添加到student_1表,如果id是奇數(shù)添加到student_2表
spring.shardingsphere.sharding.tables.student.table-strategy.inline.sharding-column=id
spring.shardingsphere.sharding.tables.student.table-strategy.inline.algorithm-expression=student_$->{id % 2 + 1}
# 打開(kāi)sql輸出日志
spring.shardingsphere.props.sql.show=true
或者是yml格式
spring:
main:
allow-bean-definition-overriding: true
shardingsphere:
datasource:
g1:
driver-class-name: com.mysql.cj.jdbc.Driver
password: 123456
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://localhost:3306/sharding_db?characterEncoding=utf-8&useUnicode=true&useSSL=false&serverTimezone=UTC
username: root
names: g1
props:
sql:
show: true
sharding:
tables:
student:
actual-data-nodes: g1.student_$->{1..2}
key-generator:
column: id
type: SNOWFLAKE
table-strategy:
inline:
algorithm-expression: student_$->{id % 2 + 1}
sharding-column: id
編寫(xiě)實(shí)體類(lèi)
@Data
public class Student {
private Long id;
private String name;
private int age;
private String gender;
}
編寫(xiě)mapper接口
@Repository
public interface StudentMapper extends BaseMapper<Student> {
}
編寫(xiě)測(cè)試類(lèi)
@SpringBootTest
class ShardingJdbcDemoApplicationTests {
@Autowired
private StudentMapper studentMapper;
@Test
public void test01() {
for (int i = 0; i < 10; i++) {
Student student = new Student();
student.setName("wuwl");
student.setAge(27);
student.setGender("男");
studentMapper.insert(student);
}
}
}
執(zhí)行測(cè)試

執(zhí)行成功,主鍵通過(guò)雪花算法在后端生成,傳入到數(shù)據(jù)庫(kù)中,根據(jù)奇偶性進(jìn)行分表。
student_1表數(shù)據(jù):

student_2表數(shù)據(jù):

兩張表的數(shù)據(jù)分別有5條,但這只是因?yàn)檠┗ㄋ惴ㄉ傻膇d奇數(shù)偶數(shù)各5個(gè),不是1:1的關(guān)系,需要注意。
主鍵生成后,根據(jù)策略插入到對(duì)應(yīng)的表中,從打印出來(lái)的sql可以證明這一點(diǎn)。
通過(guò)mapper接口的selectById方法進(jìn)行查詢(xún)時(shí),會(huì)先根據(jù)主鍵策略判斷在哪個(gè)庫(kù),再直接去那個(gè)庫(kù)根據(jù)主鍵查詢(xún)。而如果是通過(guò)其它條件查詢(xún),或者是多個(gè)id的selectById方法查詢(xún),又是如何的呢?
@Test
public void test03() {
List<Long> list = new ArrayList<>();
list.add(1362282042768609282l);
list.add(1362282040277192705l);
List<Student> studentList = studentMapper.selectBatchIds(list);
System.out.println(studentList);
}
取了兩張表的id進(jìn)行查詢(xún)。

執(zhí)行同樣的sql,在兩張表中都查詢(xún)一遍,再組合結(jié)果。
如果所有的id,都來(lái)自同一張表,那是否會(huì)去多個(gè)表中重復(fù)查詢(xún)呢?

只執(zhí)行了一遍。所以,在執(zhí)行查詢(xún)時(shí),sharding會(huì)先判斷是否可以確定需要的數(shù)據(jù)來(lái)自那張表,如果能,則直接去那一張表中查詢(xún)數(shù)據(jù)即可,而如果不能確定,則會(huì)多個(gè)表重復(fù)查詢(xún),以確定查詢(xún)結(jié)果的完整性。
到此這篇關(guān)于使用sharding-jdbc實(shí)現(xiàn)水平分表的示例代碼的文章就介紹到這了,更多相關(guān)sharding-jdbc 水平分表內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- sharding-jdbc5.0.0實(shí)現(xiàn)分表實(shí)踐
- 利用Sharding-Jdbc進(jìn)行分庫(kù)分表的操作代碼
- 使用sharding-jdbc實(shí)現(xiàn)水平分庫(kù)+水平分表的示例代碼
- springboot實(shí)現(xiàn)以代碼的方式配置sharding-jdbc水平分表
- SpringBoot整合sharding-jdbc實(shí)現(xiàn)自定義分庫(kù)分表的實(shí)踐
- SpringBoot整合sharding-jdbc實(shí)現(xiàn)分庫(kù)分表與讀寫(xiě)分離的示例
- Java使用Sharding-JDBC分庫(kù)分表進(jìn)行操作
- Sharding-Jdbc 自定義復(fù)合分片的實(shí)現(xiàn)(分庫(kù)分表)
- 利用Sharding-Jdbc組件實(shí)現(xiàn)分表
相關(guān)文章
SpringBoot啟動(dòng)過(guò)程的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot啟動(dòng)過(guò)程的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
在Java SE上使用Headless模式的超級(jí)指南
這篇文章主要介紹了在Java SE上使用Headless模式的超級(jí)指南,文中介紹了Headless模式實(shí)際使用的各種技巧,極力推薦!需要的朋友可以參考下2015-07-07
java類(lèi)實(shí)現(xiàn)日期的時(shí)間差的實(shí)例講解
在本篇文章里小編給大家整理的是一篇關(guān)于java類(lèi)實(shí)現(xiàn)日期的時(shí)間差的實(shí)例講解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-01-01
mybatis批量插入時(shí),有字段可能為null會(huì)報(bào)錯(cuò)問(wèn)題
這篇文章主要介紹了mybatis批量插入時(shí),有字段可能為null會(huì)報(bào)錯(cuò)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
解決BeanUtils.copyProperties無(wú)法成功封裝的問(wèn)題
這篇文章主要介紹了解決BeanUtils.copyProperties無(wú)法成功封裝的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06

