Spring Boot 2.1.6 使用sharding-sphere + Mybatis-Plus 实现分库分表(一)
1.Spring Boot 2.1.6 使用sharding-sphere + Mybatis-Plus 实现分库分表(一)
2.基于Spring Boot 2.2.0 使用sharding-sphere + Mybatis-Plus 实现主从分离(二)
菜鸟学习笔记,大佬请绕路,转载请标明出处
关于sharding-sphere介绍,这里不做过多解释,请自行百度。
- 创建空Spring boot 项目,官方创建项目地址,pom.xml文件引入相关依赖。
pom.xml文件如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>sharding-sphere</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sharding-sphere</name>
<description>基于Spring Boot 2.1.6 使用sharding-sphere + Mybatis-Plus 实现分库分表</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--Mybatis-Plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.1</version>
</dependency>
<!--shardingsphere start-->
<!-- for spring boot -->
<dependency>
<groupId>io.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>3.1.0</version>
</dependency>
<!-- for spring namespace -->
<dependency>
<groupId>io.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-namespace</artifactId>
<version>3.1.0</version>
</dependency>
<!--shardingsphere end-->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
项目结构图
- 创建创建数据库和表
ds0
├── user_0
└── user_1
ds1
├── user_0
└── user_1
分库分表则表示数据库与表结构一定是一样的
ds0
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for user_0
-- ----------------------------
DROP TABLE IF EXISTS `user_0`;
CREATE TABLE `user_0` (
`id` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- ----------------------------
-- Table structure for user_1
-- ----------------------------
DROP TABLE IF EXISTS `user_1`;
CREATE TABLE `user_1` (
`id` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ds1
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for user_0
-- ----------------------------
DROP TABLE IF EXISTS `user_0`;
CREATE TABLE `user_0` (
`id` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- ----------------------------
-- Table structure for user_1
-- ----------------------------
DROP TABLE IF EXISTS `user_1`;
CREATE TABLE `user_1` (
`id` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-
application.yml 文件配置
数据库的使用的mysql
spring:
application:
name: sharing-sphere-server
main:
allow-bean-definition-overriding: true #设置为true,表示后发现的bean会覆盖之前相同名称的bean。
server:
port: 9961
sharding:
jdbc:
datasource:
names: ds0,ds1
ds0:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver # 使用新版本驱动,则要指定区时serverTimezone
jdbc-url: jdbc:mysql://localhost:3306/ds0?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
username: ***
password: ***
ds1:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://localhost:3306/ds1?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
username: ***
password: ***
# 水平拆分的数据库(表) 配置分库 + 分表策略 行表达式分片策略
# 分库策略
config:
sharding:
default-database-strategy:
inline:
sharding-column: id
algorithm-expression: ds$->{id %2}
# 分表策略 其中user为逻辑表 分表主要取决于age字段
tables:
user:
actual-data-nodes: ds$->{0..1}.user_$->{0..1}
table-strategy:
inline:
sharding-column: age
algorithm-expression: user_$->{age %2} # 分片算法表达式
key-generator-column-name: id
#打印执行的数据库以及语句
props:
sql:
show: true
#mybatis-plus映射mapper文件
mybatis-plus:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.example.shardingSphere.entity
-
使用到类
实体类
mapper文件
dao层
service层以及实现类
简单的测试controller类 -
测试
启动项目,看到如下日志表示项目启动成功,因为我这只配置两个库,所以这里只有两个hikariPool
先用postman往数据库里添加一些测试数据,成功返回true,我这里数据库主键没有设置为自增,
控制台看到执行的日志
查询日志
-
总结
到这里基本上实现了简单的分库分表。在自己实际操作过程中遇到以下问题:
1.连接数据库驱动问题,一开始我用的是com.mysql.jdbc.Driver,结果报如下错误,后来换成提示中的驱动才得以解决。
com.mysql.jdbc.Driver 是 mysql-connector-java 5中的
com.mysql.cj.jdbc.Driver 是 mysql-connector-java 6中的
2.使用新的驱动,连接数据库url要加上区时serverTimezone,如果不加会出现如下错误:
serverTimezone=UTC ,会比中国时间早8个小时,即java中的设置的时间与数据中的不一致
如果在中国可以设置:
//北京时间东八区 serverTimezone=GMT%2B8
//或者使用上海时间 serverTimezone=Asia/Shanghai
参考文献:https://mp.weixin.qq.com/s/2_W4w8KW3lTGYYA9fUJGzw
https://shardingsphere.apache.org/document/current/cn/manual/
项目地址:https://github.com/Breet1413/sharding-sphere
后期将完善读写分离、数据分片 + 读写分离、数据分片 + 数据脱敏案例等。