SpringBoot+MybatisPlus實(shí)現(xiàn)sharding-jdbc分庫(kù)分表的示例代碼
介紹
學(xué)習(xí)sharding-jdbc的分庫(kù)分表功能,以分庫(kù),分表,分庫(kù)分表三種方式來(lái)實(shí)現(xiàn),此文章只展示分庫(kù)分表,為簡(jiǎn)潔其余兩種分篇寫(xiě)
1.環(huán)境搭建
代碼結(jié)構(gòu)

數(shù)據(jù)庫(kù)搭建(2個(gè)mysql數(shù)據(jù)庫(kù)),并在2個(gè)庫(kù)下都添加多個(gè)相同結(jié)構(gòu)的表



pom.xml依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.20</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.0.0-RC1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
</dependencies>
application.yml (配置文件分離,結(jié)構(gòu)更清晰)
spring:
main:
allow-bean-definition-overriding: true # 需要配置否則加載數(shù)據(jù)源報(bào)錯(cuò) 是否允許定義重名的bean對(duì)象覆蓋原有的bean
profiles:
active: databasetable
mybatis-plus:
mapperLocations: classpath*:mapper/*Mapper.xml
application-databasetable.yml
# 數(shù)據(jù)源命名
spring.shardingsphere.datasource.names=book2021,book2022
# datasource1
spring.shardingsphere.datasource.book2021.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.book2021.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.book2021.url=jdbc:mysql://192.168.31.212:3306/book2021?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8&allowMultiQueries=true
spring.shardingsphere.datasource.book2021.username=root
spring.shardingsphere.datasource.book2021.password=123
# datasource2
spring.shardingsphere.datasource.book2022.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.book2022.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.book2022.url=jdbc:mysql://192.168.31.212:3307/book2022?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8&allowMultiQueries=true
spring.shardingsphere.datasource.book2022.username=root
spring.shardingsphere.datasource.book2022.password=123
#庫(kù)名.表名
spring.shardingsphere.sharding.tables.book.actual-data-nodes=book202${1..2}.book_info_202${1..2}0${1..9},book202${1..2}.book_info_202${1..2}1${0..2}
# 數(shù)據(jù)庫(kù)分片字段
spring.shardingsphere.sharding.tables.book.database-strategy.standard.sharding-column=create_time
# 數(shù)據(jù)庫(kù)分片策略
spring.shardingsphere.sharding.tables.book.database-strategy.standard.preciseAlgorithmClassName=cn.xwl.xshardingjdbc.config.database.PreciseDatabaseShardingAlgorithm
spring.shardingsphere.sharding.tables.book.database-strategy.standard.rangeAlgorithmClassName=cn.xwl.xshardingjdbc.config.database.RangeDatabaseShardingAlgorithm
# 數(shù)據(jù)表分片字段
spring.shardingsphere.sharding.tables.book.table-strategy.standard.sharding-column=create_time
# 數(shù)據(jù)表分表策略
spring.shardingsphere.sharding.tables.book.table-strategy.standard.preciseAlgorithmClassName=cn.xwl.xshardingjdbc.config.table.PreciseTableShardingAlgorithm
spring.shardingsphere.sharding.tables.book.table-strategy.standard.rangeAlgorithmClassName=cn.xwl.xshardingjdbc.config.table.RangeTableShardingAlgorithm
# 打印sql
spring.shardingsphere.props.sql.show=true
2.代碼編寫(xiě)
同分庫(kù),分表兩篇文中的一致,這里不粘貼了,項(xiàng)目地址 本測(cè)試Demo的Gitee地址
到此這篇關(guān)于SpringBoot+MybatisPlus實(shí)現(xiàn)sharding-jdbc分庫(kù)分表的示例代碼的文章就介紹到這了,更多相關(guān)SpringBoot MybatisPlus sharding-jdbc分庫(kù)分表內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- MybatisPlus攔截器如何實(shí)現(xiàn)數(shù)據(jù)表分表
- SpringBoot+Mybatis-plus+shardingsphere實(shí)現(xiàn)分庫(kù)分表的方案
- Mybatis-Plus集成Sharding-JDBC與Flyway實(shí)現(xiàn)多租戶分庫(kù)分表實(shí)戰(zhàn)
- SQL數(shù)據(jù)分表Mybatis?Plus動(dòng)態(tài)表名優(yōu)方案
- SpringBoot+MybatisPlus+Mysql+Sharding-JDBC分庫(kù)分表
- springboot+mybatis-plus基于攔截器實(shí)現(xiàn)分表的示例代碼
- Mybatis-plus使用TableNameHandler分表詳解(附完整示例源碼)
- Spring Boot 集成 Sharding-JDBC + Mybatis-Plus 實(shí)現(xiàn)分庫(kù)分表功能
- MyBatis-Plus使用動(dòng)態(tài)表名分表查詢的實(shí)現(xiàn)
相關(guān)文章
詳解如何在Spring?Security中自定義權(quán)限表達(dá)式
這篇文章主要和大家詳細(xì)介紹一下如何在Spring?Security中自定義權(quán)限表達(dá)式,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-07-07
Eclipse+Maven構(gòu)建Hadoop項(xiàng)目的方法步驟
這篇文章主要介紹了Eclipse+Maven構(gòu)建Hadoop項(xiàng)目的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02
Java 日期時(shí)間工具包–java.time的使用
這篇文章主要介紹了Java 日期時(shí)間工具包–java.time的使用,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下2021-04-04
Javaweb實(shí)現(xiàn)在線人數(shù)統(tǒng)計(jì)代碼實(shí)例
這篇文章主要介紹了Javaweb實(shí)現(xiàn)在線人數(shù)統(tǒng)計(jì)代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
MybatisGenerator文件生成不出對(duì)應(yīng)文件的問(wèn)題
本文介紹了使用MybatisGenerator生成文件時(shí)遇到的問(wèn)題及解決方法,主要步驟包括檢查目標(biāo)表是否存在、是否能連接到數(shù)據(jù)庫(kù)、配置生成器的路徑等,通過(guò)在項(xiàng)目結(jié)構(gòu)中引入相應(yīng)的jar包,并在GeneratorSqlmap.java文件中運(yùn)行,可以成功生成對(duì)應(yīng)的文件2025-01-01
Feign Client 超時(shí)時(shí)間配置不生效的解決
這篇文章主要介紹了Feign Client 超時(shí)時(shí)間配置不生效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
Spring解決循環(huán)依賴的方法(三級(jí)緩存)
今天,我們要說(shuō)的是spring是如何解決循環(huán)依賴的。對(duì)于一個(gè)問(wèn)題說(shuō)解決之前,我們首先要先明確形成問(wèn)題的本因。那么循環(huán)依賴,何為循環(huán)依賴呢?感興趣的朋友跟隨小編一起看看吧2021-11-11
Java多線程實(shí)現(xiàn)Callable接口
本文給大家分享的是使用Java多線程來(lái)實(shí)現(xiàn)callable接口的方法,以及使用方法,另外還有一個(gè)網(wǎng)友的實(shí)例,希望能夠?qū)Υ蠹艺莆認(rèn)ava多線程有所幫助。2016-06-06

