集成 sharding-jdbc 实现分库分表
最近学到了一个分库分表实现本 demo 主要演示了如何集成 sharding-jdbc
实现分库分表操作,ORM 层使用了Mybatis-Plus
简化开发,童鞋们可以按照自己的喜好替换为 JPA、通用Mapper、JdbcTemplate甚至原生的JDBC都可以。
1、运行方式
在数据库创建2个数据库,分别为:
spring-boot-demo
、spring-boot-demo-2
去数据库执行
sql/schema.sql
,创建6
张分片表找到
DataSourceShardingConfig
配置类,修改数据源
的相关配置,位于dataSourceMap()
这个方法找到测试类
SpringBootDemoShardingJdbcApplicationTests
进行测试
2、关键代码
2.1. pom.xml
2.2. CustomSnowflakeKeyGenerator.java
2.3. DataSourceShardingConfig.java
2.3. SpringBootDemoShardingJdbcApplicationTests.java
参考
ShardingSphere
官网:https://shardingsphere.apache.org/index_zh.html (虽然文档确实垃圾,但是还是得参考啊~)Mybatis-Plus
语法参考官网:https://mybatis.plus/

扫码拉群,学习打卡,交流经验