1、搭建环

(1) 技术: SpringBoot2.2.1+ MyBatisPlus + Sharding-JDBC + Druid 连接池
(2)创建 SpringBoot 工程

(3)修改工程 SpringBoot 版本 2.2.1

引入依赖

    <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.20</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.0.0-RC1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>

2、 按照水平分表的方式, 创建数据库和数据库表
(1)创建数据库 course_db
(2)在数据库创建两张表 course_1 和 course_2
(3)约定规则:如果添加课程 id 是偶数把数据添加 course_1,如果奇数添加到 course_2

CREATE TABLE `course_db`.`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 `course_db`.`course_2` (
`cid` bigint(20) NOT NULL,
`cname` varchar(50) NULL,
`user_id` bigint(20) NULL,
`cstatus` varchar(10) NULL,
PRIMARY KEY (`cid`)
);

3.创建实体类和mapper

Course.java

package com.weianlai.shardingjdbc.entity;

import lombok.Data;

@Data
public class Course{ private static final long serialVersionUID = 1L; private Long cid; private String cname; private Long userId; private String cstatus; }

CourseMapper.java

package com.weianlai.shardingjdbc.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.weianlai.shardingjdbc.entity.Course;
import org.springframework.stereotype.Repository; @Repository
public interface CourseMapper extends BaseMapper<Course> {
}

启动类扫描mapper

@MapperScan("com.weianlai.shardingjdbc.mapper")

4、配置 Sharding-JDBC 分片策略
(1)在项目 application.properties 配置文件中进行配置

## 配置数据源,给数据源起名称
spring.shardingsphere.datasource.names=m1
## 一个实体类对应两张表,覆盖
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/course_db?serverTimezone=GMT%2B8
spring.shardingsphere.datasource.m1.username=root
spring.shardingsphere.datasource.m1.password=root #指定 course 表分布情况,配置表在哪个数据库里面,表名称都是什么 m1.course_1 ,m1.course_2
spring.shardingsphere.sharding.tables.course.actual-data-nodes=m1.course_$->{1..2} # 指定 course 表里面主键 cid 生成策略 SNOWFLAKE
spring.shardingsphere.sharding.tables.course.key-generator.column=cid
spring.shardingsphere.sharding.tables.course.key-generator.type=SNOWFLAKE # 指定分片策略 约定 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

5、编写测试代码

package com.weianlai.shardingjdbc;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.weianlai.shardingjdbc.entity.Course;
import com.weianlai.shardingjdbc.mapper.CourseMapper;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class)
@SpringBootTest
class ShardingJdbcDemoApplicationTests { @Autowired
CourseMapper courseMapper; @Test
public void addCourse() {
for (int i = 0; i < 10; i++) {
Course course = new Course();
course.setCname("java");
course.setUserId(100L);
course.setCstatus("Normal");
courseMapper.insert(course);
}
} @Test
public void findCourse() {
QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("cid", 581259549126688769L);
Course course = courseMapper.selectOne(queryWrapper);
System.out.println("coursr=" + course);
} }

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

  1. mysql中的优化, 简单的说了一下垂直分表, 水平分表(有几种模运算),读写分离.

    一.mysql中的优化 where语句的优化 1.尽量避免在 where 子句中对字段进行表达式操作select id from uinfo_jifen where jifen/60 > 100 ...

  2. mycat - 水平分表

    相对于垂直拆分的区别是:垂直拆分是把不同的表拆到不同的数据库中,而水平拆分是把同一个表拆到不同的数据库中.水平拆分不是将表的数据做分类,而是按照某个字段的某种规则来分散到多个库之中,每个表中包含一部分 ...

  3. mycat水平分表

    和垂直分库不同,水平分表,是将那些io频繁,且数据量大的表进行水平切分. 基本的配置和垂直分库一样,我们需要改的就是我们的 schema.xml和rule.xml文件配置(server.xml不用做任 ...

  4. mysql 水平分表技术

    这里做的是我的一个笔记. 水平分表比较简单, 理解就是: 合并的表使用的必须是MyISAM引擎 表的结构必须一致,包括索引.字段类型.引擎和字符集 数据表 user1 CREATE TABLE `us ...

  5. 玩转SpringBoot之整合Mybatis拦截器对数据库水平分表

    利用Mybatis拦截器对数据库水平分表 需求描述 当数据量比较多时,放在一个表中的时候会影响查询效率:或者数据的时效性只是当月有效的时候:这时我们就会涉及到数据库的分表操作了.当然,你也可以使用比较 ...

  6. MySQL常见水平分表技术方案

    根据经验,Mysql表数据一般达到百万级别,查询效率会很低,容易造成表锁,甚至堆积很多连接,直接挂掉:水平分表能够很大程度较少这些压力. 1.按时间分表 这种分表方式有一定的局限性,当数据有较强的实效 ...

  7. mysql使用MRG_MyISAM(MERGE)实现水平分表

    在MySQL中数据的优化尤其是大数据量的优化是一门很大的学问,当然其它数据库也是如此,即使你不是DBA,做为一名程序员掌握一些基本的优化信息,也可以让你在自己的程序开发中受益匪浅.当然数据库的优化有很 ...

  8. mysql数据库的水平分表与垂直分表实例讲解

    mysql语句的优化有局限性,mysql语句的优化都是围绕着索引去优化的,那么如果mysql中的索引也解决不了海量数据查询慢的状况,那么有了水平分表与垂直分表的出现(我就是记录一下自己的理解) 水平分 ...

  9. TDSQL MySQL版基本原理-水平分表 读写分离 弹性扩展 强同步

    TDSQL MySQL版(TDSQL for MySQL)是部署在腾讯云上的一种支持自动水平拆分.Shared Nothing 架构的分布式数据库.TDSQL MySQL版 即业务获取的是完整的逻辑库 ...

随机推荐

  1. Python import Queue ImportError: No module named 'Queue'

    python3 中引入Queue 会报出这个问题 python3 中这样引入 import queue python2 中这样引入 import Queue 为了兼容 可以这样 import sys ...

  2. 在Ubuntu下的C语言编程

    以运行在虚拟机下的Ubuntu为例: mkdir fenchen 来创建一个文件夹 cd fenchen 切换到这个文件夹下面 vi test.c 创建并编辑一个test.c文件 按 i 编辑,之后把 ...

  3. Web实时通信,SignalR真香,不用愁了

    前言 对于B/S模式的项目,基础的场景都是客户端发起请求,服务端返回响应结果就结束了一次连接:但在很多实际应用场景中,这种简单的请求和响应模式就显得很吃力,比如消息通知.监控看板信息自动刷新等实时通信 ...

  4. 一文搞懂js中的typeof用法

    基础 typeof 运算符是 javascript 的基础知识点,尽管它存在一定的局限性(见下文),但在前端js的实际编码过程中,仍然是使用比较多的类型判断方式. 因此,掌握该运算符的特点,对于写出好 ...

  5. robotframework定位页面内Frame框架里的元素

    在自动化开发中,会遇到在页面内部点开一个新的页面后,新的页面元素无法定位到的情况,如点击下图的上传图片,弹出的界面无法直接定位到: 遇到这种情况需要查看弹出界面是否是Frame框架页面:打开火狐浏览器 ...

  6. MySQL之DDL数据定义语言:库、表的管理

    库的管理 常用命令 #创建库 create database if not exists 库名 [ character set 字符集名]; create database if not exists ...

  7. 移动GPU分类/百科

    ARM mali gpu四大微架构概述 https://zhuanlan.zhihu.com/p/107141045 http://www.neardi.com/news_23/487.html

  8. Android ANR从原理到日志分析,记下来就够了

    站在巨人的肩膀上可以看的更远 做一个优秀的搬运工 Android 彻底理解安卓应用无响应机制 Android ANR日志分析全面解析 优秀的文章不可独享,要扩散,要做好笔记,哈 <沁园春长沙&g ...

  9. c++学习笔记(六)

    windows批处理 什么是批处理? 批处理(Batch),也称为批处理脚本. 顾名思义,批处理就是对某对象进行批量的处理.批处理文件的扩展名为bat. 批处理文件(batch file)包含一系列 ...

  10. requests的get请求基本使用

    官方文档 https://docs.python-requests.org/zh_CN/latest/   快速上手 https://docs.python-requests.org/zh_CN/la ...