Overview

ShardingSphere-JDBC provides official Spring Namespace to make convenient for developers to integrate ShardingSphere-JDBC and Spring.

Usage

Import Maven Dependency

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

Configure Spring Bean

Configuration Item Explanation

Namespace: http://shardingsphere.apache.org/schema/shardingsphere/datasource/datasource-5.2.1.xsd

<shardingsphere:data-source />

NameTypeDescription
idAttributeSpring Bean Id
database-name (?)AttributeJDBC data source alias
data-source-namesAttributeData source name, multiple data source names are separated by commas
rule-refsAttributeRule name, multiple rule names are separated by commas
mode (?)TagMode configuration
props (?)TagProperties configuration, Please refer to Properties Configuration for more details

Example

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:shardingsphere="http://shardingsphere.apache.org/schema/shardingsphere/datasource"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://shardingsphere.apache.org/schema/shardingsphere/datasource
  7. http://shardingsphere.apache.org/schema/shardingsphere/datasource/datasource.xsd
  8. ">
  9. <shardingsphere:data-source id="ds" database-name="foo_schema" data-source-names="..." rule-refs="...">
  10. <shardingsphere:mode type="..." />
  11. <props>
  12. <prop key="xxx.xxx">${xxx.xxx}</prop>
  13. </props>
  14. </shardingsphere:data-source>
  15. </beans>

Use Data Source

Same with Spring Boot Starter.