官网: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-masterdemo-sharding-slave
每个数据库中创建三个表t_user_0t_user_1t_user_2,建表语句如下

  1. USE `demo-sharding-master`;
  2. DROP TABLE IF EXISTS `t_user_0`;
  3. CREATE TABLE `t_user_0`
  4. (
  5. `id` int NOT NULL AUTO_INCREMENT COMMENT '主键',
  6. `user_name` varchar(50) NULL COMMENT '用户',
  7. `sex` int(1) NULL COMMENT '性别 男1,女2',
  8. `address` varchar(255) NULL COMMENT '地址',
  9. PRIMARY KEY (`id`)
  10. ) ENGINE = InnoDB
  11. DEFAULT CHARSET = utf8 COMMENT ='master示例表0';
  12. DROP TABLE IF EXISTS `t_user_1`;
  13. CREATE TABLE `t_user_1`
  14. (
  15. `id` int NOT NULL AUTO_INCREMENT COMMENT '主键',
  16. `user_name` varchar(50) NULL COMMENT '用户',
  17. `sex` int(1) NULL COMMENT '性别 男1,女2',
  18. `address` varchar(255) NULL COMMENT '地址',
  19. PRIMARY KEY (`id`)
  20. ) ENGINE = InnoDB
  21. DEFAULT CHARSET = utf8 COMMENT ='master示例表1';
  22. DROP TABLE IF EXISTS `t_user_2`;
  23. CREATE TABLE `t_user_2`
  24. (
  25. `id` int NOT NULL AUTO_INCREMENT COMMENT '主键',
  26. `user_name` varchar(50) NULL COMMENT '用户',
  27. `sex` int(1) NULL COMMENT '性别 男1,女2',
  28. `address` varchar(255) NULL COMMENT '地址',
  29. PRIMARY KEY (`id`)
  30. ) ENGINE = InnoDB
  31. DEFAULT CHARSET = utf8 COMMENT ='master示例表2';
  32. USE `demo-sharding-slave`;
  33. DROP TABLE IF EXISTS `t_user_0`;
  34. CREATE TABLE `t_user_0`
  35. (
  36. `id` int NOT NULL AUTO_INCREMENT COMMENT '主键',
  37. `user_name` varchar(50) NULL COMMENT '用户',
  38. `sex` int(1) NULL COMMENT '性别 男1,女2',
  39. `address` varchar(255) NULL COMMENT '地址',
  40. PRIMARY KEY (`id`)
  41. ) ENGINE = InnoDB
  42. DEFAULT CHARSET = utf8 COMMENT ='slave示例表0';
  43. DROP TABLE IF EXISTS `t_user_1`;
  44. CREATE TABLE `t_user_1`
  45. (
  46. `id` int NOT NULL AUTO_INCREMENT COMMENT '主键',
  47. `user_name` varchar(50) NULL COMMENT '用户',
  48. `sex` int(1) NULL COMMENT '性别 男1,女2',
  49. `address` varchar(255) NULL COMMENT '地址',
  50. PRIMARY KEY (`id`)
  51. ) ENGINE = InnoDB
  52. DEFAULT CHARSET = utf8 COMMENT ='slave示例表1';
  53. DROP TABLE IF EXISTS `t_user_2`;
  54. CREATE TABLE `t_user_2`
  55. (
  56. `id` int NOT NULL AUTO_INCREMENT COMMENT '主键',
  57. `user_name` varchar(50) NULL COMMENT '用户',
  58. `sex` int(1) NULL COMMENT '性别 男1,女2',
  59. `address` varchar(255) NULL COMMENT '地址',
  60. PRIMARY KEY (`id`)
  61. ) ENGINE = InnoDB
  62. DEFAULT CHARSET = utf8 COMMENT ='slave示例表2';

image.png

2. 引入Maven依赖:

  1. <!-- Mybatis 依赖配置 -->
  2. <dependency>
  3. <groupId>org.mybatis.spring.boot</groupId>
  4. <artifactId>mybatis-spring-boot-starter</artifactId>
  5. </dependency>
  6. <!-- druid -->
  7. <dependency>
  8. <groupId>com.alibaba</groupId>
  9. <artifactId>druid-spring-boot-starter</artifactId>
  10. </dependency>
  11. <dependency>
  12. <groupId>mysql</groupId>
  13. <artifactId>mysql-connector-java</artifactId>
  14. </dependency>
  15. <!--sharding-jdbc依赖-->
  16. <dependency>
  17. <groupId>io.shardingjdbc</groupId>
  18. <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
  19. <version>2.0.0.M3</version>
  20. </dependency>