# sharding-jdbc
**Repository Path**: netidol/sharding-jdbc
## Basic Information
- **Project Name**: sharding-jdbc
- **Description**: Sharding-JDBC是一个轻量级的关系型数据库中间件,提供分库分表、读写分离和柔性事务等功能。
- **Primary Language**: Java
- **License**: Apache-2.0
- **Default Branch**: master
- **Homepage**: http://shardingjdbc.io/
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 974
- **Created**: 2018-01-01
- **Last Updated**: 2020-12-19
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# Sharding-JDBC - JDBC driver for shard databases and tables
# [Homepage](http://shardingjdbc.io/)
# [中文主页](http://shardingjdbc.io/index_zh.html)
[](https://travis-ci.org/dangdangdotcom/sharding-jdbc)
[](https://maven-badges.herokuapp.com/maven-central/io.shardingjdbc/sharding-jdbc)
[](https://coveralls.io/github/shardingjdbc/sharding-jdbc?branch=master)
[](https://github.com/shardingjdbc/sharding-jdbc/releases)
[](https://www.apache.org/licenses/LICENSE-2.0.html)
# Overview
Sharding-JDBC is a JDBC extension, provides distributed features such as sharding, read/write splitting, BASE transaction and database orchestration.
# Features
## 1. Sharding
* Aggregation functions, group by, order by and limit SQL supported in distributed database.
* Join (inner/outer) query supported.
* Sharding operator `=`, `BETWEEN` and `IN` supported.
* Sharding algorithm customization supported.
* Hint supported.
## 2. Read/Write Splitting
* Same transaction data concurrency guarantee.
* Hint supported.
## 3. BASE Transaction
* Best efforts delivery transaction.
* Try confirm cancel transaction (TBD).
## 6. Distributed ID Generation
* Distributed Unique Time-Sequence Generation
## 5. Compatibility
* ORM self-adapting. JPA, Hibernate, Mybatis, Spring JDBC Template or JDBC supported.
* Connection-pool self-adapting. DBCP, C3P0, BoneCP, Druid supported.
* Any Database supported theoretically. Support MySQL, Oracle, SQLServer and PostgreSQL.
## 6. Configuration
* Java config
* Spring namespace
* YAML
* Inline expression
## 7. Orchestration (new feature for 2.0)
* Configuration center, can support data sources, tables and sharding strategies switch dynamically. (2.0.0.M1)
* Smart client to orchestrate data access service, can failover automatically (2.0.0.M2)
* Output apm information based on open tracing protocol (2.0.0.M3)
# Architecture

# [Release Notes](https://github.com/shardingjdbc/sharding-jdbc/releases)
# [Roadmap](ROADMAP.md)
# Quick Start
## Add maven dependency
```xml
io.shardingjdbc
sharding-jdbc-core
${latest.release.version}
```
## Rule configuration
```java
Map dataSourceMap = new HashMap<>();
BasicDataSource dataSource1 = new BasicDataSource();
dataSource1.setDriverClassName("com.mysql.jdbc.Driver");
dataSource1.setUrl("jdbc:mysql://localhost:3306/ds_0");
dataSource1.setUsername("root");
dataSource1.setPassword("");
dataSourceMap.put("ds_0", dataSource1);
BasicDataSource dataSource2 = new BasicDataSource();
dataSource2.setDriverClassName("com.mysql.jdbc.Driver");
dataSource2.setUrl("jdbc:mysql://localhost:3306/ds_1");
dataSource2.setUsername("root");
dataSource2.setPassword("");
dataSourceMap.put("ds_1", dataSource2);
TableRuleConfiguration orderTableRuleConfig = new TableRuleConfiguration();
orderTableRuleConfig.setLogicTable("t_order");
orderTableRuleConfig.setActualDataNodes("ds_${0..1}.t_order_${[0, 1]}");
orderTableRuleConfig.setDatabaseShardingStrategyConfig(new InlineShardingStrategyConfiguration("user_id", "ds_${user_id % 2}"));
orderTableRuleConfig.setTableShardingStrategyConfig(new InlineShardingStrategyConfiguration("order_id", "t_order_${order_id % 2}"));
ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
shardingRuleConfig.getTableRuleConfigs().add(orderTableRuleConfig);
// config order_item table rule...
DataSource dataSource = ShardingDataSourceFactory.createDataSource(dataSourceMap, shardingRuleConfig);
```
Or use yaml to configure:
```yaml
dataSources:
ds_0: !!org.apache.commons.dbcp.BasicDataSource
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/ds_0
username: root
password:
ds_1: !!org.apache.commons.dbcp.BasicDataSource
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/ds_1
username: root
password:
shardingRule:
tables:
t_order:
actualDataNodes: ds_${0..1}.t_order_${0..1}
databaseStrategy:
inline:
shardingColumn: user_id
algorithmExpression: ds_${user_id % 2}
tableStrategy:
inline:
shardingColumn: order_id
algorithmExpression: t_order_${order_id % 2}
t_order_item:
actualDataNodes: ds_${0..1}.t_order_item_${0..1}
databaseStrategy:
inline:
shardingColumn: user_id
algorithmExpression: ds_${user_id % 2}
tableStrategy:
inline:
shardingColumn: order_id
algorithmExpression: t_order_item_${order_id % 2}
```
```java
DataSource dataSource = ShardingDataSourceFactory.createDataSource(yamlFile);
```
## Use raw JDBC API
```java
DataSource dataSource = ShardingDataSourceFactory.createDataSource(dataSourceMap, shardingRuleConfig);
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 preparedStatement = conn.prepareStatement(sql)) {
preparedStatement.setInt(1, 10);
preparedStatement.setInt(2, 1001);
try (ResultSet rs = preparedStatement.executeQuery()) {
while(rs.next()) {
System.out.println(rs.getInt(1));
System.out.println(rs.getInt(2));
}
}
}
```
## Use spring namespace
```xml
```