Sharding-JDBC 之公共表

一、公共表概念

  1. 存储固定数据的表,表数据很少发生变化,查询时经常要进行关联。
  2. 在每个数据库中都创建出相同结构公共表。
  3. 操作公共表时,同时操作添加了公共表的数据库中的公共表,添加记录时,同时添加,删除时,同时删除。

二、在多个数据库中创建相同结构的公共表

1、这里我们准备两个库

        1)、edu_db_1

        2)、edu_db_2

2、创建公共表

这里我们创建两个功能表

create table t_udict(
	`dictid` bigint(20) primary key,
	`ustatus` varchar(100) not null,
	`uvalue` varchar(100) not null
);

create table p_diction(
	`dictId` bigint(20) primary key,
	`ustatus` varchar(100) not null,
	`uvalue` varchar(100) not null
);

 

  


三、在yml中对公共表进行配置

spring:
  shardingsphere:
    # 数据源配置
    datasource:
      # 数据源名称,多数据源以逗号分隔,名称可以随意起名
      names: db1,db2
      db1: #配置第一个数据源
        driver-class-name: com.mysql.cj.jdbc.Driver
        type: com.alibaba.druid.pool.DruidDataSource
        password: Tz@202011
        username: root
        url: jdbc:mysql://192.168.56.20:3306/edu_db_1?allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
      db2: #配置第一个数据源
        driver-class-name: com.mysql.cj.jdbc.Driver
        type: com.alibaba.druid.pool.DruidDataSource
        password: Tz@202011
        username: root
        url: jdbc:mysql://192.168.56.20:3306/edu_db_2?allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
    # 规则配置
    rules:
      sharding:
        # 分布式序列算法配置
        key-generators:
          dict-snowflake:
            type: snowflake #分布式序列算法类型,雪花算法:SNOWFLAKE; UUID:UUID)
            column: dictid #分布式主键列
          pdict-snowflake:
            type: snowflake #分布式序列算法类型,雪花算法:SNOWFLAKE; UUID:UUID)
            column: dictId #分布式主键列
        tables:
          # 逻辑表名称
          t_udict:
            key-generate-strategy:
              column: dictid #分布式序列列名称
              key-generator-name: dict-snowflake #分布式序列算法名称
          p_diction:
            key-generate-strategy:
              column: dictId #分布式序列列名称
              key-generator-name: pdict-snowflake #分布式序列算法名称
        broadcast-tables:
          - t_udict
          - p_diction
    # 属性配置
    props:
      # 展示修改以后的sql语句
      sql-show: true

四、编写测试代码

1、创建Controller

package com.sharding.demo.web;


import com.sharding.demo.model.Pdict;
import com.sharding.demo.model.Udict;
import com.sharding.demo.service.DictionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "dict")
public class DictionController {

    @Autowired
    private DictionService dictionService;

    @RequestMapping(value = "addDict")
    public void addDict(String type){
        if (type.equals("1")){
            Udict udict = new Udict();
            udict.setUstatus("1");
            udict.setUvalue("10");
            dictionService.addUdict(udict);
        }else{
            Pdict pdict = new Pdict();
            pdict.setUstatus("1");
            pdict.setUvalue("10");
            dictionService.addPdict(pdict);
        }
    }

    @RequestMapping(value = "delDictionByParam")
    public void delCourseByParam(String dictId,String type){
        if (type.equals("1")){
            dictionService.delUdictById(dictId);
        }else{
            dictionService.delPdictById(dictId);
        }
    }
}

2、创建Service

package com.sharding.demo.service;

import com.sharding.demo.model.Pdict;
import com.sharding.demo.model.Udict;

public interface DictionService {
    void addUdict(Udict udict);

    void addPdict(Pdict pdict);

    void delUdictById(String dictId);

    void delPdictById(String dictId);
}
package com.sharding.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sharding.demo.mapper.PDictionMapper;
import com.sharding.demo.mapper.UDictionMapper;
import com.sharding.demo.model.Pdict;
import com.sharding.demo.model.Udict;
import com.sharding.demo.service.DictionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class DictionServiceImpl implements DictionService {

    @Resource
    private UDictionMapper uDictionMapper;

    @Resource
    private PDictionMapper pDictionMapper;

    @Override
    public void addUdict(Udict udict) {
        uDictionMapper.insert(udict);
    }

    @Override
    public void addPdict(Pdict pdict) {
        pDictionMapper.insert(pdict);
    }

    @Override
    public void delUdictById(String dictId) {
        QueryWrapper<Udict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dictid",dictId);
        uDictionMapper.delete(queryWrapper);
    }

    @Override
    public void delPdictById(String dictId) {
        QueryWrapper<Pdict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dictId",dictId);
        pDictionMapper.delete(queryWrapper);
    }
}

3、创建Mapper

package com.sharding.demo.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sharding.demo.model.Udict;

public interface UDictionMapper extends BaseMapper<Udict> {
}
package com.sharding.demo.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sharding.demo.model.Pdict;

public interface PDictionMapper extends BaseMapper<Pdict> {
}

五、进行测试(对其中p_diction测试一下)

1、向其中的p_diction插入数据

edu_db_1库中:

edu_db_2库中:

 

2、删除数据 

 edu_db_1库中:

edu_db_2库中:

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值