官网:https://shardingsphere.apache.org/document/current/cn/overview/
Git实例:https://github.com/apache/shardingsphere-example/blob/dev/sharding-jdbc-example/sharding-example/sharding-spring-boot-mybatis-example/src/main/resources/application-sharding-databases-tables.properties
概述
sharding-jdbc
定位为轻量级 Java 框架,在 Java 的 JDBC 层提供的额外服务。 它使用客户端直连数据库,以 jar 包形式提供服务,无需额外部署和依赖,可理解为一个增强版的JDBC驱动,完全兼容JDBC和各种ORM框架。内部改写了SQL的添加和查询规则。适用于任何基于Java的ORM框架,如:JPA, Hibernate, Mybatis, Spring JDBC Template或直接使用JDBC。- 目前已经进入Apache孵化器。以4.x版本为新的发布开始
快速开始
1. 创建数据库
创建两个数据库demo-sharding-master
和demo-sharding-slave
每个数据库中创建三个表t_user_0
,t_user_1
,t_user_2
,建表语句如下
USE `demo-sharding-master`;
DROP TABLE IF EXISTS `t_user_0`;
CREATE TABLE `t_user_0`
(
`id` int NOT NULL AUTO_INCREMENT COMMENT '主键',
`user_name` varchar(50) NULL COMMENT '用户',
`sex` int(1) NULL COMMENT '性别 男1,女2',
`address` varchar(255) NULL COMMENT '地址',
PRIMARY KEY (`id`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8 COMMENT ='master示例表0';
DROP TABLE IF EXISTS `t_user_1`;
CREATE TABLE `t_user_1`
(
`id` int NOT NULL AUTO_INCREMENT COMMENT '主键',
`user_name` varchar(50) NULL COMMENT '用户',
`sex` int(1) NULL COMMENT '性别 男1,女2',
`address` varchar(255) NULL COMMENT '地址',
PRIMARY KEY (`id`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8 COMMENT ='master示例表1';
DROP TABLE IF EXISTS `t_user_2`;
CREATE TABLE `t_user_2`
(
`id` int NOT NULL AUTO_INCREMENT COMMENT '主键',
`user_name` varchar(50) NULL COMMENT '用户',
`sex` int(1) NULL COMMENT '性别 男1,女2',
`address` varchar(255) NULL COMMENT '地址',
PRIMARY KEY (`id`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8 COMMENT ='master示例表2';
USE `demo-sharding-slave`;
DROP TABLE IF EXISTS `t_user_0`;
CREATE TABLE `t_user_0`
(
`id` int NOT NULL AUTO_INCREMENT COMMENT '主键',
`user_name` varchar(50) NULL COMMENT '用户',
`sex` int(1) NULL COMMENT '性别 男1,女2',
`address` varchar(255) NULL COMMENT '地址',
PRIMARY KEY (`id`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8 COMMENT ='slave示例表0';
DROP TABLE IF EXISTS `t_user_1`;
CREATE TABLE `t_user_1`
(
`id` int NOT NULL AUTO_INCREMENT COMMENT '主键',
`user_name` varchar(50) NULL COMMENT '用户',
`sex` int(1) NULL COMMENT '性别 男1,女2',
`address` varchar(255) NULL COMMENT '地址',
PRIMARY KEY (`id`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8 COMMENT ='slave示例表1';
DROP TABLE IF EXISTS `t_user_2`;
CREATE TABLE `t_user_2`
(
`id` int NOT NULL AUTO_INCREMENT COMMENT '主键',
`user_name` varchar(50) NULL COMMENT '用户',
`sex` int(1) NULL COMMENT '性别 男1,女2',
`address` varchar(255) NULL COMMENT '地址',
PRIMARY KEY (`id`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8 COMMENT ='slave示例表2';
2. 引入Maven依赖:
<!-- Mybatis 依赖配置 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<!-- druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--sharding-jdbc依赖-->
<dependency>
<groupId>io.shardingjdbc</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>2.0.0.M3</version>
</dependency>