当前位置:网站首页>五分钟带你上手ShardingJDBC实现MySQL分库分表
五分钟带你上手ShardingJDBC实现MySQL分库分表
2022-08-01 14:52:00 【InfoQ】
Mysql环境
项目配置
依赖引入
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
<version>5.1.0</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
配置文件配置
# 配置真实数据源,ds{0..2}
spring.shardingsphere.datasource.names=ds0,ds1,ds2
# 配置第 1 个数据源
spring.shardingsphere.datasource.ds0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds0.jdbc-url=jdbc:mysql://192.168.31.241:3306/testdb
spring.shardingsphere.datasource.ds0.username=root
spring.shardingsphere.datasource.ds0.password=root
# 配置第 2 个数据源
spring.shardingsphere.datasource.ds1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds1.jdbc-url=jdbc:mysql://192.168.31.242:3306/testdb
spring.shardingsphere.datasource.ds1.username=root
spring.shardingsphere.datasource.ds1.password=root
# 配置第 3 个数据源
spring.shardingsphere.datasource.ds2.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds2.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds2.jdbc-url=jdbc:mysql://192.168.31.243:3306/testdb
spring.shardingsphere.datasource.ds2.username=root
spring.shardingsphere.datasource.ds2.password=root
# 标准分片表配置ds->{0..2}的含义是
# 针对employee表新增数据时,有三个数据源ds0、ds1、ds2中都有employee表
# spring.shardingsphere.rules.sharding.tables.employee.actual-data-nodes=ds$->{0..2}.employee
# 定义数据源的分片规则,按employee表的id % 3 取模得到数据应放在哪个数据源
spring.shardingsphere.rules.sharding.sharding-algorithms.database-inline.type=INLINE
spring.shardingsphere.rules.sharding.sharding-algorithms.database-inline.props.algorithm-expression=ds$->{id % 3}
# 定义哪一个列用于生成主键 employee对应的是相应的表名
spring.shardingsphere.rules.sharding.tables.employee.key-generate-strategy.column=id
# 定义employee表哪个是分片字段,这里按主键字段id,这个表示基于哪一个列进行分片
spring.shardingsphere.rules.sharding.tables.employee.database-strategy.standard.sharding-column=id
# 将employee表与分片规则database-inline绑定
spring.shardingsphere.rules.sharding.tables.employee.database-strategy.standard.sharding-algorithm-name=database-inline
# 默认主键生成策略采用snowflake
spring.shardingsphere.sharding.default-key-generate-strategy.xxx=snowflake
#SNOWFLAKE算法配置
spring.shardingsphere.rules.sharding.key-generators.snowflake.type=SNOWFLAKE
#机器唯一标识
spring.shardingsphere.rules.sharding.key-generators.snowflake.props.worker-id=666
#显示分库分表后执行的SQL语句
spring.shardingsphere.props.sql-show=true
测试使用
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itlaoqi.shardingjdbc.entity.Employee;
public interface EmployeeMapper extends BaseMapper<Employee> {
}
mport com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
@TableName("employee")
public class Employee {
@TableId
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itlaoqi.shardingjdbc.entity.Employee;
import com.itlaoqi.shardingjdbc.mapper.EmployeeMapper;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.util.List;
@SpringBootTest
public class EmployeeTestor {
@Resource
private EmployeeMapper employeeMapper;
@Test
public void testInsert(){
for(int i = 0 ; i < 10 ; i++) {
Employee employee = new Employee();
employee.setName("MJ" + i);
employeeMapper.insert(employee);
}
}
@Test
public void testSelect(){
List<Employee> employees = employeeMapper.selectList(new QueryWrapper<>());
}
}
边栏推荐
猜你喜欢

LeetCode50天刷题计划(Day 10—— 三数之和(20.50-22.40)

Koreographer Professional Edition丨一款Unity音游插件教程

Inflation continues, Kenya's food security a concern

MySQL:索引

LeetCode50天刷题计划(Day 8—— 盛最多水的容器(23.00-1.20)

考研大事件!这6件事考研人必须知道!

The soul asks: How does MySQL solve phantom reads?

【LeetCode】37、解数独

强网杯2022 pwn 赛题解析——yakagame

股票策略02 | 技术择时+行业因子+市值轮动
随机推荐
反序列化漏洞详解
2022年5月20日最全摸鱼游戏导航
docker部署mysql并修改其占用内存大小
SQL每日一练(牛客新题库)——第2天: 条件查询
Pytorch - Distributed Model Training
测试如何拓展自己的知识面?
May 20, 2022 The most complete fish game navigation
产品力无提升的雷克萨斯新款ES ,为何敢于涨价?
WPF如何自定义隐藏下拉框选项
信息录入率百分百上海强化施工现场建筑工人实名制管理
datetime64[ns]转化为datetime
透过现象看本质,如何针对用户做好需求分析
接口测试框架开发实践5:配置文件读取
SQL查询数据以及排序
搭建ntp时间服务器(安装sql2000配置服务器失败)
长江欧拉生态创新中心成立,武汉数字经济再添坚实底座
MBI5020 LED驱动
直播系统聊天技术(八):vivo直播系统中IM消息模块的架构实践
HTB-Mirai
牛客刷SQL--3