ShardingSphere之公共表实战(七)

导读:本篇文章讲解 ShardingSphere之公共表实战(七),希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

概述

场景

  • 什么叫公共表?
    存储固定数据的表,表中的数据很少发生变化,查询时经常进行关联,比如字典表。
  • 每个数据库中创建出相同结构的公共表。

SpringBoot环境搭建

参考ShardingSphere之水平分表实战(三)

建表

在多个数据库中都创建结构相同的公共表。
在这里插入图片描述

CREATE TABLE `t_udict` (
  `dictid` BIGINT(11) NOT NULL PRIMARY KEY,
  `ustatus` varchar(255) DEFAULT NULL,
  `uvalue` varchar(255) DEFAULT NULL
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4;

application.properties配置

# 配置公共表
spring.shardingsphere.sharding.broadcast-tables=t_udict
spring.shardingsphere.sharding.tables.t_udict.key-generator.column=dictid
spring.shardingsphere.sharding.tables.t_udict.key-generator.type=SNOWFLAKE

编写代码

package com.study.shardingjdbcdemo.entity;

import lombok.Data;

import javax.persistence.Id;
import javax.persistence.Table;

@Data
@Table(name = "t_udict")
public class Udict {
    @Id
    private Long dictid;

    private String ustatus;

    private String uvalue;
}

package com.study.shardingjdbcdemo.mapper;

import com.study.shardingjdbcdemo.entity.Udict;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;

@Repository
public interface UdictMapper extends Mapper<Udict> {
}

测试代码

@Test
    public void addCommon() {
        Udict udict = new Udict();
        udict.setUvalue("已启用");
        udict.setUstatus("a");
        udictMapper.insertSelective(udict);
    }

    @Test
    public void deleteCommon() {
        udictMapper.deleteByPrimaryKey(760236497562501121l);
    }

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/100036.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!