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

Springboot集成Mybatis-plus、ClickHouse實(shí)現(xiàn)增加數(shù)據(jù)、查詢數(shù)據(jù)功能

 更新時(shí)間:2024年08月27日 11:45:37   作者:青花科技  
本文給大家講解Springboot + mybatis-plus 集成ClickHouse,實(shí)現(xiàn)增加數(shù)據(jù)、查詢數(shù)據(jù)功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧

前言

上一章節(jié)講解在阿里云ECS centos服務(wù)器上安裝ClickHouse。
這一章節(jié)我們?cè)赟pringboot里集成Mybatis-plus、ClickHouse。
環(huán)境:JDK8 + Springboot 2.6.13 + ClickHouse

1、構(gòu)建JDK8 + Springboot 2.6.13項(xiàng)目

JDK8 + Springboot 2.x基本上都可以,保險(xiǎn)起見,2.5-2.7左右最好。

1.1、修改Server URL,支持Java8

在Idea里創(chuàng)建一個(gè)Springboot項(xiàng)目,首先修改Server URL,默認(rèn)的Server URL已經(jīng)不支持JDK8。

1.2、 選擇Springboot 版本、選擇加載的依賴包

1.3、查看pom.xml文件

構(gòu)建完成之后,就會(huì)生成一個(gè)Springboot項(xiàng)目,文件里最主要是pom.xml文件。

1.4、檢查項(xiàng)目結(jié)構(gòu)

1.4.1、檢查項(xiàng)目設(shè)置

與我們的項(xiàng)目里選擇的JDK8保持一致

1.4.2、檢查模塊

檢查項(xiàng)目結(jié)構(gòu),語言級(jí)別、Sources、Resources、Test Resources等。

1.5、檢查項(xiàng)目配置

檢查項(xiàng)目的Settings。

1.5.1、配置Maven環(huán)境

(JDK8 對(duì)應(yīng)的是3.3 - 3.9等,一般使用3.6、3.8最佳)

1.5.2、檢查Java編譯配置

檢查Java編譯配置,1.8、8都可以,代表使用java8編譯Java文件。

2、集成Mybatis-plus、ClickHouse

2.1、加載Mybatis-plus、ClickHouse依賴包

在pom.xml文件里加載Mybatis-plus、ClickHouse依賴包。
順道把fastjson也加入進(jìn)去。

        <dependency>
            <groupId>ru.yandex.clickhouse</groupId>
            <artifactId>clickhouse-jdbc</artifactId>
            <version>0.1.53</version>
        </dependency>
        <!--Mybatis-plus ORM-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>2.0.33</version>
        </dependency>

2.2、修改配置文件

把默認(rèn)的application.properties文件修改為 application.yaml文件

spring:
  application:
    name: clickhouse-project
  datasource:
    type: com.zaxxer.hikari.HikariDataSource
    url: jdbc:clickhouse://clickhouse遠(yuǎn)程主機(jī):8123/default
    driver-class-name: ru.yandex.clickhouse.ClickHouseDriver
    username: default
    password:
mybatis-plus:
  # 搜索指定包別名
  type-aliases-package: com.xique.springbootclick.entity
  configuration:
    map-underscore-to-camel-case: true  #開啟駝峰命名
    cache-enabled: false #開啟二級(jí)緩存
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 控制臺(tái)日志
  check-config-location: true # 檢查xml是否存在
  type-enums-package: com.gton.enumPackage  #通用枚舉開啟
  global-config:
    db-config:
      logic-not-delete-value: 1
      logic-delete-field: isDel
      logic-delete-value: 0
      table-prefix: t_
server:
  port: 18123

3、在clickhouse里添加表

創(chuàng)建一張商品表,skuId、url、image、country、name、price、freight等字段。

CREATE TABLE default.t_product
(
    `skuId`  String,
    `url` String,
    `image` String,
    `country` String,
    `name` String,
    `price` String,
    `freight` String
)
ENGINE = SummingMergeTree
PARTITION BY country
ORDER BY skuId 
SETTINGS index_granularity = 8192 

數(shù)據(jù)庫表創(chuàng)建成功,使用show tables命令,就可以看到我們創(chuàng)建的表了。

4、在Springboot項(xiàng)目里創(chuàng)建商品表的操作類

4.1、添加雪花ID實(shí)現(xiàn)

我們這次采用雪花ID生成商品SkuId,所以添加雪花ID實(shí)現(xiàn)

public class SnowflakeIdWorker {
    /**
     * 開始時(shí)間截 (2015-01-01)
     */
    private final long twepoch = 1420041600000L;
    /**
     * 機(jī)器id所占的位數(shù)
     */
    private final long workerIdBits = 5L;
    /**
     * 數(shù)據(jù)標(biāo)識(shí)id所占的位數(shù)
     */
    private final long datacenterIdBits = 5L;
    /**
     * 支持的最大機(jī)器id,結(jié)果是31 (這個(gè)移位算法可以很快的計(jì)算出幾位二進(jìn)制數(shù)所能表示的最大十進(jìn)制數(shù))
     */
    private final long maxWorkerId = -1L ^ (-1L << workerIdBits);
    /**
     * 支持的最大數(shù)據(jù)標(biāo)識(shí)id,結(jié)果是31
     */
    private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits);
    /**
     * 序列在id中占的位數(shù)
     */
    private final long sequenceBits = 12L;
    /**
     * 機(jī)器ID向左移12位
     */
    private final long workerIdShift = sequenceBits;
    /**
     * 數(shù)據(jù)標(biāo)識(shí)id向左移17位(12+5)
     */
    private final long datacenterIdShift = sequenceBits + workerIdBits;
    /**
     * 時(shí)間截向左移22位(5+5+12)
     */
    private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;
    /**
     * 生成序列的掩碼,這里為4095 (0b111111111111=0xfff=4095)
     */
    private final long sequenceMask = -1L ^ (-1L << sequenceBits);
    /**
     * 工作機(jī)器ID(0~31)
     */
    private long workerId;
    /**
     * 數(shù)據(jù)中心ID(0~31)
     */
    private long datacenterId;
    /**
     * 毫秒內(nèi)序列(0~4095)
     */
    private long sequence = 0L;
    /**
     * 上次生成ID的時(shí)間截
     */
    private long lastTimestamp = -1L;
    /**
     * 構(gòu)造函數(shù)
     * @param workerId     工作ID (0~31)
     * @param datacenterId 數(shù)據(jù)中心ID (0~31)
     */
    public SnowflakeIdWorker(long workerId, long datacenterId) {
        if (workerId > maxWorkerId || workerId < 0) {
            throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId));
        }
        if (datacenterId > maxDatacenterId || datacenterId < 0) {
            throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId));
        }
        this.workerId = workerId;
        this.datacenterId = datacenterId;
    }
    /**
     * 獲得下一個(gè)ID (該方法是線程安全的)
     * @return SnowflakeId
     */
    public synchronized long nextId() {
        long timestamp = timeGen();
        // 如果當(dāng)前時(shí)間小于上一次ID生成的時(shí)間戳,說明系統(tǒng)時(shí)鐘回退過這個(gè)時(shí)候應(yīng)當(dāng)拋出異常
        if (timestamp < lastTimestamp) {
            throw new RuntimeException(
                    String.format("Clock moved backwards.  Refusing to generate id for %d milliseconds", lastTimestamp - timestamp));
        }
        // 如果是同一時(shí)間生成的,則進(jìn)行毫秒內(nèi)序列
        if (lastTimestamp == timestamp) {
            sequence = (sequence + 1) & sequenceMask;
            // 毫秒內(nèi)序列溢出
            if (sequence == 0) {
                //阻塞到下一個(gè)毫秒,獲得新的時(shí)間戳
                timestamp = tilNextMillis(lastTimestamp);
            }
        }
        // 時(shí)間戳改變,毫秒內(nèi)序列重置
        else {
            sequence = 0L;
        }
        // 上次生成ID的時(shí)間截
        lastTimestamp = timestamp;
        // 移位并通過或運(yùn)算拼到一起組成64位的ID
        return ((timestamp - twepoch) << timestampLeftShift) //
                | (datacenterId << datacenterIdShift) //
                | (workerId << workerIdShift) //
                | sequence;
    }
    /**
     * 阻塞到下一個(gè)毫秒,直到獲得新的時(shí)間戳
     * @param lastTimestamp 上次生成ID的時(shí)間截
     * @return 當(dāng)前時(shí)間戳
     */
    protected long tilNextMillis(long lastTimestamp) {
        long timestamp = timeGen();
        while (timestamp <= lastTimestamp) {
            timestamp = timeGen();
        }
        return timestamp;
    }
    /**
     * 返回以毫秒為單位的當(dāng)前時(shí)間
     * @return 當(dāng)前時(shí)間(毫秒)
     */
    protected long timeGen() {
        return System.currentTimeMillis();
    }
}

通過Common類去調(diào)用

public class Common {
    public static volatile SnowflakeIdWorker idWorker = null;
    public static synchronized SnowflakeIdWorker getIdWorker() {
        if (null == idWorker) {
            idWorker = new SnowflakeIdWorker(0, 0);
        }
        return idWorker;
    }
}

4.2、添加Mybatis-plus寫法的表操作

可以使用Mybatis代碼生成工具,逆向ClickHouse數(shù)據(jù)庫生成代碼,并復(fù)制到項(xiàng)目里面,省時(shí)省力。
不會(huì)的同學(xué)可以去學(xué)習(xí)我的【Springboot】專欄。

5、測(cè)試

增加單元測(cè)試。

package com.qhkj.clickhousedemo;
import com.alibaba.fastjson.JSON;
import com.qhkj.clickhousedemo.constant.Common;
import com.qhkj.clickhousedemo.entity.Product;
import com.qhkj.clickhousedemo.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.util.List;
@Slf4j
@SpringBootTest
public class ProductTest {
    @Resource
    private ProductService productService;
    @Test
    void saveProduct() {
        Product product = Product.builder()
                .skuId(String.valueOf(Common.getIdWorker().nextId()))
                .url("http://xxx.com/skuid=xxxx")
                .country("EN")
                .image("http://image.xxx.com/skuid=xxxx")
                .name("DIABLO 4 GOLD SEASON 5")
                .price("275美元")
                .freight("15美元運(yùn)費(fèi)")
                .build();
        productService.save(product);
        log.info("商品信息保存成功");
    }
    @Test
    void selectAll() {
        List<Product> productList = productService.selectAll();
        log.info("productList:{}", JSON.toJSONString(productList));
    }
}

5.1、新增數(shù)據(jù)

構(gòu)造一個(gè)商品類,新增一條數(shù)據(jù)并執(zhí)行

5.2、查詢數(shù)據(jù)

查詢所有數(shù)據(jù)。

結(jié)尾

本章節(jié),講解Springboot + mybatis-plus 集成ClickHouse,實(shí)現(xiàn)增加數(shù)據(jù)、查詢數(shù)據(jù)。
下一章節(jié),我們?cè)陧?xiàng)目里集成RabbitMq,使用消息隊(duì)列來接收數(shù)據(jù),并存儲(chǔ)到ClickHouse。
數(shù)據(jù)來源我們另外構(gòu)造一個(gè)系統(tǒng),使用Springboot + Jsoup解析ebay網(wǎng)數(shù)據(jù),去獲取耳機(jī)、顯卡、iPhone等類目的熱賣商品,使用RabbitMq發(fā)送數(shù)據(jù)。

到此這篇關(guān)于Springboot里集成Mybatis-plus、ClickHouse的文章就介紹到這了,更多相關(guān)Springboot集成Mybatis-plus、ClickHouse內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 淺談JSP是如何編譯成servlet并提供服務(wù)的

    淺談JSP是如何編譯成servlet并提供服務(wù)的

    JSP是Servlet的一種特殊形式,JSP頁面最終是編譯為Servlet執(zhí)行的,那么本文主要介紹了JSP是如何編譯成servlet并提供服務(wù)的,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • Java排序算法之SleepSort排序示例

    Java排序算法之SleepSort排序示例

    這篇文章主要介紹了Java排序算法之SleepSort排序,結(jié)合實(shí)例形式分析了SleepSort排序的實(shí)現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下
    2017-01-01
  • springboot的application.yml配置port不生效的解決方案

    springboot的application.yml配置port不生效的解決方案

    這篇文章主要介紹了springboot的application.yml配置port不生效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • java枚舉類的屬性、方法和構(gòu)造方法應(yīng)用實(shí)戰(zhàn)

    java枚舉類的屬性、方法和構(gòu)造方法應(yīng)用實(shí)戰(zhàn)

    這篇文章主要介紹了java枚舉類的屬性、方法和構(gòu)造方法應(yīng)用,結(jié)合實(shí)例形式分析了java枚舉類的定義、構(gòu)造及相關(guān)應(yīng)用操作技巧,需要的朋友可以參考下
    2019-08-08
  • 解決idea不顯示Services工具欄的問題

    解決idea不顯示Services工具欄的問題

    這篇文章主要介紹了解決idea不顯示Services工具欄的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • SpringBoot本地磁盤映射問題

    SpringBoot本地磁盤映射問題

    這篇文章主要介紹了SpringBoot本地磁盤映射問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • idea如何設(shè)置Git忽略對(duì)某些文件或文件夾的版本追蹤

    idea如何設(shè)置Git忽略對(duì)某些文件或文件夾的版本追蹤

    這篇文章主要介紹了idea如何設(shè)置Git忽略對(duì)某些文件或文件夾的版本追蹤問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • maven 指定version不生效的問題

    maven 指定version不生效的問題

    這篇文章主要介紹了maven 指定version不生效的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • 通過Java修改游戲存檔的實(shí)現(xiàn)思路

    通過Java修改游戲存檔的實(shí)現(xiàn)思路

    這篇文章主要介紹了通過Java修改游戲存檔的實(shí)現(xiàn)思路,實(shí)現(xiàn)方法也很簡單,因?yàn)橹参锎髴?zhàn)僵尸游戲的數(shù)據(jù)文件存儲(chǔ)在本地的存儲(chǔ)位置是已知的,因此我們可以將實(shí)現(xiàn)過程拆分為三個(gè)步驟,需要的朋友可以參考下
    2021-10-10
  • javaweb在線支付功能實(shí)現(xiàn)代碼

    javaweb在線支付功能實(shí)現(xiàn)代碼

    這篇文章主要為大家詳細(xì)介紹了javaweb在線支付功能的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-04-04

最新評(píng)論