简介

ShardingSphere-JDBC 提供官方的 Spring 命名空间,使开发者可以非常便捷的整合 ShardingSphere-JDBC 和 Spring。

使用步骤

引入 Maven 依赖

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

配置 Spring Bean

配置项说明

命名空间:http://shardingsphere.apache.org/schema/shardingsphere/datasource/datasource-5.0.0.xsd

<shardingsphere:data-source />

名称类型说明
id属性Spring Bean Id
schema-name (?)属性JDBC 数据源别名
data-source-names标签数据源名称,多个数据源以逗号分隔
rule-refs标签规则名称,多个规则以逗号分隔
mode (?)标签运行模式配置
props (?)标签属性配置,详情请参见属性配置

配置示例

  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" schema-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>

在 Spring 中使用 ShardingSphere 数据源

使用方式同 Spring Boot Starter。