sharding-jdbc與springboot結合使用-單庫分表簡單使用


sharding-jdbc官網:

https://shardingsphere.apache.org/document/current/cn/quick-start/sharding-jdbc-quick-start/

sharding-jdbc springboot配置:

https://shardingsphere.apache.org/document/current/cn/manual/sharding-jdbc/configuration/config-spring-boot/

1、添加依賴

 <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.10</version>
</dependency>
<dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
            <version>4.0.0-RC1</version>
</dependency>
View Code

2、配置文件方式

spring.shardingsphere.props.sql.show=true
spring.shardingsphere.datasource.names=master
spring.shardingsphere.datasource.master.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.master.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.shardingsphere.datasource.master.url=jdbc:oracle:thin:@XXXX:1521:XXX
spring.shardingsphere.datasource.master.username=XXX
spring.shardingsphere.datasource.master.password=XXX

spring.shardingsphere.sharding.tables.student.actual-data-nodes=master.student_${0..1}
spring.shardingsphere.sharding.tables.student.table-strategy.inline.sharding-column=age
spring.shardingsphere.sharding.tables.student.table-strategy.inline.algorithm-expression=student_${age % 2}

#spring.shardingsphere.sharding.tables.STUDENT.table-strategy.standard.sharding-column=AGE
#spring.shardingsphere.sharding.tables.STUDENT.table-strategy.standard.precise-algorithm-class-name=com.chamc.rics.basics.test.algorithm.MyPreciseShardingAlgorithm


#spring.sharding.jdbc.config.sharding.default-table-strategy.standard.sharding-column=USER_ID
##精確分片算法,用於=和IN,實現類
#sharding.jdbc.config.sharding.default-table-strategy.standard.precise-algorithm-class-name=com.solider76.oo.service.chat.config.MyShardingConfig
##范圍分片算法,用於BETWEEN,實現類
#sharding.jdbc.config.sharding.default-table-strategy.standard.range-algorithm-class-name=com.solider76.oo.service.chat.config.MyShardingConfig
View Code

3、自定義方式

  參照官網及大牛的文章:http://wuwenliang.net/categories/Sharding-JDBC/

4、異常解決

對於保存數據操作,報錯。原因是配置文件中:

spring.shardingsphere.sharding.tables.student.table-strategy.inline.algorithm-expression=student_${age % 2}

student寫成了大寫,出現 不能對null 取mod()操作。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM