日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

5、ShardingSphere 之 公共表

發布時間:2025/3/19 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 5、ShardingSphere 之 公共表 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

      • 1 公共表
      • 2 在多個數據庫中創建公共表
        • 2.1 edudb1庫中t_dict
        • 2.2 edudb2庫中t_dict
        • 2.3 userdb庫中t_dict
      • 3 創建po
      • 4 創建mapper
      • 5 創建application.properties配置文件
      • 6 Test
      • 7 Test result

1 公共表

1.1 存儲固定數據的表,表數據很少發生變化,查詢時候經常進行關聯

1.2 在每個數據庫中創建出相同結構公共表

2 在多個數據庫中創建公共表

2.1 edudb1庫中t_dict

CREATE TABLE `edudb1`.`t_dict` (`dict_id` BIGINT NOT NULL,`ustatus` VARCHAR(45) NOT NULL,`uvalue` VARCHAR(45) NOT NULL,PRIMARY KEY (`dict_id`));

2.2 edudb2庫中t_dict

CREATE TABLE `edudb2`.`t_dict` (`dict_id` BIGINT NOT NULL,`ustatus` VARCHAR(45) NOT NULL,`uvalue` VARCHAR(45) NOT NULL,PRIMARY KEY (`dict_id`));

2.3 userdb庫中t_dict

CREATE TABLE `userdb`.`t_dict` (`dict_id` BIGINT NOT NULL,`ustatus` VARCHAR(45) NOT NULL,`uvalue` VARCHAR(45) NOT NULL,PRIMARY KEY (`dict_id`));

3 創建po

package com.ccb.sharding.po;import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName;@TableName("t_dict") public class Dict {@TableId("dict_id")private Long dictId;private String ustatus;private String uvalue;public Long getDictId() {return dictId;}public void setDictId(Long dictId) {this.dictId = dictId;}public String getUstatus() {return ustatus;}public void setUstatus(String ustatus) {this.ustatus = ustatus;}public String getUvalue() {return uvalue;}public void setUvalue(String uvalue) {this.uvalue = uvalue;}@Overridepublic String toString() {return "Dict{" +"dictId=" + dictId +", ustatus='" + ustatus + '\'' +", uvalue='" + uvalue + '\'' +'}';} }

4 創建mapper

package com.ccb.sharding.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.ccb.sharding.po.Dict; import org.springframework.stereotype.Repository;@Repository public interface DictMapper extends BaseMapper<Dict> {}

5 創建application.properties配置文件

# sharding-JDBC分片策略(公共表配置)# 配置數據源,給數據源命名 spring.shardingsphere.datasource.names=ds0,ds1,ds2# 配置數據源具體內容,連接池、驅動、地址、用戶名和密碼 spring.shardingsphere.datasource.ds0.type=com.alibaba.druid.pool.DruidDataSource spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.cj.jdbc.Driver spring.shardingsphere.datasource.ds0.url=jdbc:mysql://localhost:3306/userdb?serverTimezone=GMT%2B8 spring.shardingsphere.datasource.ds0.username=root spring.shardingsphere.datasource.ds0.password=chengwen# 配置數據源具體內容,連接池、驅動、地址、用戶名和密碼 spring.shardingsphere.datasource.ds1.type=com.alibaba.druid.pool.DruidDataSource spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.cj.jdbc.Driver spring.shardingsphere.datasource.ds1.url=jdbc:mysql://localhost:3306/edudb1?serverTimezone=GMT%2B8 spring.shardingsphere.datasource.ds1.username=root spring.shardingsphere.datasource.ds1.password=chengwen# 配置數據源具體內容,連接池、驅動、地址、用戶名和密碼 spring.shardingsphere.datasource.ds2.type=com.alibaba.druid.pool.DruidDataSource spring.shardingsphere.datasource.ds2.driver-class-name=com.mysql.cj.jdbc.Driver spring.shardingsphere.datasource.ds2.url=jdbc:mysql://localhost:3306/edudb2?serverTimezone=GMT%2B8 spring.shardingsphere.datasource.ds2.username=root spring.shardingsphere.datasource.ds2.password=chengwen# 一個實體類對應兩張表,覆蓋 spring.main.allow-bean-definition-overriding=true# 配置公共表 spring.shardingsphere.sharding.broadcast-tables=t_dict# 配置數據庫中 t_dict 表主鍵 dict_id 生成策略 SNOWFLAKE 雪花算法 spring.shardingsphere.sharding.tables.t_dict.key-generator.column=dict_id spring.shardingsphere.sharding.tables.t_dict.key-generator.type=SNOWFLAKE# 打印sql輸出日志 spring.shardingsphere.props.sql.show=true

6 Test

package com.ccb.sharding;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.ccb.sharding.mapper.DictMapper; import com.ccb.sharding.po.Dict; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest class ShardingApplicationTests {@AutowiredDictMapper dictMapper;// ================= 測試公共表 ======================@Testpublic void addDict(){Dict dict = new Dict();dict.setUstatus("S");dict.setUvalue("已成功");dictMapper.insert(dict);}@Testpublic void getDict(){QueryWrapper queryWrapper = new QueryWrapper();queryWrapper.eq("dict_id","1276077724019986434");Dict dict = dictMapper.selectOne(queryWrapper);System.out.println(dict);}

7 Test result

edudb1

edudb2

userdb

getDict

總結

以上是生活随笔為你收集整理的5、ShardingSphere 之 公共表的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。