【教程】shardingjdbc (二) 开始使用

本文以SpringBoot为背景,介绍了如何集成Shardingjdbc进行数据库分片。首先,引入Shardingjdbc starter依赖和相关数据库驱动。接着,配置ShardingSphereDataSource管理数据库连接池,而非传统的spring.datasource。接着,创建实体类和对应的数据库表,配置多个数据源。最后,定义分片规则,包括分表策略、分库策略和分片键,完成测试,观察数据是否按预期分布到不同库的表中。
摘要由CSDN通过智能技术生成

以springboot 来开始使用shardingjdbc

  1. 引入shardingjdbc starter依赖

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

其他的比如数据库连接驱动,mybaytis,等引入,完整pom如下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>shardingjdbcDemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>shardingjdbcDemo</name>

    <description>Demo project for shardingJdbc</description>

    <properties>
        <java.version>1.8</java.version>
        <shardingsphere.version>5.0.0</shardingsphere.version>
        <hikaricp.version>3.4.5</hikaricp.version>
        <mybatisplus-version>3.4.3.1</mybatisplus-version>
        <mysql-connector-version>8.0.27</mysql-connector-version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
          
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sharding-JDBC 是一款基于 Java 的开源分布式数据库中间件,提供了分库分表、读写分离、柔性事务等功能。下面是 Sharding-JDBC使用步骤: 1. 引入依赖 在项目的 pom.xml 文件中,引入 Sharding-JDBC 的相关依赖: ```xml <dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>sharding-jdbc-core</artifactId> <version>${sharding-jdbc.version}</version> </dependency> ``` 其中,`${sharding-jdbc.version}` 是 Sharding-JDBC 的版本号。 2. 配置数据源 在项目中配置数据源,并将数据源的信息添加到 Sharding-JDBC 的配置中。Sharding-JDBC 支持多种数据源,包括 JDBC、Spring、MyBatis 等。以 JDBC 数据源为例,配置文件如下: ```yaml spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true username: root password: root sharding: jdbc: datasource: ds0: url: jdbc:mysql://localhost:3306/ds0?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true username: root password: root ds1: url: jdbc:mysql://localhost:3306/ds1?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true username: root password: root config: sharding: tables: user: actualDataNodes: ds$->{0..1}.user$->{0..2} tableStrategy: standard: shardingColumn: id shardingAlgorithmName: userShardingAlgorithm keyGenerateStrategy: column: id keyGeneratorName: snowflake shardingAlgorithms: userShardingAlgorithm: type: INLINE props: algorithm-expression: user$->{id % 2} keyGenerators: snowflake: type: SNOWFLAKE props: worker-id: 123 ``` 其中,`spring.datasource` 是项目中的数据源配置,`sharding.jdbc.datasource` 是 Sharding-JDBC 的数据源配置。在 `sharding.jdbc.datasource` 中,`ds0` 和 `ds1` 分别代表两个数据源,`actualDataNodes` 表示表的真实数据节点,`tableStrategy` 表示分表策略,`keyGenerateStrategy` 表示主键生成策略,`shardingAlgorithms` 表示分片算法,`keyGenerators` 表示主键生成器。 3. 配置 Sharding-JDBC 在项目中配置 Sharding-JDBC,并将数据源的信息添加到 Sharding-JDBC 的配置中。配置文件如下: ```yaml spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true username: root password: root sharding: jdbc: datasource: ds0: url: jdbc:mysql://localhost:3306/ds0?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true username: root password: root ds1: url: jdbc:mysql://localhost:3306/ds1?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true username: root password: root config: sharding: tables: user: actualDataNodes: ds$->{0..1}.user$->{0..2} tableStrategy: standard: shardingColumn: id shardingAlgorithmName: userShardingAlgorithm keyGenerateStrategy: column: id keyGeneratorName: snowflake shardingAlgorithms: userShardingAlgorithm: type: INLINE props: algorithm-expression: user$->{id % 2} keyGenerators: snowflake: type: SNOWFLAKE props: worker-id: 123 ``` 其中,`spring.datasource` 是项目中的数据源配置,`sharding.jdbc.datasource` 是 Sharding-JDBC 的数据源配置。在 `sharding.jdbc.config` 中,`sharding` 表示 Sharding-JDBC 的配置,`tables` 表示分片表的配置,`shardingAlgorithms` 表示分片算法的配置,`keyGenerators` 表示主键生成器的配置。 4. 使用 Sharding-JDBC 在项目中使用 Sharding-JDBC,只需要按照普通的 JDBC 使用方式即可。Sharding-JDBC 会自动根据配置将 SQL 语句分片执行,并将结果合并返回。例如: ```java public class UserService { @Autowired private JdbcTemplate jdbcTemplate; public List<User> listUsers() { String sql = "SELECT * FROM user"; return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class)); } } ``` 在上面的代码中,`JdbcTemplate` 是 Spring Framework 提供的 JDBC 工具类,可以方便地执行 SQL 语句。Sharding-JDBC 会自动将 SQL 语句分片执行,并将结果合并返回。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值