最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用sharding-jdbc實(shí)現(xiàn)水平分庫(kù)+水平分表的示例代碼

 更新時(shí)間:2021年12月17日 09:56:56   作者:穿條秋褲到處跑  
本文主要介紹了使用sharding-jdbc實(shí)現(xiàn)水平分庫(kù)+水平分表,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

前面的文章使用sharding-jdbc實(shí)現(xiàn)水平分表中詳細(xì)記錄了如何使用sharding-jdbc實(shí)現(xiàn)水平分表,即根據(jù)相應(yīng)的策略,將一部分?jǐn)?shù)據(jù)存入到表1中,一部分?jǐn)?shù)據(jù)存入到表2中,邏輯上為同一張表,分表操作全部交由sharding-jdbc進(jìn)行處理。
可能根據(jù)需要,還需要將一張表的數(shù)據(jù)拆分存入到多個(gè)數(shù)據(jù)庫(kù)中,甚至多個(gè)數(shù)據(jù)庫(kù)的多個(gè)表中,使用sharding-jdbc同樣可以實(shí)現(xiàn)。

重復(fù)的篇幅則不再贅述,下面重點(diǎn)記錄升級(jí)的過(guò)程。
分庫(kù)分表策略:將id為偶數(shù)的存入到庫(kù)1中,奇數(shù)存入到庫(kù)2中,在每個(gè)庫(kù)中,再根據(jù)學(xué)生的性別分別存到到表1和表2中。

新建兩個(gè)數(shù)據(jù)庫(kù)sharding_db1和sharding_db2,在兩個(gè)數(shù)據(jù)庫(kù)中在分別創(chuàng)建結(jié)構(gòu)相同的兩張表,student_1和student_2。

CREATE TABLE `NewTable` (
`ID`  bigint(20) NOT NULL ,
`NAME`  varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL ,
`AGE`  int(11) NOT NULL ,
`GENDER`  int(1) NOT NULL ,
PRIMARY KEY (`ID`)
);

相比前面文章中,將gender性別字段設(shè)置成了int類型,方便根據(jù)性別再進(jìn)行分表。

修改配置文件

spring.main.allow-bean-definition-overriding=true
# 配置Sharding-JDBC的分片策略
# 配置數(shù)據(jù)源,給數(shù)據(jù)源起名g1,g2...此處可配置多數(shù)據(jù)源
spring.shardingsphere.datasource.names=g1,g2
# 配置數(shù)據(jù)源具體內(nèi)容:連接池,驅(qū)動(dòng),地址,用戶名,密碼
spring.shardingsphere.datasource.g1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.g1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.g1.url=jdbc:mysql://localhost:3306/sharding_db1?characterEncoding=utf-8&useUnicode=true&useSSL=false&serverTimezone=UTC
spring.shardingsphere.datasource.g1.username=root
spring.shardingsphere.datasource.g1.password=123456
spring.shardingsphere.datasource.g2.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.g2.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.g2.url=jdbc:mysql://localhost:3306/sharding_db2?characterEncoding=utf-8&useUnicode=true&useSSL=false&serverTimezone=UTC
spring.shardingsphere.datasource.g2.username=root
spring.shardingsphere.datasource.g2.password=123456
# 配置數(shù)據(jù)庫(kù)的分布,表的分布
spring.shardingsphere.sharding.tables.student.actual-data-nodes=g$->{1..2}.student_$->{1..2}
# 指定student表 主鍵gid 生成策略為 SNOWFLAKE
spring.shardingsphere.sharding.tables.student.key-generator.column=id
spring.shardingsphere.sharding.tables.student.key-generator.type=SNOWFLAKE
# 指定數(shù)據(jù)庫(kù)分片策略 約定id值是偶數(shù)添加到sharding_db1中,奇數(shù)添加到sharding_db2中
spring.shardingsphere.sharding.tables.student.database-strategy.inline.sharding-column=id
spring.shardingsphere.sharding.tables.student.database-strategy.inline.algorithm-expression=g$->{id % 2 + 1}
# 指定表分片策略 約定gender值是0添加到student_1表,如果gender是1添加到student_2表
spring.shardingsphere.sharding.tables.student.table-strategy.inline.sharding-column=gender
spring.shardingsphere.sharding.tables.student.table-strategy.inline.algorithm-expression=student_$->{gender % 2 + 1}
# 打開(kāi)sql輸出日志
spring.shardingsphere.props.sql.show=true

配置多個(gè)數(shù)據(jù)源時(shí),使用逗號(hào)隔開(kāi),分別配置其屬性。除了配置表分片策略,還需配置庫(kù)分配策略。

測(cè)試類

@SpringBootTest
class ShardingJdbcDemoApplicationTests {

    @Autowired
    private StudentMapper studentMapper;

    @Test
    public void test01() {
        for (int i = 0; i < 15; i++) {
            Student student = new Student();
            student.setName("wuwl");
            student.setAge(27);
            student.setGender(i%2);
            studentMapper.insert(student);
        }
    }
}

運(yùn)行效果:

在這里插入圖片描述

看樣子是成功了,查看數(shù)據(jù)庫(kù)數(shù)據(jù)。

sharding_db1.student_1:

在這里插入圖片描述

sharding_db1.student_2:

在這里插入圖片描述

sharding_db2.student_1:

在這里插入圖片描述

sharding_db2.student_2:

在這里插入圖片描述

到此這篇關(guān)于使用sharding-jdbc實(shí)現(xiàn)水平分庫(kù)+水平分表的示例代碼的文章就介紹到這了,更多相關(guān)sharding-jdbc水平分庫(kù)水平分表內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • spring中@autowired、@Qualifier、@Primary注解的使用說(shuō)明

    spring中@autowired、@Qualifier、@Primary注解的使用說(shuō)明

    這篇文章主要介紹了spring中@autowired、@Qualifier、@Primary注解的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • springboot整合阿里云百煉DeepSeek實(shí)現(xiàn)sse流式打印的操作方法

    springboot整合阿里云百煉DeepSeek實(shí)現(xiàn)sse流式打印的操作方法

    這篇文章主要介紹了springboot整合阿里云百煉DeepSeek實(shí)現(xiàn)sse流式打印,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2025-04-04
  • Java實(shí)現(xiàn)聊天室界面

    Java實(shí)現(xiàn)聊天室界面

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)聊天室界面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • Java人民幣小寫轉(zhuǎn)大寫字符串的實(shí)現(xiàn)

    Java人民幣小寫轉(zhuǎn)大寫字符串的實(shí)現(xiàn)

    這篇文章主要介紹了Java人民幣小寫轉(zhuǎn)大寫字符串的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • 聊一聊Java的JVM類加載機(jī)制

    聊一聊Java的JVM類加載機(jī)制

    這篇文章主要介紹了聊一聊Java的JVM類加載機(jī)制,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • 解決idea默認(rèn)帶的equals和hashcode引起的bug

    解決idea默認(rèn)帶的equals和hashcode引起的bug

    這篇文章主要介紹了解決idea默認(rèn)帶的equals和hashcode引起的bug,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • java 中@Deprecated 注解的實(shí)例詳解

    java 中@Deprecated 注解的實(shí)例詳解

    這篇文章主要介紹了java 中@Deprecated 注解的實(shí)例詳解的相關(guān)資料,這里對(duì)@Deprecated注解進(jìn)行了詳細(xì)介紹,希望能幫助到大家,需要的朋友可以參考下
    2017-08-08
  • SpringBoot中自定義參數(shù)綁定步驟詳解

    SpringBoot中自定義參數(shù)綁定步驟詳解

    這篇文章主要介紹了SpringBoot中自定義參數(shù)綁定步驟詳解,非常不錯(cuò),具有參考借鑒價(jià)值 ,需要的朋友可以參考下
    2018-02-02
  • java 動(dòng)態(tài)代理的方法總結(jié)

    java 動(dòng)態(tài)代理的方法總結(jié)

    這篇文章主要介紹了java 動(dòng)態(tài)代理的方法總結(jié)的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • 基于Consumer接口、Predicate接口初使用

    基于Consumer接口、Predicate接口初使用

    這篇文章主要介紹了Consumer接口、Predicate接口初使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12

最新評(píng)論

广东省| 专栏| 玉环县| 酉阳| 敖汉旗| 阿鲁科尔沁旗| 双城市| 富民县| 彭州市| 湘乡市| 奉化市| 平谷区| 富蕴县| 黑龙江省| 永嘉县| 灵寿县| 资源县| 栖霞市| 宁化县| 华亭县| 天等县| 精河县| 克山县| 体育| 浮梁县| 乌海市| 许昌市| 泸水县| 峨山| 江阴市| 垦利县| 巴青县| 乌什县| 香港 | 阜宁县| 宁南县| 正宁县| 阿克陶县| 黄骅市| 赤城县| 三江|