背景信息
ShardingSphere-JDBC 提供了 JDBC 驱动,可以仅通过配置变更即可使用,无需改写代码。
参数解释
驱动类名称
org.apache.shardingsphere.driver.ShardingSphereDriver
URL 配置
- 以
jdbc:shardingsphere:
为前缀 - 配置文件:
xxx.yaml
,配置文件格式与 YAML 配置一致 - 配置文件加载规则:
absolutepath:
前缀表示从绝对路径中加载配置文件classpath:
前缀表示从类路径中加载配置文件
操作步骤
- 引入 Maven 依赖
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc-core</artifactId>
<version>${shardingsphere.version}</version>
</dependency>
- 使用驱动
- 使用原生驱动:
Class.forName("org.apache.shardingsphere.driver.ShardingSphereDriver");
String jdbcUrl = "jdbc:shardingsphere:classpath:config.yaml";
String sql = "SELECT i.* FROM t_order o JOIN t_order_item i ON o.order_id=i.order_id WHERE o.user_id=? AND o.order_id=?";
try (
Connection conn = DriverManager.getConnection(jdbcUrl);
PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setInt(1, 10);
ps.setInt(2, 1000);
try (ResultSet rs = preparedStatement.executeQuery()) {
while(rs.next()) {
// ...
}
}
}
- 使用数据库连接池
String driverClassName = "org.apache.shardingsphere.driver.ShardingSphereDriver";
String jdbcUrl = "jdbc:shardingsphere:classpath:config.yaml";
// 以 HikariCP 为例
HikariDataSource dataSource = new HikariDataSource();
dataSource.setDriverClassName(driverClassName);
dataSource.setJdbcUrl(jdbcUrl);
String sql = "SELECT i.* FROM t_order o JOIN t_order_item i ON o.order_id=i.order_id WHERE o.user_id=? AND o.order_id=?";
try (
Connection conn = dataSource.getConnection();
PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setInt(1, 10);
ps.setInt(2, 1000);
try (ResultSet rs = preparedStatement.executeQuery()) {
while(rs.next()) {
// ...
}
}
}
配置示例
加载 classpath 中 config.yaml 配置文件的 JDBC URL:
jdbc:shardingsphere:classpath:config.yaml
加载绝对路径中 config.yaml 配置文件的 JDBC URL:
jdbc:shardingsphere:absolutepath:/path/to/config.yaml
当前内容版权归 ShardingSphere 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 ShardingSphere .