本文共 2159 字,大约阅读时间需要 7 分钟。
user
表,字段包括 id
(主键),name
、uid
、school
和 age
CREATE TABLE `user` ( `id` bigint(255) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, `uid` varchar(255) DEFAULT NULL, `school` varchar(255) DEFAULT NULL, `age` int(11) DEFAULT NULL, PRIMARY KEY (`id`))
在项目依赖中添加以下坐标:
org.apache.shardingsphere sharding-jdbc-spring-boot-starter 4.0.0-RC3 org.apache.shardingsphere sharding-jdbc-spring-namespace 4.0.0-RC3
# 其他配置(如服务器绑定、上下文路径等)...# 数据源配置spring.shardingsphere.datasource.names=ds-0,ds-1spring.shardingsphere.datasource.ds-0.type=com.zaxxer.hikari.HikariDataSourcespring.shardingsphere.datasource.ds-0.driver-class-name=com.mysql.jdbc.Driverspring.shardingsphere.datasource.ds-0.jdbc-url=jdbc:mysql://youraddress:3306/demo_ds_0spring.shardingsphere.datasource.ds-0.username=springspring.shardingsphere.datasource.ds-0.password=springspring.shardingsphere.datasource.ds-1.type=com.zaxxer.hikari.HikariDataSourcespring.shardingsphere.datasource.ds-1.driver-class-name=com.mysql.jdbc.Driverspring.shardingsphere.datasource.ds-1.jdbc-url=jdbc:mysql://youraddress:3306/demo_ds_1spring.shardingsphere.datasource.ds-1.username=springspring.shardingsphere.datasource.ds-1.password=spring# 分片配置spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=agespring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression=ds-$-&{age % 2}# 表配置spring.shardingsphere.sharding.tables.user.actual-data-nodes=ds-$-{0..1}.usersspring.shardingsphere.sharding.tables.user.key-generator.column=idspring.shardingsphere.sharding.tables.user.key-generator.type=SNOWFLAKEspring.shardingsphere.sharding.tables.user.key-generator.props.worker.id=123
age=1
,数据将保存在 demo_ds_1
中age=2
,数据将保存在 demo_ds_0
中age=3
,数据将保存在 demo_ds_0
中转载地址:http://tcuh.baihongyu.com/