分库分表简单介绍

分库分表是一种数据库管理方法,它的目的是提高数据库的性能、扩展性和可用性,无论是分库还是分表,都是从水平与垂直两个维度进行切分。

shardingsphere 组件

Apache ShardingSphere

1.引入maven依赖

XML 复制代码
<dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
            <version>4.0.0-RC1</version>
</dependency>

2.没有用垂直,只有在业务上垂直

只用到了水平,水平拆表

cls=provide,order,user,

=order.orderMaster${0...9}

配置 针对规则配置的模式

单机模式(默认)内存模式 集群模式

配置项说明:

在application.properties中配置选项

XML 复制代码
#此配置将默认后面的bean覆盖前面的同名bean,否则会报错
spring.main.allow-bean-definition-overriding=true

#sharding-jdbc配置
##配置数据源名称
spring.shardingsphere.datasource.names=order

##配置数据源g1具体内容
spring.shardingsphere.datasource.order.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.order.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.order.url=jdbc:mysql://192.168.14.2:3306/goods_db1 useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC
spring.shardingsphere.datasource.order.username=root
spring.shardingsphere.datasource.order.password=123456

##配置公共表
spring.shardingsphere.sharding.broadcast-tables=dict
##配置公共表的主键及生成策略
spring.shardingsphere.sharding.tables.dict.key-generator.column=id
spring.shardingsphere.sharding.tables.dict.key-generator.type=SNOWFLAKE



##配置order_master数据库分布以及表分布
spring.shardingsphere.sharding.tables.order_master.actual-data-nodes=order.order_master_${0..9}


spring.shardingsphere.sharding.tables.order_master.table-strategy.inline.sharding-column=id
spring.shardingsphere.sharding.tables.order_master.table-strategy.inline.algorithm-expression=order_master_${id%10}

##配置order_item数据库分布以及表分布
spring.shardingsphere.sharding.tables.order_item.actual-data-nodes=order.order_item_${0..9}


spring.shardingsphere.sharding.tables.order_item.table-strategy.inline.sharding-column=orderId
spring.shardingsphere.sharding.tables.order_item.table-strategy.inline.algorithm-expression=order_item_${orderId%10}




##打印sql
spring.shardingsphere.props.sql.show=true

写model和mapper

java 复制代码
@Data
public class Goods {
    private Long id;
    private String name;
    private Long uid;
    private String status;
}

@Data
public class Dict {
    private Long id;
    private String name;
    private String status;
}

@Mapper
public interface GoodsMapper extends BaseMapper<Goods> {
}

@Mapper
public interface DictMapper extends BaseMapper<Dict> {
}

测试方法

java 复制代码
@Test
    void addGoods(){
        for (int i = 0; i < 10; i++) {
            Goods goods = new Goods();
            goods.setName("商品" + i);
            goods.setUid(RandomUtil.randomLong(1000L, 9999L));
            goods.setStatus("备注 " + i);
            goodsMapper.insert(goods);
        }
    }

    @Test
    void getGoods(){
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
//        wrapper.eq("id", "1714939259473723393");
        Goods goods = goodsMapper.selectById(1716273265985294340L);

        System.out.println(goods.toString());
    }

	@Test
	void addDict(){
        for (int i = 0; i < 10; i++) {
            Dict dict = new Dict();
            dict.setName("字典" + i);
            dict.setStatus("备注 " + i);
            dictMapper.insert(dict);
        }
    }

    @Test
    void deleteDict(){
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.eq("id", "1716276779037872130");
        dictMapper.delete(wrapper);
    }

3.根据id ={id%10}

加上用户的后一位

4.order.orderItem${0...9}

=orderId

={orderId%10}

5.能不分就不分,根据业务走,

根据规则走

相关推荐
TDengine (老段)1 小时前
TDengine 字符串函数 CHAR 用户手册
java·大数据·数据库·物联网·时序数据库·tdengine·涛思数据
qq7422349841 小时前
Python操作数据库之pyodbc
开发语言·数据库·python
姚远Oracle ACE2 小时前
Oracle 如何计算 AWR 报告中的 Sessions 数量
数据库·oracle
Dxy12393102162 小时前
MySQL的SUBSTRING函数详解与应用
数据库·mysql
码力引擎2 小时前
【零基础学MySQL】第十二章:DCL详解
数据库·mysql·1024程序员节
杨云龙UP2 小时前
【MySQL迁移】MySQL数据库迁移实战(利用mysqldump从Windows 5.7迁至Linux 8.0)
linux·运维·数据库·mysql·mssql
l1t2 小时前
利用DeepSeek辅助修改luadbi-duckdb读取DuckDB decimal数据类型
c语言·数据库·单元测试·lua·duckdb
安当加密3 小时前
Nacos配置安全治理:把数据库密码从YAML里请出去
数据库·安全
ColderYY3 小时前
Python连接MySQL数据库
数据库·python·mysql
GW_Cheng3 小时前
达梦数据库适配遇到的一些问题
数据库·国产化·达梦数据库