SpringBoot+MybatisPlus實(shí)現(xiàn)sharding-jdbc分庫分表的示例代碼
介紹
學(xué)習(xí)sharding-jdbc的分庫分表功能,以分庫,分表,分庫分表三種方式來實(shí)現(xiàn),此文章只展示分庫分表,為簡潔其余兩種分篇寫
1.環(huán)境搭建
代碼結(jié)構(gòu)
數(shù)據(jù)庫搭建(2個mysql數(shù)據(jù)庫),并在2個庫下都添加多個相同結(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ù)源報錯 是否允許定義重名的bean對象覆蓋原有的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 #庫名.表名 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ù)庫分片字段 spring.shardingsphere.sharding.tables.book.database-strategy.standard.sharding-column=create_time # 數(shù)據(jù)庫分片策略 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àng)目地址 本測試Demo的Gitee地址
到此這篇關(guān)于SpringBoot+MybatisPlus實(shí)現(xiàn)sharding-jdbc分庫分表的示例代碼的文章就介紹到這了,更多相關(guān)SpringBoot MybatisPlus sharding-jdbc分庫分表內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot集成Sharding-JDBC實(shí)現(xiàn)分庫分表方式
- SpringBoot+MybatisPlus+Mysql+Sharding-JDBC分庫分表
- SpringBoot整合sharding-jdbc實(shí)現(xiàn)自定義分庫分表的實(shí)踐
- SpringBoot整合sharding-jdbc實(shí)現(xiàn)分庫分表與讀寫分離的示例
- SpringBoot 2.0 整合sharding-jdbc中間件實(shí)現(xiàn)數(shù)據(jù)分庫分表
- SpringBoot整合sharding-jdbc?實(shí)現(xiàn)分庫分表操作的示例代碼
相關(guān)文章
Java NIO實(shí)現(xiàn)群聊系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java NIO實(shí)現(xiàn)群聊系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11Java線程池運(yùn)行狀態(tài)監(jiān)控實(shí)現(xiàn)解析
這篇文章主要介紹了Java線程池運(yùn)行狀態(tài)監(jiān)控實(shí)現(xiàn)解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-10-10Spring Cloud EureKa Ribbon 服務(wù)注冊發(fā)現(xiàn)與調(diào)用
這篇文章主要介紹了Spring Cloud EureKa Ribbon 服務(wù)注冊發(fā)現(xiàn)與調(diào)用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02一文探索Apache HttpClient如何設(shè)定超時時間
Apache HttpClient是一個流行的Java庫,用于發(fā)送HTTP請求,這篇文章主要為大家介紹了Apache HttpClient如何設(shè)定超時時間,感興趣的小伙伴可以學(xué)習(xí)一下2023-10-10Java 詳解循環(huán)屏障CyclicBarrier如何實(shí)現(xiàn)多線程分段等待執(zhí)行完成
CyclicBarrier是一個同步工具類,可以翻譯成循環(huán)屏障,也叫障礙器或同步屏障。CyclicBarrier內(nèi)部有一個計數(shù)器count,調(diào)用障礙器的await方法會使計數(shù)器count的值減一,當(dāng)計數(shù)器count的值為0時,表明調(diào)用了await方法線程已經(jīng)達(dá)到了設(shè)置的數(shù)量2021-11-11基于Java的電梯系統(tǒng)實(shí)現(xiàn)過程
這篇文章主要介紹了基于Java的電梯系統(tǒng)實(shí)現(xiàn)過程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-10-10