Springboot集成Mybatis-plus、ClickHouse實(shí)現(xiàn)增加數(shù)據(jù)、查詢數(shù)據(jù)功能
前言
上一章節(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),語(yǔ)言級(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ù)庫(kù)表創(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í)間戳,說(shuō)明系統(tǒng)時(shí)鐘回退過(guò)這個(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;
// 移位并通過(guò)或運(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();
}
}通過(guò)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ù)庫(kù)生成代碼,并復(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ì)列來(lái)接收數(shù)據(jù),并存儲(chǔ)到ClickHouse。
數(shù)據(jù)來(lái)源我們另外構(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)文章希望大家以后多多支持腳本之家!
- SpringBoot同時(shí)集成Mybatis和Mybatis-plus框架
- SpringBoot+MyBatis-Plus實(shí)現(xiàn)分頁(yè)示例
- Springboot整合mybatis-plus使用pageHelper進(jìn)行分頁(yè)(使用步驟)
- SpringBoot+MyBatis-Plus實(shí)現(xiàn)分頁(yè)的項(xiàng)目實(shí)踐
- springboot集成mybatis-plus全過(guò)程
- springboot項(xiàng)目中mybatis-plus@Mapper注入失敗問(wèn)題
- springboot+mybatis-plus實(shí)現(xiàn)自動(dòng)建表的示例
- SpringBoot中使用MyBatis-Plus實(shí)現(xiàn)分頁(yè)接口的詳細(xì)教程
- SpringBoot?mybatis-plus使用json字段實(shí)戰(zhàn)指南
- springboot3.2整合mybatis-plus詳細(xì)代碼示例
- 全網(wǎng)最新springboot整合mybatis-plus的過(guò)程
相關(guān)文章
springboot接入deepseek深度求索代碼示例(java版)
這篇文章主要介紹了springboot接入deepseek深度求索的相關(guān)資料,包括創(chuàng)建APIKey,封裝詢問(wèn)工具方法,傳入問(wèn)題,調(diào)用方法,但發(fā)現(xiàn)只能回答簡(jiǎn)單問(wèn)題,需要的朋友可以參考下2025-01-01
mybatis配置Mapper.xml文件時(shí)遇到的問(wèn)題及解決
這篇文章主要介紹了mybatis配置Mapper.xml文件時(shí)遇到的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
解決Spring Security 用戶帳號(hào)已被鎖定問(wèn)題
這篇文章主要介紹了解決Spring Security 用戶帳號(hào)已被鎖定問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
Java重入鎖(ReentrantLock)從入門到源碼深度解析
本文給大家介紹Java重入鎖(ReentrantLock)從入門到源碼深度解析,通過(guò)本文學(xué)習(xí)可以全方位地認(rèn)識(shí)ReentrantLock,從基本概念到高級(jí)特性,從使用方式到源碼剖析,從底層原理到實(shí)際應(yīng)用,感興趣的朋友跟隨小編一起看看吧2026-02-02
JAVA使用POI(XSSFWORKBOOK)讀取EXCEL文件過(guò)程解析
這篇文章主要介紹了JAVA使用POI(XSSFWORKBOOK)讀取EXCEL文件過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
springboot中@RestController注解實(shí)現(xiàn)
在JavaWeb開發(fā)中,Spring框架及其組件SpringMVC因高效和強(qiáng)大功能而廣受歡迎,@RestController注解是SpringMVC中的重要組成部分,下面就來(lái)介紹一下,感興趣的可以了解一下2024-09-09

