springboot+ShardingJDBC+mySql分库分表以及不分库分表操作

      数据量达到一定程度的话可能就会进行分库分表,所以最近研究了一个分库分表的插件ShardingJDBC,因为myCat或者其他的开源插件都已经不维护了或者停止更新,目前ShardingJDBC比较活跃,而且不用进行单独部署工作,这点还是比较好的。那么接下来就贴一下代码,看怎么使用它。

1.先把表结构贴出来,我的数据库一个部署在linux的腾讯云上,一个部署在本机上第一个数据库是mall_0,表为t_order_0,t_order_1。第二个数据库是mall_1表为t_order_0,t_order_1。稍后在后边附带数据库脚本。

2.首先需要在pom.xml里添加jar包,必然要放的呀~

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

       <!-- MySQL的JDBC驱动包,操作mysql,必然需要mysql啊 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!-- shardingjdbc,主角登场 -->
        <dependency>
            <groupId>io.shardingjdbc</groupId>
            <artifactId>sharding-jdbc-core</artifactId>
            <version>2.0.3</version>
        </dependency>

        <!-- druid数据库连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.6</version>
        </dependency>

3.jar包下载完毕,就需要写配置文件喽,需要配置数据库连接信息

server:
  port: 9999
spring:
  application:
    name: sharding-jdbc
  jackson:
    time-zone: GMT+8
    date-format: yyyy-MM-dd HH:mm:ss
    default-property-inclusion: non_null
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    ds0:
      url: jdbc:mysql://localhost:3306/mall_0?serverTimezone=UTC
      driverClassName: com.mysql.cj.jdbc.Driver
      username: root
      password: root
    ds1:
      url: jdbc:mysql://xxx:3307/mall_1?serverTimezone=UTC
      driverClassName: com.mysql.cj.jdbc.Driver
      username: root
      password: root

 

4.就需要启动注入方式将分库分表规则写入,再把dataSorce连接加入规则,添加类ShardingConfig

package com.itdf.config;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.
好的,下面是详细的配置步骤: 1. 引入依赖 在 pom.xml 文件中添加以下依赖: ```xml <!-- Sharding JDBC Starter for Spring Boot --> <dependency> <groupId>io.shardingsphere</groupId> <artifactId>sharding-jdbc-spring-boot-starter</artifactId> <version>${sharding-jdbc.version}</version> </dependency> ``` 其中,`${sharding-jdbc.version}` 为 Sharding JDBC 的版本号。你可以在 Maven 中央仓库查找最新版本。 2. 配置数据源 在 application.yml 文件中添加数据源配置,示例如下: ```yaml spring: datasource: url: jdbc:mysql://localhost:3306/test?useSSL=false username: root password: 123456 ``` 这里我们使用 MySQL 作为数据库,其中 `test` 为数据库名称,`root` 和 `123456` 分别是数据库的用户名和密码。 3. 配置 Sharding JDBC 在 application.yml 文件中添加 Sharding JDBC 的配置,示例如下: ```yaml spring: shardingsphere: datasource: names: ds0 ds0: type: com.zaxxer.hikari.HikariDataSource driver-class-name: com.mysql.jdbc.Driver jdbc-url: jdbc:mysql://localhost:3306/test?useSSL=false username: root password: 123456 sharding: tables: order: actualDataNodes: ds0.order_${0..11} tableStrategy: inline: order_${new java.text.SimpleDateFormat("MM").format(new java.util.Date())} keyGenerateStrategy: column: id type: SNOWFLAKE ``` 其中: - `datasource.names`:数据源名称,这里我们只有一个数据源,所以为 `ds0`。 - `datasource.ds0.type`:数据源类型,这里使用 HikariCP 数据源。 - `datasource.ds0.driver-class-name`:数据库驱动类名。 - `datasource.ds0.jdbc-url`:数据库连接 URL。 - `datasource.ds0.username` 和 `datasource.ds0.password`:数据库用户名和密码。 - `sharding.tables.order.actualDataNodes`:分表配置,使用 `${0..11}` 示分为 12 张名为 `order_0` 到 `order_11`。 - `sharding.tables.order.tableStrategy.inline`:分表策略,这里使用月份作为后缀,例如 1 月的名为 `order_1`。 - `sharding.tables.order.keyGenerateStrategy`:主键生成策略,这里使用 Snowflake 算法生成主键。 4. 测试 完成以上配置之后,我们可以在代码中使用自动生成的 `OrderMapper`,例如: ```java @Autowired private OrderMapper orderMapper; @Test public void testInsert() { Order order = new Order(); order.setUserId(1L); order.setOrderId(1L); order.setCreateTime(new Date()); orderMapper.insert(order); } ``` 这样插入数据,就会根据月份自动分到对应的中。 以上就是使用 Spring Boot 和 Sharding JDBC 按照月份分表的详细配置。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值