shardingjdbc使用中遇到的坑

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值