ShardingSphere学习:06

实战代码后续上传

代码同上篇一致,只是配置文件不同。我们主要看一下,配置文件。

分表配置

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

1#配置数据源 2spring.shardingsphere.datasource.names=test0 3#test0 4spring.shardingsphere.datasource.test0.type=com.zaxxer.hikari.HikariDataSource 5spring.shardingsphere.datasource.test0.driver-class-name=com.mysql.cj.jdbc.Driver 6spring.shardingsphere.datasource.test0.jdbcUrl=jdbc:mysql://127.0.0.1:3306/test0 7spring.shardingsphere.datasource.test0.username=devadmin 8spring.shardingsphere.datasource.test0.password=

设置绑定表

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

设置分表的策略

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

1# health_record 2spring.shardingsphere.sharding.tables.health_record.actual-data-nodes=test0.health_record$->{0..1} 3spring.shardingsphere.sharding.tables.health_record.table-strategy.inline.algorithm-expression=health_record$->{record_id % 2} 4spring.shardingsphere.sharding.tables.health_record.table-strategy.inline.sharding-column=record_id 5spring.shardingsphere.sharding.tables.health_record.key-generator.column=record_id 6spring.shardingsphere.sharding.tables.health_record.key-generator.type=SNOWFLAKE 7# health_task 8spring.shardingsphere.sharding.tables.health_task.actual-data-nodes=test0.health_task$->{0..1} 9spring.shardingsphere.sharding.tables.health_task.table-strategy.inline.algorithm-expression=health_task$->{record_id %2} 10spring.shardingsphere.sharding.tables.health_task.table-strategy.inline.sharding-column=record_id 11spring.shardingsphere.sharding.tables.health_task.key-generator.column=task_id 12spring.shardingsphere.sharding.tables.health_task.key-generator.type=SNOWFLAKE

完整配置

1server.port=8080 2#打印sql 3spring.shardingsphere.props.sql.show=true 4#配置数据源 5spring.shardingsphere.datasource.names=test0 6#test0 7spring.shardingsphere.datasource.test0.type=com.zaxxer.hikari.HikariDataSource 8spring.shardingsphere.datasource.test0.driver-class-name=com.mysql.cj.jdbc.Driver 9spring.shardingsphere.datasource.test0.jdbcUrl=jdbc:mysql://127.0.0.1:3306/test0 10spring.shardingsphere.datasource.test0.username=devadmin 11spring.shardingsphere.datasource.test0.password= 12 13 14# 设置绑定表 15spring.shardingsphere.sharding.binding-tables[0]=health_record,health_task 16# health_record 17spring.shardingsphere.sharding.tables.health_record.actual-data-nodes=test0.health_record$->{0..1} 18spring.shardingsphere.sharding.tables.health_record.table-strategy.inline.algorithm-expression=health_record$->{record_id % 2} 19spring.shardingsphere.sharding.tables.health_record.table-strategy.inline.sharding-column=record_id 20spring.shardingsphere.sharding.tables.health_record.key-generator.column=record_id 21spring.shardingsphere.sharding.tables.health_record.key-generator.type=SNOWFLAKE 22# health_task 23spring.shardingsphere.sharding.tables.health_task.actual-data-nodes=test0.health_task$->{0..1} 24spring.shardingsphere.sharding.tables.health_task.table-strategy.inline.algorithm-expression=health_task$->{record_id %2} 25spring.shardingsphere.sharding.tables.health_task.table-strategy.inline.sharding-column=record_id 26spring.shardingsphere.sharding.tables.health_task.key-generator.column=task_id 27spring.shardingsphere.sharding.tables.health_task.key-generator.type=SNOWFLAKE

数据库如下

数据库结构

health_level 数据如下

user数据如下

health_record0,health_record1

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

health_task0,health_task1

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

点赞
收藏

评论区

加载中...

相关推荐

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

Nginx 调整文件上传大小限制

使用3A服务器做了网页,感觉挺不错的,使用LNMP环境用Nginx部署了前端,发现上传附件大一点就会报错,查看配置文件,发现spring的附件配置已经配置了。那么就看下Nginx的body设置。nginx文件上传默认是1MB。在server

springcloud vue 微服务分布式 activiti工作流 前后分离 集成代码生成器 shiro权限

1.代码生成器:\正反双向\(单表、主表、明细表、树形表,快速开发利器)freemaker模版技术,0个代码不用写,生成完整的一个模块,带页面、建表sql脚本、处理类、service等完整模块2.多数据源:(支持同时连接无数个数据库,可以不同的模块连接不同数的据库)支持N个数据源3.阿里数据库连接池dru

java 微服务分布式 springcloud vue.js flowable 流程引擎

1.代码生成器:\正反双向\(单表、主表、明细表、树形表,快速开发利器)freemaker模版技术,0个代码不用写,生成完整的一个模块,带页面、建表sql脚本、处理类、service等完整模块2.多数据源:(支持同时连接无数个数据库,可以不同的模块连接不同数的据库)支持N个数据源3.阿里数据库连接池dru

java代码生成工具之autoCode部署

autoCode介绍autoCode是一个代码生成工具。基于velocity模板引擎,采用SpringMVCmybatisFDUImysql此工具只负责生成代码文件,不会生成完整功能的应用程序.其特点主要有用户登陆每个用户有他自己独立的数据库连接配置和模板配置;数据源配置

Spring的JNDI数据源连接池配置示例及Spring对JNDI实现分析

在使用Tomcat服务器SpringFramework进行JavaEE项目的开发部署的时候可以在Tomcat的配置文件中进行JDBC数据源的配置,具体步骤如下(这里省略了工程的建立步骤):1)添加如下代码到tomcat的conf目录下的server.xml中:Xml代码 收藏代码<Context