使用sharding-jdbc實(shí)現(xiàn)水平分庫(kù)+水平分表的示例代碼
前面的文章使用sharding-jdbc實(shí)現(xiàn)水平分表中詳細(xì)記錄了如何使用sharding-jdbc實(shí)現(xiàn)水平分表,即根據(jù)相應(yīng)的策略,將一部分?jǐn)?shù)據(jù)存入到表1中,一部分?jǐn)?shù)據(jù)存入到表2中,邏輯上為同一張表,分表操作全部交由sharding-jdbc進(jìn)行處理。
可能根據(jù)需要,還需要將一張表的數(shù)據(jù)拆分存入到多個(gè)數(shù)據(jù)庫(kù)中,甚至多個(gè)數(shù)據(jù)庫(kù)的多個(gè)表中,使用sharding-jdbc同樣可以實(shí)現(xiàn)。
重復(fù)的篇幅則不再贅述,下面重點(diǎn)記錄升級(jí)的過程。
分庫(kù)分表策略:將id為偶數(shù)的存入到庫(kù)1中,奇數(shù)存入到庫(kù)2中,在每個(gè)庫(kù)中,再根據(jù)學(xué)生的性別分別存到到表1和表2中。
新建兩個(gè)數(shù)據(jù)庫(kù)sharding_db1和sharding_db2,在兩個(gè)數(shù)據(jù)庫(kù)中在分別創(chuàng)建結(jié)構(gòu)相同的兩張表,student_1和student_2。
CREATE TABLE `NewTable` ( `ID` bigint(20) NOT NULL , `NAME` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL , `AGE` int(11) NOT NULL , `GENDER` int(1) NOT NULL , PRIMARY KEY (`ID`) );
相比前面文章中,將gender性別字段設(shè)置成了int類型,方便根據(jù)性別再進(jìn)行分表。
修改配置文件
spring.main.allow-bean-definition-overriding=true # 配置Sharding-JDBC的分片策略 # 配置數(shù)據(jù)源,給數(shù)據(jù)源起名g1,g2...此處可配置多數(shù)據(jù)源 spring.shardingsphere.datasource.names=g1,g2 # 配置數(shù)據(jù)源具體內(nèi)容:連接池,驅(qū)動(dòng),地址,用戶名,密碼 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_db1?characterEncoding=utf-8&useUnicode=true&useSSL=false&serverTimezone=UTC spring.shardingsphere.datasource.g1.username=root spring.shardingsphere.datasource.g1.password=123456 spring.shardingsphere.datasource.g2.type=com.alibaba.druid.pool.DruidDataSource spring.shardingsphere.datasource.g2.driver-class-name=com.mysql.cj.jdbc.Driver spring.shardingsphere.datasource.g2.url=jdbc:mysql://localhost:3306/sharding_db2?characterEncoding=utf-8&useUnicode=true&useSSL=false&serverTimezone=UTC spring.shardingsphere.datasource.g2.username=root spring.shardingsphere.datasource.g2.password=123456 # 配置數(shù)據(jù)庫(kù)的分布,表的分布 spring.shardingsphere.sharding.tables.student.actual-data-nodes=g$->{1..2}.student_$->{1..2} # 指定student表 主鍵gid 生成策略為 SNOWFLAKE spring.shardingsphere.sharding.tables.student.key-generator.column=id spring.shardingsphere.sharding.tables.student.key-generator.type=SNOWFLAKE # 指定數(shù)據(jù)庫(kù)分片策略 約定id值是偶數(shù)添加到sharding_db1中,奇數(shù)添加到sharding_db2中 spring.shardingsphere.sharding.tables.student.database-strategy.inline.sharding-column=id spring.shardingsphere.sharding.tables.student.database-strategy.inline.algorithm-expression=g$->{id % 2 + 1} # 指定表分片策略 約定gender值是0添加到student_1表,如果gender是1添加到student_2表 spring.shardingsphere.sharding.tables.student.table-strategy.inline.sharding-column=gender spring.shardingsphere.sharding.tables.student.table-strategy.inline.algorithm-expression=student_$->{gender % 2 + 1} # 打開sql輸出日志 spring.shardingsphere.props.sql.show=true
配置多個(gè)數(shù)據(jù)源時(shí),使用逗號(hào)隔開,分別配置其屬性。除了配置表分片策略,還需配置庫(kù)分配策略。
測(cè)試類
@SpringBootTest class ShardingJdbcDemoApplicationTests { @Autowired private StudentMapper studentMapper; @Test public void test01() { for (int i = 0; i < 15; i++) { Student student = new Student(); student.setName("wuwl"); student.setAge(27); student.setGender(i%2); studentMapper.insert(student); } } }
運(yùn)行效果:
看樣子是成功了,查看數(shù)據(jù)庫(kù)數(shù)據(jù)。
sharding_db1.student_1:
sharding_db1.student_2:
sharding_db2.student_1:
sharding_db2.student_2:
到此這篇關(guān)于使用sharding-jdbc實(shí)現(xiàn)水平分庫(kù)+水平分表的示例代碼的文章就介紹到這了,更多相關(guān)sharding-jdbc水平分庫(kù)水平分表內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot實(shí)現(xiàn)分庫(kù)分表
- Mysql分庫(kù)分表之后主鍵處理的幾種方法
- Mysql數(shù)據(jù)庫(kù)分庫(kù)分表全面瓦解
- 利用Sharding-Jdbc進(jìn)行分庫(kù)分表的操作代碼
- SpringBoot整合sharding-jdbc實(shí)現(xiàn)自定義分庫(kù)分表的實(shí)踐
- SpringBoot整合sharding-jdbc實(shí)現(xiàn)分庫(kù)分表與讀寫分離的示例
- Java中ShardingSphere分庫(kù)分表實(shí)戰(zhàn)
- Spring Boot 集成 Sharding-JDBC + Mybatis-Plus 實(shí)現(xiàn)分庫(kù)分表功能
- Springboot2.x+ShardingSphere實(shí)現(xiàn)分庫(kù)分表的示例代碼
- springboot整合shardingjdbc實(shí)現(xiàn)分庫(kù)分表最簡(jiǎn)單demo
- 使用ShardingSphere-Proxy實(shí)現(xiàn)分表分庫(kù)
相關(guān)文章
SpringBoot高級(jí)配置之臨時(shí)屬性、配置文件、日志、多環(huán)境配置詳解
這篇文章主要介紹了SpringBoot高級(jí)配置之臨時(shí)屬性、配置文件、日志、多環(huán)境配置,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-02-02Java的內(nèi)存區(qū)域與內(nèi)存溢出異常你了解嗎
這篇文章主要為大家詳細(xì)介紹了Java的內(nèi)存區(qū)域與內(nèi)存溢出異常,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-03-03Java編程Post數(shù)據(jù)請(qǐng)求和接收代碼詳解
這篇文章主要介紹了Java編程Post數(shù)據(jù)請(qǐng)求和接收代碼詳解,涉及enctype的三種編碼,post與get等相關(guān)內(nèi)容,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11Java中try-catch-finally執(zhí)行順序你知道嗎
本文主要介紹了try-catch-finally執(zhí)行順序你知道嗎,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06