5、ShardingSphere 之 公共表
生活随笔
收集整理的這篇文章主要介紹了
5、ShardingSphere 之 公共表
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
文章目錄
- 1 公共表
- 2 在多個數(shù)據(jù)庫中創(chuàng)建公共表
- 2.1 edudb1庫中t_dict
- 2.2 edudb2庫中t_dict
- 2.3 userdb庫中t_dict
- 3 創(chuàng)建po
- 4 創(chuàng)建mapper
- 5 創(chuàng)建application.properties配置文件
- 6 Test
- 7 Test result
1 公共表
1.1 存儲固定數(shù)據(jù)的表,表數(shù)據(jù)很少發(fā)生變化,查詢時候經(jīng)常進(jìn)行關(guān)聯(lián)
1.2 在每個數(shù)據(jù)庫中創(chuàng)建出相同結(jié)構(gòu)公共表
2 在多個數(shù)據(jù)庫中創(chuàng)建公共表
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 創(chuàng)建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 創(chuàng)建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 創(chuàng)建application.properties配置文件
# sharding-JDBC分片策略(公共表配置)# 配置數(shù)據(jù)源,給數(shù)據(jù)源命名 spring.shardingsphere.datasource.names=ds0,ds1,ds2# 配置數(shù)據(jù)源具體內(nèi)容,連接池、驅(qū)動、地址、用戶名和密碼 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# 配置數(shù)據(jù)源具體內(nèi)容,連接池、驅(qū)動、地址、用戶名和密碼 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# 配置數(shù)據(jù)源具體內(nèi)容,連接池、驅(qū)動、地址、用戶名和密碼 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# 一個實(shí)體類對應(yīng)兩張表,覆蓋 spring.main.allow-bean-definition-overriding=true# 配置公共表 spring.shardingsphere.sharding.broadcast-tables=t_dict# 配置數(shù)據(jù)庫中 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=true6 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
總結(jié)
以上是生活随笔為你收集整理的5、ShardingSphere 之 公共表的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 4、ShardingSphere 之 S
- 下一篇: 3、MySQL 8.0.20在Linux