数据库分库分表
- 垂直分表
将字段拆分出多个表
- 水平分表
将数据拆分多个表
<!-- 分库分表插件 --><dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>sharding-jdbc-spring-boot-starter</artifactId></dependency>在执行sql时,按照配置的策略,动态改变表名查找对应的数据库表进行操作
# 多个数据库水平分表配置# 数据源名称,多数据源以逗号分隔spring.shardingsphere.datasource.names=datasource1,datasource2# 数据源 datasource1spring.shardingsphere.datasource.datasource1.type=com.alibaba.druid.pool.DruidDataSourcespring.shardingsphere.datasource.datasource1.driver-class-name=com.mysql.cj.jdbc.Driverspring.shardingsphere.datasource.datasource1.url=jdbc:mysql://localhost:3306/spring?characterEncoding=utf8&useSSL=false&serverTimezone=GMT+8&rewriteBatchedStatements=truespring.shardingsphere.datasource.datasource1.username=rootspring.shardingsphere.datasource.datasource1.password=root# 数据源 datasource2spring.shardingsphere.datasource.datasource2.type=com.alibaba.druid.pool.DruidDataSourcespring.shardingsphere.datasource.datasource2.driver-class-name=com.mysql.cj.jdbc.Driverspring.shardingsphere.datasource.datasource2.url=jdbc:mysql://localhost:3306/spring1?characterEncoding=utf8&useSSL=false&serverTimezone=GMT+8&rewriteBatchedStatements=truespring.shardingsphere.datasource.datasource2.username=rootspring.shardingsphere.datasource.datasource2.password=root# 允许一个实体类对应多张表spring.main.allow-bean-definition-overriding=true# 标准分片表配置# 指定course表分布情况( 配置表在 哪个数据库 的 哪些表里 ),一共有两个数据源datasource1,datasource2,每个库中有3张表course_1,course_2,course_2spring.shardingsphere.rules.sharding.tables.course.actual-data-nodes=datasource$->{1..2}.course_$->{1..3}# 分布式序列策略配置 指定course表里的主键cid生成策略为snowflakespring.shardingsphere.rules.sharding.tables.course.key-generate-strategy.column=cid # 分布式序列列名称spring.shardingsphere.rules.sharding.tables.course.key-generate-strategy.key-generator-name=SNOWFLAKE # 分布式序列算法名称# 指定分片策略-表 约定 cid%3 取模的值对应存放的表序号spring.shardingsphere.sharding.tables.course.table-strategy.inline.sharding-column=cidspring.shardingsphere.sharding.tables.course.table-strategy.inline.algorithm-expression=course_$->{cid % 3 1}# 指定分片策略-数据库 约定 (user_id%2 1) 对应存放的数据源序号spring.shardingsphere.sharding.tables.course.database-strategy.inline.sharding-column=user_idspring.shardingsphere.sharding.tables.course.database-strategy.inline.algorithm-expression=datasource$->{user_id % 2 1}# 打开sql输出日志spring.shardingsphere.props.sql.show=true
赞 (0)