集成 sharding-jdbc 實現(xiàn)分庫分表
最近學(xué)到了一個分庫分表實現(xiàn)本 demo 主要演示了如何集成?sharding-jdbc
?實現(xiàn)分庫分表操作,ORM 層使用了Mybatis-Plus
簡化開發(fā),童鞋們可以按照自己的喜好替換為 JPA、通用Mapper、JdbcTemplate甚至原生的JDBC都可以。
1、運行方式
在數(shù)據(jù)庫創(chuàng)建2個數(shù)據(jù)庫,分別為:
spring-boot-demo
、spring-boot-demo-2
去數(shù)據(jù)庫執(zhí)行?
sql/schema.sql
?,創(chuàng)建?6
?張分片表找到?
DataSourceShardingConfig
?配置類,修改?數(shù)據(jù)源
?的相關(guān)配置,位于?dataSourceMap()
?這個方法找到測試類?
SpringBootDemoShardingJdbcApplicationTests
?進(jìn)行測試
2、關(guān)鍵代碼
2.1.?pom.xml
2.2.?CustomSnowflakeKeyGenerator.java
2.3.?DataSourceShardingConfig.java
2.3.?SpringBootDemoShardingJdbcApplicationTests.java
參考
ShardingSphere
?官網(wǎng):https://shardingsphere.apache.org/index_zh.html (雖然文檔確實垃圾,但是還是得參考啊~)Mybatis-Plus
?語法參考官網(wǎng):https://mybatis.plus/? ? ? ?

? ? ? ? ? ? ? ? ? ?掃碼拉群,學(xué)習(xí)打卡,交流經(jīng)驗
標(biāo)簽: