sharding-jdbc+druid实现主从库和分库分表

前置:

springboot2.2.13+shardingjdbc4.1.3+druid1.2.5

 

设计简单表结构:

CREATE TABLE sharding_db_0.`cms_merchant_0` (
  `merchant_id` bigint(20) NOT NULL,
  `merchant_name` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`merchant_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;

CREATE TABLE sharding_db_0.`cms_merchant_1` (
  `merchant_id` bigint(20) NOT NULL,
  `merchant_name` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`merchant_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;

CREATE TABLE sharding_db_1.`cms_merchant_0` (
  `merchant_id` bigint(20) NOT NULL,
  `merchant_name` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`merchant_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;

CREATE TABLE sharding_db_1.`cms_merchant_1` (
  `merchant_id` bigint(20) NOT NULL,
  `merchant_name` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`merchant_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;

 

springboot配置如下:

spring:
  profiles:
    active:
    - dev
#  datasource:
#    driver-class-name: com.mysql.jdbc.Driver
#    url: jdbc:mysql://127.0.0.1:3306/yiciyu?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
#    username: root
#    password: 123456

  
  shardingsphere:
    datasource:
      names: ds0,ds1
      ds0:
        driver-class-name: com.mysql.jdbc.Driver
        type: com.alibaba.druid.pool.DruidDataSource
        url: jdbc:mysql://127.0.0.1:3306/sharding_db_0?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
        username: root
        password: 123456
      ds1:
        driver-class-name: com.mysql.jdbc.Driver
        type: com.alibaba.druid.pool.DruidDataSource
        url: jdbc:mysql://127.0.0.1:3308/sharding_db_1?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
        username: root
        password: 123456
    sharding:
      tables:
        cms_merchant: 
          actual-data-nodes: msds$->{0..1}.cms_merchant_$->{0..1}
          database-strategy:
            inline:
              sharding-column: merchant_id
              #计算方式:value % [库数]
              algorithm-expression: msds$->{merchant_id % 2}
          table-strategy:
            inline:
              sharding-column: merchant_id
              #计算方式:value / [库数] % [表数],示例中仅通过merchant_id后两位路由,为保障
              algorithm-expression: cms_merchant_$->{((int) (Integer.parseInt(Long.toString(merchant_id).substring(1)) / 2)) % 2}
      #可缺省,缺省时走单库方式
      master-slave-rules:
        msds0:
          master-data-source-name: ds0
          slave-data-source-names:
          - ds0
          - ds0
        msds1:
          master-data-source-name: ds1
          slave-data-source-names:
          - ds1
          - ds1
              
          
    
mybatis-plus:
  mapper-locations:  classpath:mapper/**/*.xml
  type-aliases-package: com.yiciyu.*.entity,com.yiciyu.*.model
  global-config:
    db-config:
      id-type: auto
      table-underline: true

 

测试代码:

package com.yiciyu.se;

import java.util.Arrays;
import java.util.List;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yiciyu.lu.util.JsonUtils;
import com.yiciyu.se.dao.MerchantDao;
import com.yiciyu.se.entity.MerchantEntity;

@SpringBootTest
class ShardingDbApplicationTests {
	
	@Autowired
	private MerchantDao merchantDao;

	@Test
	void insertTests() {
		for (long i = 100; i < 200; i++) {
			MerchantEntity entity = new MerchantEntity();
			entity.setMerchantId(i);
			entity.setMerchantName("商户名-" + i);
			merchantDao.insert(entity);
		}
	}
	
	@Test
	void updateTests() {
		for (long i = 100; i < 200; i++) {
			MerchantEntity entity = new MerchantEntity();
			entity.setMerchantId(i);
			entity.setMerchantName("update-商户名-" + i);
			merchantDao.updateById(entity);
		}
	}
	
	@Test
	void singleQueryTests() {
		for (long i = 100; i < 200; i++) {
			MerchantEntity entity = merchantDao.selectById(i);
			System.out.println(i + "===" + JsonUtils.toString(entity));
		}
	}
	
	@Test
	void multQueryTests() {
		//inline不支持range(即between、大于小于等范围查询)
		List<MerchantEntity> list = merchantDao.selectList(new QueryWrapper<MerchantEntity>()
				.in("merchant_id", Arrays.asList(100L, 101L, 102L, 103L)));
		System.out.println("list===" + JsonUtils.toString(list));
	}

}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值