1.版本
springboot:3.0.2
shardingsphere:5.4.1
2.shardingsphere官网
https://shardingsphere.apache.org/
中文文档
https://shardingsphere.apache.org/document/current/cn/overview/
3.建springboot3项目
建普通springboot3项目,包括mybatis-plus和数据库表
4.集成shardingsphere5
pom.xml:
...
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc-core</artifactId>
<version>5.4.1</version>
</dependency>
...
application.properties:
...
# 配置 DataSource Driver
spring.datasource.driver-class-name=org.apache.shardingsphere.driver.ShardingSphereDriver
# 指定 YAML 配置文件
spring.datasource.url=jdbc:shardingsphere:classpath:shardingsphereconf.yaml
shardingsphereconf.yaml:
# JDBC 逻辑库名称。在集群模式中,使用该参数来联通 ShardingSphere-JDBC 与 ShardingSphere-Proxy。
# 默认值:logic_db
databaseName:
shardingSphereTest
mode:
type: Standalone
repository:
type: JDBC
dataSources:
ds_1:
dataSourceClassName: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.cj.jdbc.Driver
jdbcUrl: jdbc:mysql://XXXX:3306/shardingSphereTest?useUnicode=true&autoReconnect=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai
username: root
password: XXXX
rules:
- !SINGLE
tables:
- ds_1.* # 加载指定数据源中的全部单表
defaultDataSource: ds_1
5.模式配置
模式配置为单机模式,即mode设置为Standalone
6.报错
javax.xml.bind.JAXBException: Implementation of JAXB-API has not been found on module path or classpath.
原因:jaxb版本不支持jdk17
解决办法:
pom.xml:
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.2</version>
</dependency>
7.参考代码
链接: https://pan.baidu.com/s/1pQ1MVfeO0RyLZ98B-f6hyg?pwd=1111
提取码:1111
需要用init.sql中语句建表,更换数据库链接和账号密码