zoukankan      html  css  js  c++  java
  • ShardingJdbc:公共表

    接着上一篇:https://www.cnblogs.com/wwjj4811/p/14598033.html

    公共表

    存储固定数据的表,表数据很少变化,查询时候经常进行关联。(例如项目中的码值表)

    环境搭建

    在每个数据库中创建出相同结构的公共表

    create table t_udict (
    	dictid BIGINT(20) PRIMARY KEY,
    	ustatus VARCHAR(100) not null ,
    	uvalue VARCHAR(100) not null 
    )
    

    实体类和mapper:

    @Data
    @TableName("t_udict")
    public class Udict {
        private Long dictid;
        private String ustatus;
        private String uvalue;
    }
    
    public interface UdictMapper extends BaseMapper<Udict> {
    }
    

    修改配置文件

    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
    #------------------------------------------------------------------------#
    

    测试

    测试插入

        @Resource
        UdictMapper udictMapper;
    
        @Test
        void addUdict(){
            Udict udict = new Udict();
            udict.setUstatus("1");
            udict.setUvalue("在线");
            udictMapper.insert(udict);
        }
    

    image-20210331094410438

    ShardingJdbc会向所有的数据库中的公共表插入该条数据。

    测试查询

        @Test
        void getOneUdict(){
            List<Udict> udicts = udictMapper.selectList(null);
            for (Udict udict : udicts) {
                System.out.println(udict);
            }
        }
    

    image-20210331094715713

    查询公共表只会向其中一个数据库查询一次。

  • 相关阅读:
    c/c++ 网络编程 getaddrinfo 函数
    c/c++ 网络编程 bind函数
    c/c++ socket API 调用后的错误判断 perror errno
    python基础-面向对象编程之反射
    彻底理解Future模式
    Java并发编程:Callable、Future和FutureTask
    java异步调用方法
    理解ThreadLocal
    ReentrantReadWriteLock读写锁
    java锁优化
  • 原文地址:https://www.cnblogs.com/wwjj4811/p/14600080.html
Copyright © 2011-2022 走看看