通用版本的Sharding 实现单库分表

无论你springboot还是SSM 都可以整合Sharding 实现单库分表 但是我这里用的是Spring boot 整合Sharding 

<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository --> 下边是我的 spring boot版本

第一步:引入 pom文件的依赖 (注意: druid 一定要引入)

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.10</version>
</dependency>

<dependency>
    <groupId>com.dangdang</groupId>
    <artifactId>sharding-jdbc-config-spring</artifactId>
    <version>1.5.4.1</version>
</dependency>

第二步:  application.yml配置文件需要配置参数

sharding:
  jdbc:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://192.168.101.36:3306/wt-admin?characterEncoding=utf8
    username: wt
    password: 12345678

这里我解释一下 这个 application.yml 这个配置文件 只是一个参数便于DruidDataSource 连接池获取参数

第三步:建立 2个class文件和一个配置文件

 

一个文件叫:OrderShardingAlgorithm  下边是代码 直接复制就可以
package com.jxd.test.controller;
import com.dangdang.ddframe.rdb.sharding.api.ShardingValue;
import com.dangdang.ddframe.rdb.sharding.api.strategy.table.SingleKeyTableShardingAlgorithm;
import com.google.common.collect.Range;

import java.util.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ShardingSphere是一个开源的分布式数据库中间件,提供分库分、读写分离、数据加密等功能。其中,Sharding-JDBC是ShardingSphere中的一个子模块,专门用于实现分库分功能。下面简单介绍下Sharding-JDBC的使用步骤: 1. 引入依赖 ```xml <dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>sharding-jdbc-core</artifactId> <version>${sharding-jdbc.version}</version> </dependency> ``` 2. 配置数据源 在`application.yml`中配置数据源信息,以MySQL为例: ```yaml spring: datasource: sharding: type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.mysql.cj.jdbc.Driver username: root password: root jdbc-url: jdbc:mysql://localhost:3306/sharding_db?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8&rewriteBatchedStatements=true ``` 3. 配置分片规则 在`sharding.yml`中配置分片规则,以按照用户ID分库分为例: ```yaml sharding: tables: user: actualDataNodes: ds$->{0..1}.user$->{0..2} tableStrategy: standard: shardingColumn: user_id preciseAlgorithmClassName: org.apache.shardingsphere.shardingalgorithm.sharding.standard.PreciseModuloShardingAlgorithm rangeAlgorithmClassName: org.apache.shardingsphere.shardingalgorithm.sharding.standard.RangeModuloShardingAlgorithm databaseStrategy: standard: shardingColumn: user_id preciseAlgorithmClassName: org.apache.shardingsphere.shardingalgorithm.sharding.standard.PreciseModuloShardingAlgorithm rangeAlgorithmClassName: org.apache.shardingsphere.shardingalgorithm.sharding.standard.RangeModuloShardingAlgorithm ``` 其中,`actualDataNodes`指定了实际数据节点的名称,`tableStrategy`和`databaseStrategy`分别指定了和库的分片策略。 4. 配置分布式事务 如果需要使用分布式事务,需要配置对应的事务管理器,以Atomikos为例: ```yaml spring: jta: atomikos: datasource: xa-data-source-class-name: com.zaxxer.hikari.HikariDataSource min-pool-size: 1 max-pool-size: 20 max-lifetime: 300000 borrow-connection-timeout: 30000 login-timeout: 3000 maintenance-interval: 60 max-idle-time: 600000 recovery: log-file-name: txlog recovery-dir: ./ recover-delay: 10000 max-retries: 10 ``` 5. 使用Sharding-JDBC 在代码中使用`ShardingDataSource`代替原来的`DataSource`,例如: ```java @Autowired private DataSource dataSource; public void test() throws SQLException { Connection conn = dataSource.getConnection(); PreparedStatement ps = conn.prepareStatement("insert into user(user_id, username) values (?, ?)"); ps.setInt(1, 1); ps.setString(2, "test"); ps.executeUpdate(); ps.close(); conn.close(); } ``` 以上就是Sharding-JDBC的基本使用方法,如果需要更加详细的配置和使用,请参考官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值