Apache ShardingSphere 示例项目教程
1. 项目的目录结构及介绍
Apache ShardingSphere 示例项目 (shardingsphere-example
) 是一个独立的 Maven 项目,位于 Apache ShardingSphere 项目的 examples
目录下。项目的目录结构如下:
shardingsphere-example
├── example-core
│ ├── config-utility
│ ├── example-api
│ ├── example-raw-jdbc
│ ├── example-spring-jpa
│ └── example-spring-mybatis
├── shardingsphere-jdbc-example
│ ├── sharding-example
│ │ ├── sharding-raw-jdbc-example
│ │ ├── sharding-spring-boot-jpa-example
│ │ └── sharding-spring-boot-mybatis-example
│ └── ...
├── shardingsphere-proxy-example
│ └── ...
├── shardingsphere-parser-example
│ └── ...
└── ...
主要模块介绍
-
example-core: 核心示例模块,包含配置工具和不同框架的示例。
- config-utility: 配置工具模块。
- example-api: API 示例模块。
- example-raw-jdbc: 原生 JDBC 示例模块。
- example-spring-jpa: Spring JPA 示例模块。
- example-spring-mybatis: Spring MyBatis 示例模块。
-
shardingsphere-jdbc-example: ShardingSphere-JDBC 示例模块,包含分片示例。
- sharding-example: 分片示例模块。
- sharding-raw-jdbc-example: 原生 JDBC 分片示例。
- sharding-spring-boot-jpa-example: Spring Boot JPA 分片示例。
- sharding-spring-boot-mybatis-example: Spring Boot MyBatis 分片示例。
- sharding-example: 分片示例模块。
-
shardingsphere-proxy-example: ShardingSphere-Proxy 示例模块。
-
shardingsphere-parser-example: ShardingSphere-Parser 示例模块。
2. 项目的启动文件介绍
启动文件示例
以 sharding-spring-boot-mybatis-example
为例,启动文件通常位于 src/main/java
目录下,例如:
package org.apache.shardingsphere.example.sharding.spring.boot.mybatis;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ShardingSpringBootMybatisExample {
public static void main(String[] args) {
SpringApplication.run(ShardingSpringBootMybatisExample.class, args);
}
}
启动步骤
- 克隆项目: 从 GitHub 克隆项目到本地。
- 构建项目: 使用 Maven 构建项目,执行
mvn clean install
。 - 导入 IDE: 将项目导入到 IDE 中。
- 配置数据库: 准备一个可管理的数据库环境,如本地 MySQL。
- 执行启动文件: 运行启动文件中的
main
方法。
3. 项目的配置文件介绍
配置文件示例
以 sharding-spring-boot-mybatis-example
为例,配置文件通常位于 src/main/resources
目录下,例如:
spring:
shardingsphere:
datasource:
names: ds0, ds1
ds0:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://localhost:3306/ds0
username: root
password: root
ds1:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://localhost:3306/ds1
username: root
password: root
sharding:
tables:
t_order:
actual-data
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考