ShardingJDBC分表怎么配置

本篇内容介绍了“Sharding JDBC分表怎么配置”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

创新互联建站是一家集网站建设,安化企业网站建设,安化品牌网站建设,网站定制,安化网站建设报价,网络营销,网络优化,安化网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

分表配置

由于的单库分表,所以只配置一个数据源就可以了

#配置数据源
spring.shardingsphere.datasource.names=test0
#test0
spring.shardingsphere.datasource.test0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.test0.driver-class-name=com.MySQL.cj.jdbc.Driver
spring.shardingsphere.datasource.test0.jdbcUrl=jdbc:mysql://127.0.0.1:3306/test0
spring.shardingsphere.datasource.test0.username=devadmin
spring.shardingsphere.datasource.test0.password=

设置绑定表

# 设置绑定表
spring.shardingsphere.sharding.binding-tables[0]=health_record,health_task

设置分表的策略

这里根据 record_id 奇偶来分表的。分为 health_record0,health_record1 和 health_task0,health_task1

# health_record
spring.shardingsphere.sharding.tables.health_record.actual-data-nodes=test0.health_record$->{0..1}
spring.shardingsphere.sharding.tables.health_record.table-strategy.inline.algorithm-expression=health_record$->{record_id % 2}
spring.shardingsphere.sharding.tables.health_record.table-strategy.inline.sharding-column=record_id
spring.shardingsphere.sharding.tables.health_record.key-generator.column=record_id
spring.shardingsphere.sharding.tables.health_record.key-generator.type=SNOWFLAKE
# health_task
spring.shardingsphere.sharding.tables.health_task.actual-data-nodes=test0.health_task$->{0..1}
spring.shardingsphere.sharding.tables.health_task.table-strategy.inline.algorithm-expression=health_task$->{record_id %2}
spring.shardingsphere.sharding.tables.health_task.table-strategy.inline.sharding-column=record_id
spring.shardingsphere.sharding.tables.health_task.key-generator.column=task_id
spring.shardingsphere.sharding.tables.health_task.key-generator.type=SNOWFLAKE

完整配置

server.port=8080
#打印sql
spring.shardingsphere.props.sql.show=true
#配置数据源
spring.shardingsphere.datasource.names=test0
#test0
spring.shardingsphere.datasource.test0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.test0.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.test0.jdbcUrl=jdbc:mysql://127.0.0.1:3306/test0
spring.shardingsphere.datasource.test0.username=devadmin
spring.shardingsphere.datasource.test0.password=


# 设置绑定表
spring.shardingsphere.sharding.binding-tables[0]=health_record,health_task
# health_record
spring.shardingsphere.sharding.tables.health_record.actual-data-nodes=test0.health_record$->{0..1}
spring.shardingsphere.sharding.tables.health_record.table-strategy.inline.algorithm-expression=health_record$->{record_id % 2}
spring.shardingsphere.sharding.tables.health_record.table-strategy.inline.sharding-column=record_id
spring.shardingsphere.sharding.tables.health_record.key-generator.column=record_id
spring.shardingsphere.sharding.tables.health_record.key-generator.type=SNOWFLAKE
# health_task
spring.shardingsphere.sharding.tables.health_task.actual-data-nodes=test0.health_task$->{0..1}
spring.shardingsphere.sharding.tables.health_task.table-strategy.inline.algorithm-expression=health_task$->{record_id %2}
spring.shardingsphere.sharding.tables.health_task.table-strategy.inline.sharding-column=record_id
spring.shardingsphere.sharding.tables.health_task.key-generator.column=task_id
spring.shardingsphere.sharding.tables.health_task.key-generator.type=SNOWFLAKE

数据库如下

Sharding JDBC分表怎么配置

health_level 数据如下

Sharding JDBC分表怎么配置

user数据如下

Sharding JDBC分表怎么配置

health_record0,health_record1

health_record0 中 record_id 都是偶数 ,health_record1 都是奇数

Sharding JDBC分表怎么配置 Sharding JDBC分表怎么配置

health_task0,health_task1

health_task0 中 record_id 都是偶数 ,health_task1 都是奇数

Sharding JDBC分表怎么配置 Sharding JDBC分表怎么配置

“Sharding JDBC分表怎么配置”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!


分享标题:ShardingJDBC分表怎么配置
URL分享:http://myzitong.com/article/jpidog.html