MybatisPlus3.3.1整合clickhouse的過(guò)程
前言ClickHouse是俄羅斯Yandex發(fā)布的一款數(shù)據(jù)分析型數(shù)據(jù)庫(kù)支持sql語(yǔ)法,詳情可以訪問(wèn)官網(wǎng),目前網(wǎng)上還沒(méi)有MybatisPlus整合clickhouse文章發(fā)布故此寫(xiě)一遍博文記錄整理一下整個(gè)過(guò)程
完整工程已提交至碼云:https://gitee.com/yankangkk/watchmen
關(guān)于大家在評(píng)論區(qū)經(jīng)常留言關(guān)于分頁(yè)的問(wèn)題,其實(shí)在之前的朋友已經(jīng)有了好的解決辦法,下面附上截圖,供大家參考。
連接池部分用的是阿里的druid下面是數(shù)據(jù)庫(kù)連接的配置類(lèi)
import javax.annotation.Resource; import javax.sql.DataSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.alibaba.druid.pool.DruidDataSource; /** * * @author kk * Druid數(shù)據(jù)庫(kù)連接池配置 */ @Configuration public class DruidConfig { @Resource private JdbcParamConfig jdbcParamConfig; @Bean public DataSource dataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl(jdbcParamConfig.getUrl()); dataSource.setDriverClassName(jdbcParamConfig.getDriverClassName()); dataSource.setInitialSize(jdbcParamConfig.getInitialSize()); dataSource.setMinIdle(jdbcParamConfig.getMinIdle()); dataSource.setMaxActive(jdbcParamConfig.getMaxActive()); dataSource.setMaxWait(jdbcParamConfig.getMaxWait()); return dataSource; } }
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import lombok.Data; /** * @author kk * clickhouse連接信息配置 */ @Data @Component @ConfigurationProperties(prefix = "spring.datasource.click") public class JdbcParamConfig { private String driverClassName; private String url ; private Integer initialSize ; private Integer maxActive ; private Integer minIdle ; private Integer maxWait ; }
分頁(yè)插件配置
import java.util.Properties; import org.springframework.context.annotation.Bean; import com.github.pagehelper.PageHelper; /** * * @author kk * MybatisPlus相關(guān)配置 */ @Configuration public class MybatisPlusConfig { @Bean public PageHelper pageHelper() { PageHelper pageHelper = new PageHelper(); Properties properties = new Properties(); properties.setProperty("offsetAsPageNum", "true"); properties.setProperty("rowBoundsWithCount", "true"); properties.setProperty("reasonable", "true"); pageHelper.setProperties(properties); return pageHelper; } }
實(shí)體類(lèi)對(duì)應(yīng)clickhouse中的表
import java.util.Date; import com.baomidou.mybatisplus.annotation.TableName; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; /** * * @author kk * 實(shí)體類(lèi) */ @TableName("test_table") @Data @NoArgsConstructor @AllArgsConstructor @Builder public class TestTableEntity { private Long id; private String name; private String value; private Date createDate; private Object array; }
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.watchmen.clickhouse.entity.TestTableEntity; public interface TestTableMapper extends BaseMapper<TestTableEntity> { }
import org.springframework.stereotype.Service; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.IService; import com.watchmen.clickhouse.entity.TestTableEntity; public interface TestTableService extends IService<TestTableEntity>{ /** * 分頁(yè)查詢 * @param page 第幾頁(yè) * @param pageSize 每頁(yè)條數(shù) * @return Page */ Page<TestTableEntity> list(Integer page, Integer pageSize); }
import org.springframework.stereotype.Service; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.watchmen.clickhouse.entity.TestTableEntity; import com.watchmen.clickhouse.mapper.TestTableMapper; import com.watchmen.clickhouse.service.TestTableService; @Service public class TestTableServiceImpl extends ServiceImpl<TestTableMapper,TestTableEntity> implements TestTableService { @Override public Page<TestTableEntity> list(Integer page, Integer pageSize) { return this.page(new Page<TestTableEntity>(page,pageSize), new QueryWrapper<TestTableEntity>()); } }
啟動(dòng)類(lèi)加上掃描注解
import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @MapperScan("com.watchmen.clickhouse.mapper") @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
application.yml配置 106.12.154.174是我在百度云上搭建的clickhouse搭建可以直接連接測(cè)試使用
spring: datasource: type: com.alibaba.druid.pool.DruidDataSource click: driverClassName: ru.yandex.clickhouse.ClickHouseDriver url: jdbc:clickhouse://106.12.154.174:8123/default?max_result_bytes=10000 username: root paswword: initialSize: 10 maxActive: 100 minIdle: 10 maxWait: 6000
至此整合就已經(jīng)完成了寫(xiě)一個(gè) 路由層測(cè)試一下
import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.watchmen.clickhouse.entity.TestTableEntity; import com.watchmen.clickhouse.service.TestTableService; /** * * @author kk * Clickhouse增刪改查測(cè)試路由 */ @RestController @RequestMapping("/clickhouse") public class ClickhouseTest { @Autowired TestTableService testTableService; /** * 分頁(yè)查詢 * @return */ @GetMapping("/list") public Object list(@RequestParam(value = "page",defaultValue = "1") Integer page, @RequestParam(value = "page_size",defaultValue = "10") Integer pageSize) { List<TestTableEntity> list = testTableService.list(); System.out.println(list); return testTableService.list(page, pageSize); } }
測(cè)試表sql腳本
CREATE TABLE default.test_table ( `id` UInt16, `name` String, `value` String, `create_date` Date, `array` Array(String) ) ENGINE = MergeTree(create_date, id, 8192)
經(jīng)過(guò)測(cè)試我發(fā)現(xiàn)pagehelper和mybatis-plsu都不能正確識(shí)別clickhouse數(shù)據(jù),只能自己寫(xiě)分頁(yè)語(yǔ)句,clickhouse的刪除語(yǔ)句也比較特殊這里一并寫(xiě)了出來(lái),官方的建議還是批量刪除,雖然它支持單條刪除,代碼如下:
package com.watchmen.clickhouse.mapper; import java.util.List; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Select; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.watchmen.clickhouse.entity.TestTableEntity; public interface TestTableMapper extends BaseMapper<TestTableEntity> { /** * 分頁(yè)查詢 * @param page * @param pageSize * @return */ @Select("select * from test_table tt limit #{page}, #{pageSize}") List<TestTableEntity> selectPages(Integer page, Integer pageSize); /** * @author kk * 按id數(shù)組數(shù)據(jù)刪除數(shù)據(jù) */ @Delete("ALTER TABLE test_table DELETE WHERE id = #{id}") void deleteById(Integer id); }
項(xiàng)目也集成了knife4j可以直接調(diào)試
百度云的knife4j是:http://106.12.154.174:8080/doc.html#/home可以直接調(diào)試
pom的jar包依賴
<!-- 數(shù)據(jù)庫(kù)相關(guān) --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>${mybatis-plsu.version}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>${druid.version}</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>${pagehelper.version}</version> </dependency> <!-- sql性能分析插件 --> <dependency> <groupId>p6spy</groupId> <artifactId>p6spy</artifactId> <version>${p6spy.version}</version> </dependency> <!-- clickhouse-jdbc驅(qū)動(dòng) --> <dependency> <groupId>ru.yandex.clickhouse</groupId> <artifactId>clickhouse-jdbc</artifactId> <version>${clickhouse-jdbc.version}</version> </dependency>
到此這篇關(guān)于MybatisPlus3.3.1整合clickhouse的過(guò)程的文章就介紹到這了,更多相關(guān)MybatisPlus整合clickhouse內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java+Freemarker實(shí)現(xiàn)根據(jù)XML模板文件生成Word文檔
這篇文章主要為大家詳細(xì)介紹了Java如何使用Freemarker實(shí)現(xiàn)根據(jù)XML模板文件生成Word文檔,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以學(xué)習(xí)一下2023-11-11Java并發(fā)編程之性能、擴(kuò)展性和響應(yīng)
這篇文章主要介紹了Java并發(fā)編程之性能、擴(kuò)展性和響應(yīng),重點(diǎn)在于多線程應(yīng)用程序的性能問(wèn)題,給性能和擴(kuò)展性下一個(gè)定義,然后再仔細(xì)學(xué)習(xí)一下Amdahl法則,感興趣的小伙伴們可以參考一下2016-02-02解決JPA?save()方法null值覆蓋掉mysql預(yù)設(shè)的默認(rèn)值問(wèn)題
這篇文章主要介紹了解決JPA?save()方法null值覆蓋掉mysql預(yù)設(shè)的默認(rèn)值問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11創(chuàng)建SpringBoot工程并集成Mybatis的方法
這篇文章主要介紹了創(chuàng)建SpringBoot工程并集成Mybatis,需要的朋友可以參考下2018-06-06詳解SpringBoot中@ConditionalOnClass注解的使用
這篇文章主要和大家詳細(xì)介紹一下springboot中@ConditionalOnClass注解的用法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-08-08Java連接mysql數(shù)據(jù)庫(kù)以及mysql驅(qū)動(dòng)jar包下載和使用方法
這篇文章主要給大家介紹了關(guān)于Java連接mysql數(shù)據(jù)庫(kù)以及mysql驅(qū)動(dòng)jar包下載和使用方法,MySQL是一款常用的關(guān)系型數(shù)據(jù)庫(kù),它的JDBC驅(qū)動(dòng)程序使得我們可以通過(guò)Java程序連接MySQL數(shù)據(jù)庫(kù)進(jìn)行數(shù)據(jù)操作,需要的朋友可以參考下2023-11-11