二、Sharding JDBC实战-垂直分库分表

1.说明

垂直拆分

2.添加Sharding-jdbc依赖包
	<dependency>
		<groupId>org.apache.shardingsphere</groupId>
        <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
        <version>4.1.1</version>
	</dependency>
3.完整配置yml文件
spring:
  #Sharding-JDBC的配置
  shardingsphere:
    datasource:
      #数据源名称,多个用逗号分开
      names: ds1,ds2
      ds1:
        type: com.alibaba.druid.pool.DruidDataSource
        url: jdbc:mysql://192.168.0.1:3306/ds1?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
        username: root
        password: 123456
        driver-class-name: com.mysql.cj.jdbc.Driver
      ds2:
        type: com.alibaba.druid.pool.DruidDataSource
        url: jdbc:mysql://192.168.0.1:3306/ds2?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
        username: root
        password: 123456
        driver-class-name: com.mysql.cj.jdbc.Driver
    #分片的配置
    sharding:
      #表的分片策略
      tables:
        product:
          #数据节点配置,采用Groovy表达式,切分之后的真实表所在的节点
          actual-data-nodes: ds$->{1}.product
          #主键生成策略
          key-generator:
            #主键
            column: id
            #生成算法
            type: SNOWFLAKE
        shop:
          #数据节点配置,采用Groovy表达式,切分之后的真实表所在的节点
          actual-data-nodes: ds$->{2}.shop
          #主键生成策略
          key-generator:
            #主键
            column: id
            #生成算法
            type: SNOWFLAKE
    props:
      sql:
        show: true
4.附录
	<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.2</version>
    </parent>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.5.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-core</artifactId>
            <version>5.8.0</version>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-extra</artifactId>
            <version>5.8.0</version>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-crypto</artifactId>
            <version>5.8.0</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.56</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.3.4</version>
        </dependency>
       <!--sharding-jdbc-->
        <dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
            <version>4.1.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.4</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.10</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class)
@ActiveProfiles("dev")
public class ProductTest {

    @Autowired
    private ProductService productService;

    @Autowired
    private ShopService shopService;

    @Test
    public void testInsert(){
        Shop shop = new Shop();
        shop.setId(IdWorker.getId());
        shop.setName("汉服专卖店");
        shopService.save(shop);
        Product product = new Product();
        product.setLocation("广东省东莞市");
        product.setName("迷彩服");
        product.setShopId(shop.getId());
        product.setId(IdWorker.getId());
        productService.save(product);
    }
}
5.注意事项

(1)注意引入的druid包
(2) sharding-jdbc版本 4.1.1
(3)使用了mybatis-plus的Idworker

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值