亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

在mysql中新建數(shù)據(jù)庫sharding_db,新增兩張結(jié)構(gòu)一樣的表student_1和student_2。

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

此處未指定主鍵自增,因?yàn)閮蓮埍淼膇d不能重復(fù),所以只能從后端傳入id。

添加依賴

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Druid連接池 -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.20</version>
</dependency>

<!-- Mysql驅(qū)動(dòng)依賴 -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

<!-- MybatisPlus -->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.0.5</version>
</dependency>

<!-- Sharding-JDBC -->
<dependency>
    <groupId>org.apache.shardingsphere</groupId>
    <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
    <version>4.0.0-RC1</version>
</dependency>

<!-- lombok -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-test</artifactId>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
        </exclusion>
    </exclusions>
</dependency>

編寫配置文件

spring.main.allow-bean-definition-overriding=true

# 配置Sharding-JDBC的分片策略
# 配置數(shù)據(jù)源,給數(shù)據(jù)源起名g1,g2...此處可配置多數(shù)據(jù)源
spring.shardingsphere.datasource.names=g1

# 配置數(shù)據(jù)源具體內(nèi)容:連接池,驅(qū)動(dòng),地址,用戶名,密碼
# 由于上面配置數(shù)據(jù)源只有g(shù)1因此下面只配置g1.type,g1.driver-class-name,g1.url,g1.username,g1.password
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_db?characterEncoding=utf-8&useUnicode=true&useSSL=false&serverTimezone=UTC
spring.shardingsphere.datasource.g1.username=root
spring.shardingsphere.datasource.g1.password=123456

# 配置表的分布,表的策略
spring.shardingsphere.sharding.tables.student.actual-data-nodes=g1.student_$->{1..2}

# 指定student表 主鍵gid 生成策略為 SNOWFLAKE
spring.shardingsphere.sharding.tables.student.key-generator.column=id
spring.shardingsphere.sharding.tables.student.key-generator.type=SNOWFLAKE

# 指定分片策略 約定id值是偶數(shù)添加到student_1表,如果id是奇數(shù)添加到student_2表
spring.shardingsphere.sharding.tables.student.table-strategy.inline.sharding-column=id
spring.shardingsphere.sharding.tables.student.table-strategy.inline.algorithm-expression=student_$->{id % 2 + 1}

# 打開sql輸出日志
spring.shardingsphere.props.sql.show=true

或者是yml格式

spring:
  main:
    allow-bean-definition-overriding: true
  shardingsphere:
    datasource:
      g1:
        driver-class-name: com.mysql.cj.jdbc.Driver
        password: 123456
        type: com.alibaba.druid.pool.DruidDataSource
        url: jdbc:mysql://localhost:3306/sharding_db?characterEncoding=utf-8&useUnicode=true&useSSL=false&serverTimezone=UTC
        username: root
      names: g1
    props:
      sql:
        show: true
    sharding:
      tables:
        student:
          actual-data-nodes: g1.student_$->{1..2}
          key-generator:
            column: id
            type: SNOWFLAKE
          table-strategy:
            inline:
              algorithm-expression: student_$->{id % 2 + 1}
              sharding-column: id

編寫實(shí)體類

@Data
public class Student {
    private Long id;
    private String name;
    private int age;
    private String gender;
}

編寫mapper接口

@Repository
public interface StudentMapper extends BaseMapper<Student> {

}

編寫測試類

@SpringBootTest
class ShardingJdbcDemoApplicationTests {

    @Autowired
    private StudentMapper studentMapper;

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

執(zhí)行測試

在這里插入圖片描述

執(zhí)行成功,主鍵通過雪花算法在后端生成,傳入到數(shù)據(jù)庫中,根據(jù)奇偶性進(jìn)行分表。

student_1表數(shù)據(jù):

在這里插入圖片描述

student_2表數(shù)據(jù):

在這里插入圖片描述

兩張表的數(shù)據(jù)分別有5條,但這只是因?yàn)檠┗ㄋ惴ㄉ傻膇d奇數(shù)偶數(shù)各5個(gè),不是1:1的關(guān)系,需要注意。
主鍵生成后,根據(jù)策略插入到對應(yīng)的表中,從打印出來的sql可以證明這一點(diǎn)。
通過mapper接口的selectById方法進(jìn)行查詢時(shí),會(huì)先根據(jù)主鍵策略判斷在哪個(gè)庫,再直接去那個(gè)庫根據(jù)主鍵查詢。而如果是通過其它條件查詢,或者是多個(gè)id的selectById方法查詢,又是如何的呢?

    @Test
    public void test03() {
        List<Long> list = new ArrayList<>();
        list.add(1362282042768609282l);
        list.add(1362282040277192705l);
        List<Student> studentList = studentMapper.selectBatchIds(list);
        System.out.println(studentList);
    }

取了兩張表的id進(jìn)行查詢。

在這里插入圖片描述

執(zhí)行同樣的sql,在兩張表中都查詢一遍,再組合結(jié)果。
如果所有的id,都來自同一張表,那是否會(huì)去多個(gè)表中重復(fù)查詢呢?

在這里插入圖片描述

只執(zhí)行了一遍。所以,在執(zhí)行查詢時(shí),sharding會(huì)先判斷是否可以確定需要的數(shù)據(jù)來自那張表,如果能,則直接去那一張表中查詢數(shù)據(jù)即可,而如果不能確定,則會(huì)多個(gè)表重復(fù)查詢,以確定查詢結(jié)果的完整性。

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

相關(guān)文章

最新評(píng)論