一、前言
在前面我们通过以下章节对ShardingSphere
的AutoTable
有了基础的了解:
Spring Boot集成ShardingSphere分片利器 AutoTable (一)—— 简单体验 | Spring Cloud 45
书接上回,本章进行对AutoTable
支持的自动分片算法进行逐一示例说明。
二、示例搭建
示例采用
springboot
集成shardingsphere-jdbc
方式搭建。
2.1 项目总体结构
2.2 Maven 依赖
shading-sphere/shading-auto-tables-algorithm/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">
<parent>
<artifactId>shading-sphere</artifactId>
<groupId>com.gm</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>shading-algorithm</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
<version>5.2.1</version>
</dependency>
<!-- 解决Mybatis中LocalDateTime和SQL中datetime的交互 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-typehandlers-jsr310</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3.1</version>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.33</version>
</dependency>
</dependencies>
</project>
-
shardingsphere-jdbc-core-spring-boot-starter
使用版本5.2.1
-
JDBC
的ORM
框架选用mybatis-plus
2.3 配置文件
server:
port: 8844
spring:
application:
name: @artifactId@
shardingsphere:
# 数据源配置
datasource:
names: ds1,ds2
ds1:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.0.35:3306/db1?useUnicode=true&characterEncoding=UTF-8&rewriteBatchedStatements=true&allowMultiQueries=true&serverTimezone=Asia/Shanghai
username: root
password: '1qaz@WSX'
ds2:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.0.46:3306/db2?useUnicode=true&characterEncoding=UTF-8&rewriteBatchedStatements=true&allowMultiQueries=true&serverTimezone=Asia/Shanghai
username: root
password: '1qaz@WSX'
# 定义规则
rules:
sharding:
# 采用自动分片算法
autoTables:
以上配置为各自动分片算法公用的基础配置。
三、自动分片算法
AutoTable
支持全部的自动分片算法,包括:
MOD
:取模分片算法HASH_MOD
:哈希取模分片算法VOLUME_RANGE
:基于分片容量的范围分片算法BOUNDARY_RANGE
:基于分片边界的范围分片算法AUTO_INTERVAL
:自动时间段分片算法
3.1 取模 —— MOD
类似 MySQL
的 PATITION
里的 MOD
,由ShardingSphere
通过分片数和取模的分母,自动计算实际表个数。
可配置属性:
属性名称 | 数据类型 | 说明 |
---|---|---|
sharding-count | int | 分片数量 |
3.1.1 示例
该示例实际需要2
个数据源(也支持单数据源的场景),每个数据源中各有3
个t_auto_order_mod
分表,分表格式为:t_auto_order_mod_${0..5}
3.1.1.1 自动分片算法配置
# 采用自动分片算法
autoTables:
# 取模
t_auto_order_mod:
actualDataSources: ds$->{1..2}
sharding-strategy:
standard:
sharding-column: order_id
sharding-algorithm-name: auto_order_mod
# 分布式序列策略
key-generate-strategy:
# 自增列名称,缺省表示不使用自增主键生成器
column: order_id
# 分布式序列算法名称
key-generator-name: snowflake
# 分片算法配置
sharding-algorithms:
# 取模
auto_order_mod:
type: MOD
props:
sharding-count: 6
3.1.1.2 实体类
com/gm/shading/auto/tables/algorithm/entity/AutoOrderMod.java
:
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.math.BigDecimal;
@Data
@TableName("t_auto_order_mod")
public class AutoOrderMod {
@TableId(type = IdType.ASSIGN_ID)
private Long orderId;
private BigDecimal price;
private Long userId;
private String status;
}
3.1.1.3 Mapper
com/gm/shading/auto/tables/algorithm/mapper/AutoOrderModMapper.java
:
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gm.shading.auto.tables.algorithm.entity.AutoOrderMod;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface AutoOrderModMapper extends BaseMapper<AutoOrderMod> {
void save(AutoOrderMod autoOrder);
}
src/main/resources/mapper/AutoOrderModMapper.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.gm.shading.auto.tables.algorithm.mapper.AutoOrderModMapper">
<insert id="save" parameterType="com.gm.shading.auto.tables.algorithm.entity.AutoOrderMod">
insert into t_auto_order_mod (price, user_id, status)
values (#{price}, #{userId}, #{status})
</insert>
</mapper>
3.1.1.4 单元测试
com/gm/shading/auto/tables/algorithm/ShadingAutoTablesAlgorithmApplicationTests.java
:
@Autowired
AutoOrderModMapper autoOrderModMapper;
@Autowired
JdbcTemplate jdbcTemplate;
@Test
public void testCreateAutoOrderMod() {
jdbcTemplate.execute("CREATE TABLE `t_auto_order_mod` (\n" +
" `order_id` bigint(20) NOT NULL COMMENT '订单id',\n" +
" `price` decimal(10,2) NOT NULL COMMENT '订单价格',\n" +
" `user_id` bigint(20) NOT NULL COMMENT '下单用户id',\n" +
" `status` varchar(50) NOT NULL COMMENT '订单状态',\n" +
" PRIMARY KEY (`order_id`) USING BTREE\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;");
}
@Test
public void InsertAutoOrderMod() {
Random random = new Random();
for (int i = 1; i < 10; i++) {
AutoOrderMod order = new AutoOrderMod();
order.setPrice(new BigDecimal(i));
order.setUserId(Integer.valueOf(random.nextInt(25)).longValue());
order.setStatus(i + "");
autoOrderModMapper.save(order);
}
}
3.2 散列取模 —— HASH_MOD
MOD
取模通常数值类型的分片,而HASH_MOD
不仅适用于数值,也适用于字符(通过hash值取模)
列进行分片。该算法基本和MOD
类似。
可配置属性:
属性名称 | 数据类型 | 说明 |
---|---|---|
sharding-count | int | 分片数量 |
3.2.1 示例
该案例只有一个数据源,数据源中有 6
个t_auto_order_hash_mod
分表,按 order_id
的hash
值进行取模计算得到实际表。分表格式为:t_auto_order_hash_mod_${0..5}
3.2.1.1 自动分片算法配置
# 采用自动分片算法
autoTables:
# 散列取模
t_auto_order_hash_mod:
actualDataSources: ds1
sharding-strategy:
standard:
sharding-column: order_id
sharding-algorithm-name: auto_order_hash_mod
# 分布式序列策略
key-generate-strategy:
# 自增列名称,缺省表示不使用自增主键生成器
column: order_id
# 分布式序列算法名称
key-generator-name: snowflake
# 分片算法配置
sharding-algorithms:
# 散列取模
auto_order_hash_mod:
type: HASH_MOD
props:
sharding-count: 6
3.2.1.2 实体类
com/gm/shading/auto/tables/algorithm/entity/AutoOrderHashMod.java
:
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.math.BigDecimal;
@Data
@TableName("t_auto_order_hash_mod")
public class AutoOrderHashMod {
@TableId(type = IdType.ASSIGN_ID)
private Long orderId;
private BigDecimal price;
private Long userId;
private String status;
}
3.2.1.3 Mapper
com/gm/shading/auto/tables/algorithm/mapper/AutoOrderHashModMapper.java
:
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gm.shading.auto.tables.algorithm.entity.AutoOrderHashMod;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface AutoOrderHashModMapper extends BaseMapper<AutoOrderHashMod> {
void save(AutoOrderHashMod autoOrder);
}
src/main/resources/mapper/AutoOrderHashModMapper.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.gm.shading.auto.tables.algorithm.mapper.AutoOrderHashModMapper">
<insert id="save" parameterType="com.gm.shading.auto.tables.algorithm.entity.AutoOrderHashMod">
insert into t_auto_order_hash_mod (price, user_id, status)
values (#{price}, #{userId}, #{status})
</insert>
</mapper>
3.2.1.4 单元测试
com/gm/shading/auto/tables/algorithm/ShadingAutoTablesAlgorithmApplicationTests.java
:
@Autowired
AutoOrderHashModMapper autoOrderHashModMapper;
@Autowired
JdbcTemplate jdbcTemplate;
@Test
public void testCreateAutoOrderHashMod() {
jdbcTemplate.execute("CREATE TABLE `t_auto_order_hash_mod` (\n" +
" `order_id` bigint(20) NOT NULL COMMENT '订单id',\n" +
" `price` decimal(10,2) NOT NULL COMMENT '订单价格',\n" +
" `user_id` bigint(20) NOT NULL COMMENT '下单用户id',\n" +
" `status` varchar(50) NOT NULL COMMENT '订单状态',\n" +
" PRIMARY KEY (`order_id`) USING BTREE\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;");
}
@Test
public void InsertAutoOrderHashMod() {
Random random = new Random();
for (int i = 1; i < 10; i++) {
AutoOrderHashMod order = new AutoOrderHashMod();
order.setPrice(new BigDecimal(i));
order.setUserId(Integer.valueOf(random.nextInt(25)).longValue());
order.setStatus(i + "");
autoOrderHashModMapper.save(order);
}
}
3.3 容量范围 —— VOLUME_RANGE
该算法会根据 (${range-upper} - ${range-lower}) / ${sharding-volume} + 2
来算出实际的分片数量,从而实现自动分片功能。
可配置属性:
属性名称 | 数据类型 | 说明 |
---|---|---|
range-lower | long | 范围下界,超过边界的数据会报错 |
range-upper | long | 范围上界,超过边界的数据会报错 |
sharding-volume | long | 分片容量 |
要注意,这个算法之所以 + 2
,实际上包含了两个隐藏表:
- 储存小于
range-lower
情况的数据 - 储存大于
range-upper
情况的数据
3.3.1 示例
该示例实际需要2
个数据源,每个数据源中各有 2
个t_auto_order_volume_range
分表,按 price
的容量范围计算得到实际表。分表格式为:t_auto_order_volume_range_${0..3}
3.3.1.1 自动分片算法配置
# 采用自动分片算法
autoTables:
# 容量范围
t_auto_order_volume_range:
actualDataSources: ds$->{1..2}
sharding-strategy:
standard:
sharding-column: price
sharding-algorithm-name: auto_order_volume_range
# 分布式序列策略
key-generate-strategy:
# 自增列名称,缺省表示不使用自增主键生成器
column: order_id
# 分布式序列算法名称
key-generator-name: snowflake
# 分片算法配置
sharding-algorithms:
# 容量范围
auto_order_volume_range:
type: VOLUME_RANGE
props:
range-lower: 0
range-upper: 20000
sharding-volume: 10000
也就是说,上面的例子包含((20000 - 0) / 10000 + 2) = 4
个分片:
db1:
- t_auto_order_volume_range_0: (-∞..0)
- t_auto_order_volume_range_2: [10000..20000)
db2:
- t_auto_order_volume_range_1: [0..10000)
- t_auto_order_volume_range_3: [20000..+∞)
3.3.1.2 实体类
com/gm/shading/auto/tables/algorithm/entity/AutoOrderVolumeRange.java
:
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.math.BigDecimal;
@Data
@TableName("t_auto_order_volume_range")
public class AutoOrderVolumeRange {
@TableId(type = IdType.ASSIGN_ID)
private Long orderId;
private BigDecimal price;
private Long userId;
private String status;
}
3.3.1.3 Mapper
com/gm/shading/auto/tables/algorithm/mapper/AutoOrderVolumeRangeMapper.java
:
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gm.shading.auto.tables.algorithm.entity.AutoOrderVolumeRange;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface AutoOrderVolumeRangeMapper extends BaseMapper<AutoOrderVolumeRange> {
void save(AutoOrderVolumeRange autoOrder);
}
src/main/resources/mapper/AutoOrderVolumeRangeMapper.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.gm.shading.auto.tables.algorithm.mapper.AutoOrderVolumeRangeMapper">
<insert id="save" parameterType="com.gm.shading.auto.tables.algorithm.entity.AutoOrderVolumeRange">
insert into t_auto_order_volume_range (price, user_id, status)
values (#{price}, #{userId}, #{status})
</insert>
</mapper>
3.3.1.4 单元测试
com/gm/shading/auto/tables/algorithm/ShadingAutoTablesAlgorithmApplicationTests.java
:
@Autowired
AutoOrderVolumeRangeMapper autoOrderVolumeRangeMapper;
@Autowired
JdbcTemplate jdbcTemplate;
@Test
public void testCreateAutoOrderVolumeRange() {
jdbcTemplate.execute("CREATE TABLE `t_auto_order_volume_range` (\n" +
" `order_id` bigint(20) NOT NULL COMMENT '订单id',\n" +
" `price` decimal(10,2) NOT NULL COMMENT '订单价格',\n" +
" `user_id` bigint(20) NOT NULL COMMENT '下单用户id',\n" +
" `status` varchar(50) NOT NULL COMMENT '订单状态',\n" +
" PRIMARY KEY (`order_id`) USING BTREE\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;");
}
@Test
public void InsertAutoOrderVolumeRange() {
Random random = new Random();
for (int i = 1; i < 10; i++) {
AutoOrderVolumeRange order = new AutoOrderVolumeRange();
order.setPrice(new BigDecimal(random.nextInt(20000)));
order.setUserId(Integer.valueOf(random.nextInt(25)).longValue());
order.setStatus(i + "");
autoOrderVolumeRangeMapper.save(order);
}
}
3.4 边界范围 —— BOUNDARY_RANGE
该算法会根据自定义范围进行分片,如:0 ~ 100在分片0,100 ~ 1000在分片1,1000 ~ 1500在分片2 等。
可配置属性:
属性名称 | 数据类型 | 说明 |
---|---|---|
sharding-ranges | String | 分片范围,多个边界可以用 , 隔开 |
要注意实际上包含了两个隐藏表:
- 储存小于自定义范围最小值情况的数据
- 储存大于自定义范围最大值情况的数据
3.4.1 示例
该示例实际需要2
个数据源,每个数据源中各有 3
个t_auto_order_boundary_range
分表,并且按 price
的自定义范围计算得到实际表。分表格式为:t_auto_order_boundary_range_${0..5}
3.4.1.1 自动分片算法配置
# 采用自动分片算法
autoTables:
# 边界范围
t_auto_order_boundary_range:
actualDataSources: ds$->{1..2}
sharding-strategy:
standard:
sharding-column: price
sharding-algorithm-name: auto_order_boundary_range
# 分布式序列策略
key-generate-strategy:
# 自增列名称,缺省表示不使用自增主键生成器
column: order_id
# 分布式序列算法名称
key-generator-name: snowflake
# 分片算法配置
sharding-algorithms:
# 边界范围
auto_order_boundary_range:
type: BOUNDARY_RANGE
props:
sharding-ranges: 10,15,100,12000,16000
此示例中的自定义范围可以拆分为:
-∞..10,
10..15,
15..100,
100..12000,
12000..16000,
16000..+∞
3.4.1.2 实体类
com/gm/shading/auto/tables/algorithm/entity/AutoOrderBoundaryRange.java
:
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.math.BigDecimal;
@Data
@TableName("t_auto_order_boundary_range")
public class AutoOrderBoundaryRange {
@TableId(type = IdType.ASSIGN_ID)
private Long orderId;
private BigDecimal price;
private Long userId;
private String status;
}
3.4.1.3 Mapper
com/gm/shading/auto/tables/algorithm/mapper/AutoOrderBoundaryRangeMapper.java
:
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gm.shading.auto.tables.algorithm.entity.AutoOrderBoundaryRange;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface AutoOrderBoundaryRangeMapper extends BaseMapper<AutoOrderBoundaryRange> {
void save(AutoOrderBoundaryRange autoOrder);
}
src/main/resources/mapper/AutoOrderBoundaryRangeMapper.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.gm.shading.auto.tables.algorithm.mapper.AutoOrderBoundaryRangeMapper">
<insert id="save" parameterType="com.gm.shading.auto.tables.algorithm.entity.AutoOrderBoundaryRange">
insert into t_auto_order_boundary_range (price, user_id, status)
values (#{price}, #{userId}, #{status})
</insert>
</mapper>
3.4.1.4 单元测试
com/gm/shading/auto/tables/algorithm/ShadingAutoTablesAlgorithmApplicationTests.java
:
@Autowired
AutoOrderBoundaryRangeMapper autoOrderBoundaryRangeMapper;
@Autowired
JdbcTemplate jdbcTemplate;
@Test
public void testCreateAutoOrderBoundaryRange() {
jdbcTemplate.execute("CREATE TABLE `t_auto_order_boundary_range` (\n" +
" `order_id` bigint(20) NOT NULL COMMENT '订单id',\n" +
" `price` decimal(10,2) NOT NULL COMMENT '订单价格',\n" +
" `user_id` bigint(20) NOT NULL COMMENT '下单用户id',\n" +
" `status` varchar(50) NOT NULL COMMENT '订单状态',\n" +
" PRIMARY KEY (`order_id`) USING BTREE\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;");
}
@Test
public void InsertAutoOrderBoundaryRange() {
Random random = new Random();
for (int i = 1; i < 10; i++) {
AutoOrderBoundaryRange order = new AutoOrderBoundaryRange();
order.setPrice(new BigDecimal(random.nextInt(20000)));
order.setUserId(Integer.valueOf(random.nextInt(25)).longValue());
order.setStatus(i + "");
autoOrderBoundaryRangeMapper.save(order);
}
}
3.5 自动日期间隔 —— AUTO_INTERVAL
日期间隔将按:时间开始边界 到 时间结束边界 之间的秒数 除以 分片秒数 再加 2
,从而计算出总分片数。也就是 ({datetime-upper} - {datetime-lower}) / sharding-seconds + 2
。
可配置属性:
属性名称 | 数据类型 | 说明 |
---|---|---|
datetime-lower | String | 分片的起始时间范围,时间戳格式:yyyy-MM-dd HH:mm:ss |
datetime-upper | String | 分片的结束时间范围,时间戳格式:yyyy-MM-dd HH:mm:ss |
sharding-seconds | long | 单一分片所能承载的最大时间,单位:秒,允许分片键的时间戳格式的秒带有时间精度,但秒后的时间精度会被自动抹去 |
要注意,这个算法之所以 + 2
,实际上包含了两个隐藏表:
- 储存小于
datetime-lower
情况的数据 - 储存大于
datetime-upper
情况的数据
3.5.1 示例
该示例实际需要2
个数据源,共 5
个t_auto_order_auto_interval
分表,按 create_time
计算分片所能承载的最大时间得到实际表。分表格式为:t_auto_order_auto_interval_${0..4}
3.5.1.1 自动分片算法配置
# 采用自动分片算法
autoTables:
# 自动日期间隔
t_auto_order_auto_interval:
actualDataSources: ds$->{1..2}
sharding-strategy:
standard:
sharding-column: create_time
sharding-algorithm-name: auto_order_auto_interval
# 分布式序列策略
key-generate-strategy:
# 自增列名称,缺省表示不使用自增主键生成器
column: order_id
# 分布式序列算法名称
key-generator-name: snowflake
# 分片算法配置
sharding-algorithms:
# 自动日期间隔
auto_order_auto_interval:
type: AUTO_INTERVAL
props:
datetime-lower: 2023-05-07 00:00:00
datetime-upper: 2023-05-10 00:00:00
sharding-seconds: 86400
此示例中会按解析为:
?, 2023-05-07 00:00:00 = 0
2023-05-07 00:00:00, 2023-05-08 00:00:00 = 1
2023-05-08 00:00:00, 2023-05-09 00:00:00 = 2
2023-05-09 00:00:00, 2023-05-10 00:00:00 = 3
2023-05-10 00:00:00, ? = 4
3.5.1.2 实体类
com/gm/shading/auto/tables/algorithm/entity/AutoOrderBoundaryRange.java
:
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.math.BigDecimal;
import java.time.LocalDateTime;
@Data
@TableName("t_auto_order_auto_interval")
public class AutoOrderAutoInterval {
@TableId(type = IdType.ASSIGN_ID)
private Long orderId;
private BigDecimal price;
private Long userId;
private String status;
private LocalDateTime createTime;
}
3.5.1.3 Mapper
com/gm/shading/auto/tables/algorithm/mapper/AutoOrderAutoIntervalMapper.java
:
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gm.shading.auto.tables.algorithm.entity.AutoOrderAutoInterval;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface AutoOrderAutoIntervalMapper extends BaseMapper<AutoOrderAutoInterval> {
void save(AutoOrderAutoInterval autoOrder);
}
src/main/resources/mapper/AutoOrderAutoIntervalMapper.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.gm.shading.auto.tables.algorithm.mapper.AutoOrderAutoIntervalMapper">
<insert id="save" parameterType="com.gm.shading.auto.tables.algorithm.entity.AutoOrderAutoInterval">
insert into t_auto_order_auto_interval (price, user_id, status,create_time)
values (#{price}, #{userId}, #{status}, #{createTime})
</insert>
</mapper>
3.5.1.4 单元测试
com/gm/shading/auto/tables/algorithm/ShadingAutoTablesAlgorithmApplicationTests.java
:
@Autowired
AutoOrderAutoIntervalMapper autoOrderAutoIntervalMapper;
@Autowired
JdbcTemplate jdbcTemplate;
@Test
public void testCreateAutoOrderAutoInterval() {
jdbcTemplate.execute("CREATE TABLE `t_auto_order_auto_interval` (\n" +
" `order_id` bigint(20) NOT NULL COMMENT '订单id',\n" +
" `price` decimal(10,2) NOT NULL COMMENT '订单价格',\n" +
" `user_id` bigint(20) NOT NULL COMMENT '下单用户id',\n" +
" `status` varchar(50) NOT NULL COMMENT '订单状态',\n" +
" `create_time` datetime NOT NULL COMMENT '订单时间',\n" +
" PRIMARY KEY (`order_id`) USING BTREE\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;");
}
@Test
public void InsertAutoOrderAutoInterval() {
Random random = new Random();
for (int i = 1; i < 10; i++) {
AutoOrderAutoInterval order = new AutoOrderAutoInterval();
order.setPrice(new BigDecimal(random.nextInt(20000)));
order.setUserId(Integer.valueOf(random.nextInt(25)).longValue());
order.setStatus(i + "");
// 随机减少一定天数再随机增加一定天数
order.setCreateTime(LocalDateTime.now().minusDays(random.nextInt(10)).plusDays(random.nextInt(10)));
autoOrderAutoIntervalMapper.save(order);
}
}
3.6 源码
上述示例完整源码请见:https://gitee.com/gm19900510/springboot-cloud-example.git