Springboot集成Mybatis-plus、ClickHouse實現(xiàn)增加數(shù)據(jù)、查詢數(shù)據(jù)功能
前言
上一章節(jié)講解在阿里云ECS centos服務(wù)器上安裝ClickHouse。
這一章節(jié)我們在Springboot里集成Mybatis-plus、ClickHouse。
環(huán)境:JDK8 + Springboot 2.6.13 + ClickHouse
1、構(gòu)建JDK8 + Springboot 2.6.13項目
JDK8 + Springboot 2.x基本上都可以,保險起見,2.5-2.7左右最好。
1.1、修改Server URL,支持Java8
在Idea里創(chuàng)建一個Springboot項目,首先修改Server URL,默認的Server URL已經(jīng)不支持JDK8。
1.2、 選擇Springboot 版本、選擇加載的依賴包
1.3、查看pom.xml文件
構(gòu)建完成之后,就會生成一個Springboot項目,文件里最主要是pom.xml文件。
1.4、檢查項目結(jié)構(gòu)
1.4.1、檢查項目設(shè)置
與我們的項目里選擇的JDK8保持一致
1.4.2、檢查模塊
檢查項目結(jié)構(gòu),語言級別、Sources、Resources、Test Resources等。
1.5、檢查項目配置
檢查項目的Settings。
1.5.1、配置Maven環(huán)境
(JDK8 對應(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也加入進去。
<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、修改配置文件
把默認的application.properties文件修改為 application.yaml文件
spring: application: name: clickhouse-project datasource: type: com.zaxxer.hikari.HikariDataSource url: jdbc:clickhouse://clickhouse遠程主機: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 #開啟二級緩存 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 控制臺日志 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項目里創(chuàng)建商品表的操作類
4.1、添加雪花ID實現(xiàn)
我們這次采用雪花ID生成商品SkuId,所以添加雪花ID實現(xiàn)
public class SnowflakeIdWorker { /** * 開始時間截 (2015-01-01) */ private final long twepoch = 1420041600000L; /** * 機器id所占的位數(shù) */ private final long workerIdBits = 5L; /** * 數(shù)據(jù)標識id所占的位數(shù) */ private final long datacenterIdBits = 5L; /** * 支持的最大機器id,結(jié)果是31 (這個移位算法可以很快的計算出幾位二進制數(shù)所能表示的最大十進制數(shù)) */ private final long maxWorkerId = -1L ^ (-1L << workerIdBits); /** * 支持的最大數(shù)據(jù)標識id,結(jié)果是31 */ private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); /** * 序列在id中占的位數(shù) */ private final long sequenceBits = 12L; /** * 機器ID向左移12位 */ private final long workerIdShift = sequenceBits; /** * 數(shù)據(jù)標識id向左移17位(12+5) */ private final long datacenterIdShift = sequenceBits + workerIdBits; /** * 時間截向左移22位(5+5+12) */ private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; /** * 生成序列的掩碼,這里為4095 (0b111111111111=0xfff=4095) */ private final long sequenceMask = -1L ^ (-1L << sequenceBits); /** * 工作機器ID(0~31) */ private long workerId; /** * 數(shù)據(jù)中心ID(0~31) */ private long datacenterId; /** * 毫秒內(nèi)序列(0~4095) */ private long sequence = 0L; /** * 上次生成ID的時間截 */ 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; } /** * 獲得下一個ID (該方法是線程安全的) * @return SnowflakeId */ public synchronized long nextId() { long timestamp = timeGen(); // 如果當前時間小于上一次ID生成的時間戳,說明系統(tǒng)時鐘回退過這個時候應(yīng)當拋出異常 if (timestamp < lastTimestamp) { throw new RuntimeException( String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } // 如果是同一時間生成的,則進行毫秒內(nèi)序列 if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; // 毫秒內(nèi)序列溢出 if (sequence == 0) { //阻塞到下一個毫秒,獲得新的時間戳 timestamp = tilNextMillis(lastTimestamp); } } // 時間戳改變,毫秒內(nèi)序列重置 else { sequence = 0L; } // 上次生成ID的時間截 lastTimestamp = timestamp; // 移位并通過或運算拼到一起組成64位的ID return ((timestamp - twepoch) << timestampLeftShift) // | (datacenterId << datacenterIdShift) // | (workerId << workerIdShift) // | sequence; } /** * 阻塞到下一個毫秒,直到獲得新的時間戳 * @param lastTimestamp 上次生成ID的時間截 * @return 當前時間戳 */ protected long tilNextMillis(long lastTimestamp) { long timestamp = timeGen(); while (timestamp <= lastTimestamp) { timestamp = timeGen(); } return timestamp; } /** * 返回以毫秒為單位的當前時間 * @return 當前時間(毫秒) */ 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ù)制到項目里面,省時省力。
不會的同學可以去學習我的【Springboot】專欄。
5、測試
增加單元測試。
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美元運費") .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)造一個商品類,新增一條數(shù)據(jù)并執(zhí)行
5.2、查詢數(shù)據(jù)
查詢所有數(shù)據(jù)。
結(jié)尾
本章節(jié),講解Springboot + mybatis-plus 集成ClickHouse,實現(xiàn)增加數(shù)據(jù)、查詢數(shù)據(jù)。
下一章節(jié),我們在項目里集成RabbitMq,使用消息隊列來接收數(shù)據(jù),并存儲到ClickHouse。
數(shù)據(jù)來源我們另外構(gòu)造一個系統(tǒng),使用Springboot + Jsoup解析ebay網(wǎng)數(shù)據(jù),去獲取耳機、顯卡、iPhone等類目的熱賣商品,使用RabbitMq發(fā)送數(shù)據(jù)。
到此這篇關(guān)于Springboot里集成Mybatis-plus、ClickHouse的文章就介紹到這了,更多相關(guān)Springboot集成Mybatis-plus、ClickHouse內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot同時集成Mybatis和Mybatis-plus框架
- SpringBoot+MyBatis-Plus實現(xiàn)分頁示例
- Springboot整合mybatis-plus使用pageHelper進行分頁(使用步驟)
- SpringBoot+MyBatis-Plus實現(xiàn)分頁的項目實踐
- springboot集成mybatis-plus全過程
- springboot項目中mybatis-plus@Mapper注入失敗問題
- springboot+mybatis-plus實現(xiàn)自動建表的示例
- SpringBoot中使用MyBatis-Plus實現(xiàn)分頁接口的詳細教程
- SpringBoot?mybatis-plus使用json字段實戰(zhàn)指南
- springboot3.2整合mybatis-plus詳細代碼示例
- 全網(wǎng)最新springboot整合mybatis-plus的過程
相關(guān)文章
springboot的application.yml配置port不生效的解決方案
這篇文章主要介紹了springboot的application.yml配置port不生效的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07java枚舉類的屬性、方法和構(gòu)造方法應(yīng)用實戰(zhàn)
這篇文章主要介紹了java枚舉類的屬性、方法和構(gòu)造方法應(yīng)用,結(jié)合實例形式分析了java枚舉類的定義、構(gòu)造及相關(guān)應(yīng)用操作技巧,需要的朋友可以參考下2019-08-08idea如何設(shè)置Git忽略對某些文件或文件夾的版本追蹤
這篇文章主要介紹了idea如何設(shè)置Git忽略對某些文件或文件夾的版本追蹤問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03