Overview

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

The list of compatible SpringBoot versions is as follows:

  1. SpringBoot 1.x
  2. SpringBoot 2.x
  3. SpringBoot 3.x (Experimental)

Usage

Import Maven Dependency

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

Configure Spring Boot Properties

ShardingSphere-JDBC spring boot properties consists of database name, mode configuration, data source map, rule configurations and properties.

  1. # JDBC logic database name. Through this parameter to connect ShardingSphere-JDBC and ShardingSphere-Proxy.
  2. spring.shardingsphere.database.name= # logic database name, default value: logic_db
  3. spring.shardingsphere.mode.xxx= # mode configuration
  4. spring.shardingsphere.dataSource.xxx= # data source map
  5. spring.shardingsphere.rules.xxx= # rule configurations
  6. spring.shardingsphere.props= # properties

Please refer to Mode Confiugration for more mode details.

Please refer to Data Source Confiugration for more data source details.

Please refer to Rules Confiugration for more rule details.

Use Data Source

Developer can inject to use native JDBC or ORM frameworks such as JPA, Hibernate or MyBatis through the DataSource.

Take native JDBC usage as an example:

  1. @Resource
  2. private DataSource dataSource;