Apache ShardingSphere 是一款开源分布式数据库生态项目,由 JDBC、Proxy 和 Sidecar(规划中) 3 款产品组成。ShardingSphere 已于2020年4月16日成为 Apache 软件基金会的顶级项目。
关于Apache ShardingSphere 的更多内容请查看Gitee上的地址:https://gitee.com/Sharding-Sphere/
本编是ShardingSphere分库分表的一个示例,环境为:SpringBoot 2.7.4 ShardingSphere 5.2.0 Mybatis Plus。
具体的项目结构如下:
一、先准备两个数据库shard01、shard02并创建两个一样的order物理表
CREATE TABLE IF NOT EXISTS `t_order_1`
(
`order_id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(30) NULL DEFAULT NULL,
`user_id` BIGINT(20) NULL DEFAULT NULL,
PRIMARY KEY (`order_id`)
);
CREATE TABLE IF NOT EXISTS `t_order_2`
(
`order_id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(30) NULL DEFAULT NULL,
`user_id` BIGINT(20) NULL DEFAULT NULL,
PRIMARY KEY (`order_id`)
);
二、POM结构
加入相关的引用坐标。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.4</version>
<relativePath/>
</parent>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<HikariCP.version>5.0.1</HikariCP.version>
<mybatis.version>3.5.11</mybatis.version>
<snakeyaml.version>1.32</snakeyaml.version>
<shardingsphere.version>5.2.0</shardingsphere.version>
</properties>
<dependencies>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifac