分库分表是一种数据库管理方法,它的目的是提高数据库的性能、扩展性和可用性,无论是分库还是分表,都是从水平与垂直两个维度进行切分。
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.能不分就不分,根据业务走,
根据规则走