MySQL分库分表(3)SpringBoot + ShardingSphere 实现分表

tech2025-12-30  2

这篇博客通过ShardingSphere实现分表不分库

一、项目概述

1、技术架构

项目总体技术选型

SpringBoot2.0.6 + shardingsphere4.0.0-RC1 + Maven3.5.4 + MySQL + lombok(插件)

2、项目说明

场景:在实际开发中,如果表的数据过大,我们可能需要把一张表拆分成多张表,这里就是通过ShardingSphere实现分表功能,但不分库。

3、数据库设计

这里有个member库,里面的tab_user表由一张拆分成3张,分别是tab_user0、tab_user1、tab_user2。

二、核心代码

1、application.properties

server.port=8088 #指定mybatis信息 mybatis.config-location=classpath:mybatis-config.xml spring.shardingsphere.datasource.names=master # 数据源 主库 spring.shardingsphere.datasource.master.type=com.alibaba.druid.pool.DruidDataSource spring.shardingsphere.datasource.master.driver-class-name=com.mysql.jdbc.Driver spring.shardingsphere.datasource.master.url=jdbc:mysql://localhost:3306/member?characterEncoding=utf-8 spring.shardingsphere.datasource.master.username=root spring.shardingsphere.datasource.master.password=root #数据分表规则 #指定所需分的表 spring.shardingsphere.sharding.tables.tab_user.actual-data-nodes=master.tab_user$->{0..2} #指定主键 spring.shardingsphere.sharding.tables.tab_user.table-strategy.inline.sharding-column=id #分表规则为主键除以3取模 spring.shardingsphere.sharding.tables.tab_user.table-strategy.inline.algorithm-expression=tab_user$->{id % 3} #打印sql spring.shardingsphere.props.sql.show=true

Sharding-JDBC可以通过Java,YAML,Spring命名空间和Spring Boot Starter四种方式配置,开发者可根据场景选择适合的配置方式。具体可以看官网。

2、UserController

package com.oujiong.controller; import com.google.common.collect.Lists; import com.oujiong.entity.User; import com.oujiong.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.PostConstruct; import java.util.List; /** * @author xub * @Description: 接口测试 * @date 2019/10/10 下午8:52 */ @RestController public class UserController { @Autowired private UserService userService; /** * 模拟插入数据 */ List<User> userList = Lists.newArrayList(); /** * 初始化插入数据,这个注解讲的是,服务器在加载Servlet的时候只加载一次 */ @PostConstruct private void getData() { userList.add(new User(1L,"小小", "女", 3)); userList.add(new User(2L,"爸爸", "男", 30)); userList.add(new User(3L,"妈妈", "女", 28)); userList.add(new User(4L,"爷爷", "男", 64)); userList.add(new User(5L,"奶奶", "女", 62)); } /** * @Description: 批量保存用户 */ @PostMapping("save-user") public Object saveUser() { return userService.insertForeach(userList); } /** * @Description: 获取用户列表 */ @GetMapping("list-user") public Object listUser() { return userService.list(); } }

三、测试验证

1、批量插入数据

localhost:8088/save-user

我们可以从商品接口代码中可以看出,它会批量插入5条数据。我们先看控制台输出SQL语句 我们可以从SQL语句可以看出 tab_user1 和 tab_user2 表插入了两条数据,而 tab_user0 表中插入一条数据。

我们再来看数据库

tab_user0: tab_user1 tab_user2 完成分表插入数据。

2、获取数据

我们来获取列表的SQL,这里对SQL做了order排序操作

select * from tab_user order by id

请求接口结果

注意:单独执行SQL是错误的!要在系统中执行

我们可以看出虽然已经分表,但依然可以将多表数据聚合在一起并可以排序。

最新回复(0)