1、需求分析

CREATE TABLE `edu_db_1`.`course_1`  (
`cid` bigint(20) NOT NULL,
`cname` varchar(50) NULL,
`user_id` bigint(20) NULL,
`cstatus` varchar(10) NULL,
PRIMARY KEY (`cid`)
); CREATE TABLE `edu_db_1`.`course_2` (
`cid` bigint(20) NOT NULL,
`cname` varchar(50) NULL,
`user_id` bigint(20) NULL,
`cstatus` varchar(10) NULL,
PRIMARY KEY (`cid`)
); CREATE TABLE `edu_db_2`.`course_1` (
`cid` bigint(20) NOT NULL,
`cname` varchar(50) NULL,
`user_id` bigint(20) NULL,
`cstatus` varchar(10) NULL,
PRIMARY KEY (`cid`)
); CREATE TABLE `edu_db_2`.`course_2` (
`cid` bigint(20) NOT NULL,
`cname` varchar(50) NULL,
`user_id` bigint(20) NULL,
`cstatus` varchar(10) NULL,
PRIMARY KEY (`cid`)
);

3、在 SpringBoot 配置文件配置数据库分片规则

## 配置数据源,给数据源起名称
# 水平分库,配置两个数据源
spring.shardingsphere.datasource.names=m1,m2
## 一个实体类对应两张表,覆盖
spring.main.allow-bean-definition-overriding=true
##配置第一个数据源具体内容,包含连接池,驱动,地址,用户名和密码
spring.shardingsphere.datasource.m1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m1.url=jdbc:mysql://localhost:3306/edu_db_1?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m1.username=root
spring.shardingsphere.datasource.m1.password=root ##配置第二个数据源具体内容,包含连接池,驱动,地址,用户名和密码
spring.shardingsphere.datasource.m2.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.m2.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.m2.url=jdbc:mysql://localhost:3306/edu_db_2?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m2.username=root
spring.shardingsphere.datasource.m2.password=root #指定数据库分布情况,数据库里面表分布情况
# m1 m2 course_1 course_2
spring.shardingsphere.sharding.tables.course.actual-data-nodes=m$->{1..2}.course_$->{1..2} # 指定 course 表里面主键 cid 生成策略 SNOWFLAKE
spring.shardingsphere.sharding.tables.course.key-generator.column=cid
spring.shardingsphere.sharding.tables.course.key-generator.type=SNOWFLAKE # 指定数据库分片策略 约定 user_id 是偶数添加 m1,是奇数添加 m2
#spring.shardingsphere.sharding.default-database-strategy.inline.shardingcolumn=user_id
#spring.shardingsphere.sharding.default-database-strategy.inline.algorithmexpression=m$->{user_id % 2 + 1}
spring.shardingsphere.sharding.tables.course.database-strategy.inline..sharding-column=user_id
spring.shardingsphere.sharding.tables.course.database-strategy.inline.algorithm-expression=m$->{user_id % 2 + 1} # 指定表分片策略 约定 cid 值偶数添加到 course_1 表,如果 cid 是奇数添加到 course_2表
spring.shardingsphere.sharding.tables.course.table-strategy.inline.sharding-column=cid
spring.shardingsphere.sharding.tables.course.table-strategy.inline.algorithm-expression=course_$->{cid % 2 + 1} # 打开 sql 输出日志
spring.shardingsphere.props.sql.show=true

 4、编写测试方法

    //添加操作
@Test
public void addCourseDb() {
for (int i = 0; i < 10; i++) {
Course course = new Course();
course.setCname("javademo" + i);
//分库根据 user_id
course.setUserId(100L + i);
course.setCstatus("Normal1");
courseMapper.insert(course);
}
}
//查询操作
@Test
public void findCourseDb() {
QueryWrapper<Course> wrapper = new QueryWrapper<>();
//设置 userid 值
wrapper.eq("user_id",101L);
//设置 cid 值
wrapper.eq("cid",581615031192387584L);
Course course = courseMapper.selectOne(wrapper);
System.out.println(course);
}

Sharding-JDBC 实现水平分库分表的更多相关文章

  1. 分库分表中间件sharding-jdbc的使用

    数据分片产生的背景,可以查看https://shardingsphere.apache.org/document/current/cn/features/sharding/,包括了垂直拆分和水平拆分的 ...

  2. MySQL 分库分表方案,总结的非常好!

    前言 公司最近在搞服务分离,数据切分方面的东西,因为单张包裹表的数据量实在是太大,并且还在以每天60W的量增长. 之前了解过数据库的分库分表,读过几篇博文,但就只知道个模糊概念, 而且现在回想起来什么 ...

  3. mysql分库分表(二)

    mysql分库分表 参考: https://www.cnblogs.com/dongruiha/p/6727783.html https://www.cnblogs.com/oldUncle/p/64 ...

  4. 采用Sharding-JDBC解决分库分表

    源码:Sharding-JDBC(分库分表) 一.Sharding-JDBC介绍 1,介绍 Sharding-JDBC是当当网研发的开源分布式数据库中间件,从 3.0 开始Sharding-JDBC被 ...

  5. 分库分表之ShardingSphere

    目录 分库分表诞生的前景 分库分表的方式(垂直拆分,水平复制) 1.垂直拆分 1.1 垂直分库 1.2 垂直分表 2.水平拆分 2.1 水平分库 2.2 水平分表 分库分库中间件 ShardingSp ...

  6. sharding-jdbc之——分库分表实例

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/79368021 一.概述 之前,我们介绍了利用Mycat进行分库分表操作,Mycat ...

  7. Java互联网架构-Mysql分库分表订单生成系统实战分析

    概述 分库分表的必要性 首先我们来了解一下为什么要做分库分表.在我们的业务(web应用)中,关系型数据库本身比较容易成为系统性能瓶颈,单机存储容量.连接数.处理能力等都很有限,数据库本身的“有状态性” ...

  8. Mysql 分库分表方案

    0 引言 当一张表的数据达到几千万时,你查询一次所花的时间会变多,如果有联合查询的话,我想有可能会死在那儿了.分表的目的就在于此,减小数据库的负担,缩短查询时间. mysql中有一种机制是表锁定和行锁 ...

  9. 【转】mysql分库分表,数据库分库分表思路

    原文:https://www.cnblogs.com/butterfly100/p/9034281.html 同类参考:[转]数据库的分库分表基本思想 数据库分库分表思路   一. 数据切分 关系型数 ...

随机推荐

  1. Codeforces Round #736 (Div. 2)

    A,B,C就不说了,又被D题卡住了..... 感觉怎么说呢,就是题解中的三个提示都已经想到了,就是不知道该怎么解决.... D. Integers Have Friends 简述题意:题目要求你找一个 ...

  2. 最近公共祖先(lca)与树上叉分

    lca的定义不在过多解释, 代码如下: inline void bfs() { queue<int>q; deep[s]=1;q.push(s); while(!q.empty()) { ...

  3. xmind 文件 打开后会在当前目录生成 configuration,p2和workspace目录,artifacts.xml文件 解决

    在xmind安装目录下的xmind.ini修改如下配置,为绝对路径

  4. 基于EPPlus和NPOI实现的Excel导入导出

    基于EPPlus和NPOI实现的Excel导入导出 CollapseNav.Net.Tool.Excel(NuGet地址) 太长不看 导入 excel 文件流将会转为 ExcelTestDto 类型的 ...

  5. 数据库学习笔记 - MySQL基础知识

    一.数据库基础知识 1.1 Whats's 数据库 数据库(Database,DB):将大量数据保存起来,通过计算机加工而成的可以进行高效访问的数据集合.如:大型-银行存储的信息,小型-电话簿. 数据 ...

  6. ajax的post请求获取kfc官网数据

    # _*_ coding : utf-8 _*_# @Time : 2021/11/2 13:45# @Author : 秋泊酱 # 1页 # http://www.kfc.com.cn/kfccda ...

  7. UDP端口检查告警SHELL脚本(企业微信版机器人版)

    脚本准备 0Batch_Check.sh 1port_check.sh 2wechat_bot_alert.sh CheckList CheckList #支持大/小写 10.1.1.5 Udp 53 ...

  8. 第二次SQLServer试验解

    1 --给BookInfo表的BookId建立主键约束,给BookInfo表的BookName建立非空约束 2 create table BookInfo( 3 BookId int primary ...

  9. spring boot的mybatis开启日志

    logging: level: com: xxx: xxxx: xxxx: mapper: DEBUG logging.level.mapper对应的包名=DEBUG

  10. 明明pip安装python的模块了,pycharm还是找不到的解决方案

    以前pycharm的安装包和python的环境一直都不能融合在一起,到了今天才知道,原来他们都是有自己的工作环境的 自己的工作环境(虚拟解释器)和安装python的工作环境(基本解释器)不是一个环境, ...