赞
踩
springboot版本:2.2.2.RELEASE
shardingsphere版本:4.0.0-RC3
数据库:demo_ds_0,demo_ds_1
表user,分别在demo_ds_0,demo_ds_1各自建一张user表
- 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`)
- )
- <dependency>
- <groupId>org.apache.shardingsphere</groupId>
- <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
- <version>4.0.0-RC3</version>
- </dependency>
-
- <!-- for spring namespace -->
- <dependency>
- <groupId>org.apache.shardingsphere</groupId>
- <artifactId>sharding-jdbc-spring-namespace</artifactId>
- <version>4.0.0-RC3</version>
- </dependency>
- debug=false
- server.port=8081
- server.servlet.context-path=
- spring.jackson.serialization.INDENT_OUTPUT=true
- spring.profiles.active=
-
- spring.shardingsphere.props.sql.show=true
- spring.shardingsphere.datasource.names=ds-0,ds-1
-
- spring.shardingsphere.datasource.ds-0.type=com.zaxxer.hikari.HikariDataSource
- spring.shardingsphere.datasource.ds-0.driver-class-name=com.mysql.jdbc.Driver
- spring.shardingsphere.datasource.ds-0.jdbc-url=jdbc:mysql://youraddress:3306/demo_ds_0
- spring.shardingsphere.datasource.ds-0.username=
- spring.shardingsphere.datasource.ds-0.password=
-
- spring.shardingsphere.datasource.ds-1.type=com.zaxxer.hikari.HikariDataSource
- spring.shardingsphere.datasource.ds-1.driver-class-name=com.mysql.jdbc.Driver
- spring.shardingsphere.datasource.ds-1.jdbc-url=jdbc:mysql://youraddress:3306/demo_ds_1
- spring.shardingsphere.datasource.ds-1.username=
- spring.shardingsphere.datasource.ds-1.password=
-
- spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=age
- spring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression=ds-$->{age % 2}
-
- spring.shardingsphere.sharding.tables.user.actual-data-nodes=ds-$->{0..1}.user
- spring.shardingsphere.sharding.tables.user.key-generator.column=id
-
- spring.shardingsphere.sharding.tables.user.key-generator.type=SNOWFLAKE
- spring.shardingsphere.sharding.tables.user.key-generator.props.worker.id=123
1.通过代码,插入一条数据,age=1,发现数据保存在demo_ds_1的user表中
2.通过代码,插入一条数据,age=2,发现数据保存在demo_ds_0的user表中
3.通过代码,插入一条数据,age=3,发现数据保存在demo_ds_0的user表中
4通过代码查询,发现查询出三条记录,到此验证完毕。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。