当前位置: 首页 > news >正文

Sharding-Springboot-mybatis-plus整合(三)-inline策略

Sharding-Springboot-mybatis-plus整合(三)

1.简介

本节目标,使用SpringBoot整合Sharding和Mybatis-Plus验证上节分片策略

从配置文件上看策略包括( inline、standard、complex、hint)

环境搭建以inline策略演示

inline 策略

inline策略是简单的表达式策略,不支持特殊范围查询

代码地址

https://gitee.com/wn2019/sharding-study1

2.环境代码搭建

2.1Maven依赖

Mybatis-plus-3.5.3
Druid-1.1.22
Springboot-2.3.1.RELEASE

依赖POM

<?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><groupId>org.example</groupId><artifactId>sharding-study1</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><sharding-jdbc-version>4.1.1</sharding-jdbc-version></properties><!-- spring boot依赖 --><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.1.RELEASE</version></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!-- mybatis-plus --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.3</version></dependency><!-- sharding-jdbc springboot的依赖包 --><dependency><groupId>org.apache.shardingsphere</groupId><artifactId>sharding-jdbc-spring-boot-starter</artifactId><version>${sharding-jdbc-version}</version></dependency><dependency><groupId>org.apache.shardingsphere</groupId><artifactId>sharding-jdbc-spring-namespace</artifactId><version>${sharding-jdbc-version}</version></dependency><!-- MySql驱动 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.22</version></dependency></dependencies>
</project>

2.2SQL

新增两个订单表 t_order_1t_order_2

CREATE TABLE `t_order_1` (`order_id` bigint NOT NULL COMMENT '主键',`order_name` varchar(50) DEFAULT NULL COMMENT '订单名称',`order_type` varchar(50) DEFAULT NULL COMMENT '订单类型',`order_desc` varchar(200) DEFAULT NULL COMMENT '订单详情',`create_user_id` int DEFAULT NULL COMMENT '创建人',`create_user_name` varchar(50) DEFAULT NULL COMMENT '创建人姓名',`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '创建时间',PRIMARY KEY (`order_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;CREATE TABLE `t_order_2` (`order_id` bigint NOT NULL COMMENT '主键',`order_name` varchar(50) DEFAULT NULL COMMENT '订单名称',`order_type` varchar(50) DEFAULT NULL COMMENT '订单类型',`order_desc` varchar(200) DEFAULT NULL COMMENT '订单详情',`create_user_id` int DEFAULT NULL COMMENT '创建人',`create_user_name` varchar(50) DEFAULT NULL COMMENT '创建人姓名',`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '创建时间',PRIMARY KEY (`order_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;

2.3代码部分

实体类

Order.java

package com.wnn.sd.pojo;import java.util.Date;import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;import lombok.Data;/*** 订单表*/
@Data
@TableName(value = "t_order")
public class Order {// 订单ID@TableId("order_id")private Long orderId;// 订单名称private String orderName;// 订单类型private String orderType;// 订单详情描述private String orderDesc;// 创建人private int createUserId;// 创建人姓名private String createUserName;// 创建时间private Date createTime;}

mapper

IOrderMapper.class

package com.wnn.sd.mapper;import org.springframework.stereotype.Repository;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wnn.sd.pojo.Order;@Repository
public interface IOrderMapper extends BaseMapper<Order> {}

启动类

MyShardingJdbcApplication

package com.wnn.sd;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication()
@MapperScan("com.wnn.sd.mapper")
public class MyShardingJdbcApplication {public static void main(String[] args) {SpringApplication.run(MyShardingJdbcApplication.class, args);}}

测试类

package com.wnn.sd;import java.util.List;import javax.annotation.Resource;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;import com.wnn.sd.mapper.IOrderMapper;
import com.wnn.sd.pojo.Order;@RunWith(SpringRunner.class)
@SpringBootTest
public class ShardingTestBoot {@Resourceprivate IOrderMapper orderMapper;/*** 添加数据*/@Testpublic void addOrder() {for (int i = 0; i < 10; i++) {Order order = new Order();order.setOrderName("wn" + i);orderMapper.insert(order);}}/*** 查询列表数据*/@Testpublic void queryList() {List<Order> orders = orderMapper.selectList(null);orders.forEach(o -> System.out.println(o));}}

2.4 配置文件

#  单库分表 配置
spring:shardingsphere:datasource:# 配置数据库名称 相当于给数据源取别名(可以配置多个库,以逗号隔开)names: m1# 配置具体数据库连接信息m1:type: com.alibaba.druid.pool.DruidDataSourcedriverClassName: com.mysql.cj.jdbc.Driver# 配置 数据库 testurl: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTCusername: rootpassword: root# 分片策略sharding:# 配置不同表的 分片策略tables:# 配置 具体的 逻辑表的 分片策略t_order:# t_order 订单表的 主键规则keyGenerator:# 主键列column: order_id# 主键生成规则 (SNOWFLAKE 雪花算法 生成分布式唯一ID)type: SNOWFLAKE# 未知作用#            props:#              worker:#                id: 1# 配置 t_order 订单表的 具体数据库物理表的映射关系 表达式actualDataNodes: m1.t_order_$->{1..2}# 表策略tableStrategy:inline:# 分片列sharding-column: order_id# 分片规则 表达式(映射到具体的物理表 )algorithm-expression: t_order_$->{order_id % 2 + 1}# 配置是否打印SQLprops:sql.show: true# 解决一个bean映射到多张表问题main:allow-bean-definition-overriding: true

2.5 测试类代码

package com.wnn.sd;import java.util.List;import javax.annotation.Resource;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;import com.wnn.sd.mapper.IOrderMapper;
import com.wnn.sd.pojo.Order;@RunWith(SpringRunner.class)
@SpringBootTest
public class ShardingTestBoot {@Resourceprivate IOrderMapper orderMapper;/*** 添加数据*/@Testpublic void addOrder() {for (int i = 0; i < 10; i++) {Order order = new Order();order.setOrderName("wn" + i);orderMapper.insert(order);}}/*** 查询列表数据*/@Testpublic void queryList() {List<Order> orders = orderMapper.selectList(null);orders.forEach(o -> System.out.println(o));}}

至此代码完成

3.测试效果

3.1 运行测试类代码addOrder()

按照分片策略inline,新增的数据按照ID取摸+1分配到两个表中

执行效果
在这里插入图片描述
在这里插入图片描述

可以发现SQL执行的时候分为

Actual SQL(实际SQL)和Logic SQL(逻辑SQL)

查看t_order_1和t_order_2表中,t_order_1中有7条数据,主键都是偶数
在这里插入图片描述
在这里插入图片描述

3.2 运行测试类代码queryList()

可以看到从两个表中查询所有数据

3.3 简单条件查询

3.3.1使用等于查询

从表2取一个订单表按主键查询“1629805480188895233”

从日志发现,精确的路由到order_2只执行了一次查询

[           main] ShardingSphere-SQL                       : Actual SQL: m1 ::: SELECT  order_id,order_name,order_type,order_desc,create_user_id,create_user_name,create_time  FROM t_order_2 WHERE (order_id = ?) ::: [1629805480188895233]
Order(orderId=1629805480188895233, orderName=wn2, orderType=null, orderDesc=null, createUserId=0, createUserName=null, createTime=Mon Feb 27 03:28:01 CST 2023)

3.3.2使用范围IN查询

查询结果同3.3.1,从日志发现,精确的路由到order_2只执行了一次查询

3.3.3 使用范围

直接报错’Inline strategy cannot support this type ‘,说明inline策略不支持between范围查询

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.lang.IllegalStateException: Inline strategy cannot support this type sharding:RangeRouteValue(columnName=order_id, tableName=t_order, valueRange=[1629805480188895233‥1629805480214061057])
### The error may exist in com/wnn/sd/mapper/IOrderMapper.java (best guess)
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: SELECT  order_id,order_name,order_type,order_desc,create_user_id,create_user_name,create_time  FROM t_order     WHERE (order_id BETWEEN ? AND ?)
### Cause: java.lang.IllegalStateException: Inline strategy cannot support this type sharding:RangeRouteValue(columnName=order_id, tableName=t_order, valueRange=[1629805480188895233‥1629805480214061057])

相关文章:

Sharding-Springboot-mybatis-plus整合(三)-inline策略

Sharding-Springboot-mybatis-plus整合&#xff08;三&#xff09; 1.简介 本节目标&#xff0c;使用SpringBoot整合Sharding和Mybatis-Plus验证上节分片策略 从配置文件上看策略包括&#xff08; inline、standard、complex、hint&#xff09; 环境搭建以inline策略演示 …...

编码的基本概念

本专栏包含信息论与编码的核心知识&#xff0c;按知识点组织&#xff0c;可作为教学或学习的参考。markdown版本已归档至【Github仓库&#xff1a;information-theory】&#xff0c;需要的朋友们自取。或者公众号【AIShareLab】回复 信息论 也可获取。 文章目录信源编码分类前缀…...

函数指针与指针函数的区别

目录&#xff1a;一、函数指针1 函数类型2 函数指针(指向函数的指针)3 函数指针数组二.函数指针和指针函数比较1 定义不同2 写法不同3.用法不同三.函数指针做函数参数(回调函数)1 利用回调函数实现打印任意类型数据2 提供能够打印任意类型数组函数3 利用回调函数 提供查找功能四…...

死锁的四个必要条件以及如何避免死锁

死锁的四个必要条件以及如何避免死锁 一.什么是死锁&#xff1f;二.死锁的四个必要条件 1.互斥条件&#xff1a;2.请求与保持条件&#xff1a;3.不剥夺条件:4.循环等待条件: 三.如何避免死锁 1.破坏请求保持条件2.破坏不剥夺条件3.破坏循环等待条件 死锁的四个必要条件以及如…...

浏览器多线程到事件循环机制

浏览器与js运行机制 进程与线程 进程 进程是CPU分配资源的最小单位&#xff0c;它是一个可以自己独立运行且拥有自己资源空间的任务程序&#xff1b;包括程序以及程序所使用的内存及系统资源 线程 线程是CPU调度的最小单位&#xff0c;它就是程序中的一个执行流&#xff1…...

Lambda表达式的本质

一直想写一篇文章&#xff0c;来总结lambda表达式&#xff0c;但是之前感觉总结的不是特别到位&#xff0c;现在看了几篇文章和视频后&#xff0c;感觉对lambda表达式有了比较深刻的认识&#xff0c;现在进行记录总结如下&#xff1a; lambda表达式又叫做匿名函数&#xff0c;…...

类的加载过程(生命周期)

类的加载过程(生命周期) 一、装载&#xff1a;通过一个类的全限定名获取定义此类的二进制字节流将这个字节流所代表的静态存储结构转化为方法区的运行时数据结构在内存中生成一个代表这个类的java.lang.Class对象&#xff08;将字节码加载到内存中&#xff09;&#xff0c;作为…...

2023最新谷粒商城笔记之MQ消息队列篇(全文总共13万字,超详细)

MQ消息队列 其实队列JDK中本身就有&#xff0c;不过这种队列也只能单体服务可能会使用&#xff0c;一旦项目使用的分布式架构&#xff0c;那么一定还是需要用到一个消息中间件的。我们引入消息队列的原因就是对我们的页面相应速度再优化&#xff0c;让用户的体验更好&#xff…...

多变量线性回归模型

多变量线性回归模型 模型参数为n1维向量&#xff0c;此时模型公式为 hθ(x)θ0x0θ1x1θ2x2...θnxnh_{\theta}(x)\theta_{0}x_{0}\theta_{1}x_{1}\theta_{2}x_{2}...\theta_{n}x_{n} hθ​(x)θ0​x0​θ1​x1​θ2​x2​...θn​xn​ 可以简化为 hθ(x)θTXh_{\theta}(x)\th…...

php 基于ICMP协议实现一个ping命令

php 基于ICMP协议实现一个ping命令 网络协议是什么ICMP 协议什么是ICMP?ICMP 的主要功能ICMP 在 IPv4 和 IPv6 的封装Wireshark抓包ICMP 请求包分析PHP构建 ICMP 数据包php中的 pack & unpack 函数字节和字符packunpackICMP计算校验和步骤总结网络协议是什么 网络协议&…...

Java基本数据类型

1.概述 佛说&#xff0c;大千世界&#xff0c;无奇不有。在这个世界里&#xff0c;物种的多样性&#xff0c;遍地开花&#xff0c;同样&#xff0c;在Java的世界里&#xff0c;也有着异曲同工之妙&#xff0c;Java秉承面向对象的特性&#xff0c;必然少不了区分对象的类型&…...

English Learning - L2 语音作业打卡 Day2 2023.2.22 周三

English Learning - L2 语音作业打卡 Day2 2023.2.22 周三&#x1f48c; 发音小贴士&#xff1a;&#x1f48c; 当日目标音发音规则/技巧&#xff1a;&#x1f36d; Part 1【热身练习】&#x1f36d; Part2【练习内容】&#x1f36d;【练习感受】&#x1f353;元音[ ɑː ]&…...

45. 跳跃游戏 II

题目&#xff1a; 45. 跳跃游戏 II难度中等1974收藏分享切换为英文接收动态反馈给定一个长度为 n 的 0 索引整数数组 nums。初始位置为 nums[0]。每个元素 nums[i] 表示从索引 i 向前跳转的最大长度。换句话说&#xff0c;如果你在 nums[i] 处&#xff0c;你可以跳转到任意 num…...

应届生Java面试50题线程篇(含解析)

什么是线程&#xff1f; 答&#xff1a;线程是操作系统能够进行运算调度的最小单位&#xff0c;是程序执行流的最小单元。在Java中&#xff0c;可以通过实现Runnable接口或继承Thread类来创建线程。 创建线程的方式有哪些?各自的优缺点是什么&#xff1f; 继承 Thread 类&…...

【数据库】第七章 数据库设计

第七章数据库设计 数据库设计概述 数据库设计的基本步骤 需求分析概念结构设计逻辑结构设计物理结构设计数据库实施数据库运行和维护 需求分析 收集需求&#xff0c;理解需求 收集各个角色的需求 概念数据库设计 建立概念模型 &#xff0c;E-R图/IDEF1x图 消除冲突&…...

Burp Suite 常用模块简介

Burp Suite 常用模块分为 目标站点(target)模块 代理(proxy)模块 攻击(Intruder)模块 重放(Repeater) 模块 Target模块是对站点资源的收集&#xff0c;与站点各资源包发出和相应包的记录 Proxy模块是核心模块&#xff0c;可以拦截数据包发送往浏览器&#xff0c;进行修改后再…...

QML Item和Rectangle详解

1.Item和Rectangle Item类型是Qt Quick中所有可视项的基本类型。 Qt Quick中的所有可视项都继承Item。尽管Item对象没有视觉外观&#xff0c;但它定义了视觉项中常见的所有属性&#xff0c;例如x和y位置、宽度和高度、锚定和键处理支持。 Rectangle继承自Item&#xff0c;多…...

常见前端基础面试题(HTML,CSS,JS)(六)

GET 和 POST 的区别 从 http 协议的角度来说&#xff0c;GET 和 POST 它们都只是请求行中的第一个单词&#xff0c;除了语义不同&#xff0c;其实没有本质的区别。 之所以在实际开发中会产生各种区别&#xff0c;主要是因为浏览器的默认行为造成的。 受浏览器的影响&#xf…...

深度学习 李沐报错

3.6. softmax回归的从零开始实现 — 动手学深度学习 2.0.0 documentation softmax从0开始实现 函数执行需要加main指定 改成这样 if __name__"__main__":print(evaluate_accuracy(net, test_iter)) 不然会这样出错 RuntimeError: An attempt has been m…...

【JAVA程序设计】(C00104)基于Springboot的家庭理财管理系统——有文档

基于Springboot的家庭理财管理系统项目简介项目获取开发环境项目技术运行截图运行视频项目简介 基于Springboot开发的家庭理财管理系统设计与实现共分为三个角色&#xff1a;系统管理员、家庭管理员、家庭用户 管理员角色包含以下功能&#xff1a; 用户管理、修改密码、角色管…...

XCTF-web-easyupload

试了试php&#xff0c;php7&#xff0c;pht&#xff0c;phtml等&#xff0c;都没有用 尝试.user.ini 抓包修改将.user.ini修改为jpg图片 在上传一个123.jpg 用蚁剑连接&#xff0c;得到flag...

TDengine 快速体验(Docker 镜像方式)

简介 TDengine 可以通过安装包、Docker 镜像 及云服务快速体验 TDengine 的功能&#xff0c;本节首先介绍如何通过 Docker 快速体验 TDengine&#xff0c;然后介绍如何在 Docker 环境下体验 TDengine 的写入和查询功能。如果你不熟悉 Docker&#xff0c;请使用 安装包的方式快…...

Xshell远程连接Kali(默认 | 私钥)Note版

前言:xshell远程连接&#xff0c;私钥连接和常规默认连接 任务一 开启ssh服务 service ssh status //查看ssh服务状态 service ssh start //开启ssh服务 update-rc.d ssh enable //开启自启动ssh服务 任务二 修改配置文件 vi /etc/ssh/ssh_config //第一…...

网络编程(UDP编程)

思维导图 UDP基础编程&#xff08;单播&#xff09; 1.流程图 服务器&#xff1a;短信的接收方 创建套接字 (socket)-----------------------------------------》有手机指定网络信息-----------------------------------------------》有号码绑定套接字 (bind)--------------…...

Spring AI与Spring Modulith核心技术解析

Spring AI核心架构解析 Spring AI&#xff08;https://spring.io/projects/spring-ai&#xff09;作为Spring生态中的AI集成框架&#xff0c;其核心设计理念是通过模块化架构降低AI应用的开发复杂度。与Python生态中的LangChain/LlamaIndex等工具类似&#xff0c;但特别为多语…...

如何在最短时间内提升打ctf(web)的水平?

刚刚刷完2遍 bugku 的 web 题&#xff0c;前来答题。 每个人对刷题理解是不同&#xff0c;有的人是看了writeup就等于刷了&#xff0c;有的人是收藏了writeup就等于刷了&#xff0c;有的人是跟着writeup做了一遍就等于刷了&#xff0c;还有的人是独立思考做了一遍就等于刷了。…...

Python 实现 Web 静态服务器(HTTP 协议)

目录 一、在本地启动 HTTP 服务器1. Windows 下安装 node.js1&#xff09;下载安装包2&#xff09;配置环境变量3&#xff09;安装镜像4&#xff09;node.js 的常用命令 2. 安装 http-server 服务3. 使用 http-server 开启服务1&#xff09;使用 http-server2&#xff09;详解 …...

Chromium 136 编译指南 Windows篇:depot_tools 配置与源码获取(二)

引言 工欲善其事&#xff0c;必先利其器。在完成了 Visual Studio 2022 和 Windows SDK 的安装后&#xff0c;我们即将接触到 Chromium 开发生态中最核心的工具——depot_tools。这个由 Google 精心打造的工具集&#xff0c;就像是连接开发者与 Chromium 庞大代码库的智能桥梁…...

海云安高敏捷信创白盒SCAP入选《中国网络安全细分领域产品名录》

近日&#xff0c;嘶吼安全产业研究院发布《中国网络安全细分领域产品名录》&#xff0c;海云安高敏捷信创白盒&#xff08;SCAP&#xff09;成功入选软件供应链安全领域产品名录。 在数字化转型加速的今天&#xff0c;网络安全已成为企业生存与发展的核心基石&#xff0c;为了解…...

C++--string的模拟实现

一,引言 string的模拟实现是只对string对象中给的主要功能经行模拟实现&#xff0c;其目的是加强对string的底层了解&#xff0c;以便于在以后的学习或者工作中更加熟练的使用string。本文中的代码仅供参考并不唯一。 二,默认成员函数 string主要有三个成员变量&#xff0c;…...