Overview

ShardingSphere provides a JDBC driver. To use ShardingSphere, developers can configure ShardingSphereDriver in Spring.

Operation

Import Maven Dependency

  1. <dependency>
  2. <groupId>org.apache.shardingsphere</groupId>
  3. <artifactId>shardingsphere-jdbc</artifactId>
  4. <version>${shardingsphere.version}</version>
  5. </dependency>

Configure Spring Bean

Configuration Item Explanation

NameTypeDescription
driverClassAttributeDatabase Driver, need to use ShardingSphereDriver
urlAttributeYAML configuration file path

Example

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://www.springframework.org/schema/beans
  4. http://www.springframework.org/schema/beans/spring-beans.xsd">
  5. <bean id="shardingDataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
  6. <property name="driverClass" value="org.apache.shardingsphere.driver.ShardingSphereDriver" />
  7. <property name="url" value="jdbc:shardingsphere:classpath:xxx.yaml" />
  8. </bean>
  9. </beans>

Use Data Source

Same with Spring Boot.