欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

springboot+mybatis配置clickhouse实现插入查询功能

程序员文章站 2022-06-26 12:49:58
说明clickhouse 是一款用于大数据实时分析的列式数据库管理系统,在大数据量查询时有着非常优秀的性能,但是也有缺点,就是不支持事务,不支持真正的删除 / 更新,所以笔者只演示插入和查询。1.添加...

说明

clickhouse 是一款用于大数据实时分析的列式数据库管理系统,在大数据量查询时有着非常优秀的性能,
但是也有缺点,就是不支持事务,不支持真正的删除 / 更新,所以笔者只演示插入和查询。

1.添加maven依赖

<dependency>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-starter-web</artifactid>
 </dependency>
 <dependency>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-starter-aop</artifactid>
 </dependency>
 <dependency>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-starter-test</artifactid>
 </dependency>
 <dependency>
  <groupid>org.mybatis.spring.boot</groupid>
  <artifactid>mybatis-spring-boot-starter</artifactid>
  <version>1.3.2</version>
 </dependency>
 <dependency>
  <groupid>com.alibaba</groupid>
  <artifactid>druid-spring-boot-starter</artifactid>
  <version>1.1.13</version>
 </dependency>
 <dependency>
  <groupid>commons-lang</groupid>
  <artifactid>commons-lang</artifactid>
  <version>2.6</version>
 </dependency>
 <!-- clickhouse数据库 -->
 <dependency>
  <groupid>ru.yandex.clickhouse</groupid>
  <artifactid>clickhouse-jdbc</artifactid>
  <version>0.1.53</version>
 </dependency>
 <dependency>
  <groupid>org.projectlombok</groupid>
  <artifactid>lombok</artifactid>
  <optional>true</optional>
 </dependency>

2、配属数据源

spring:
 datasource:
 type: com.alibaba.druid.pool.druiddatasource
clickhouse:
 driverclassname: ru.yandex.clickhouse.clickhousedriver
 url: jdbc:clickhouse://106.55.55.249:8123/default
 password: ck的密码
 initialsize: 10
 maxactive: 100
 minidle: 10
 maxwait: 6000
mybatis:
 mapper-locations: classpath:mapper/*mapper.xml
 type-aliases-package: com.wyu.tt16clickhouse.entity
server:
 port: 8090

3、参数配置

import lombok.data;
import org.springframework.beans.factory.annotation.value;
import org.springframework.stereotype.component;

@data
@component
public class chparam {
 private string driverclassname;
 private string url;
 private string password;
 private integer initialsize;
 private integer maxactive;
 private integer minidle;
 private integer maxwait;

 @value("${clickhouse.driverclassname}")
 public void setdriverclassname(string driverclassname) {
 this.driverclassname = driverclassname;
 }

 @value("${clickhouse.url}")
 public void seturl(string url) {
 this.url = url;
 }

 @value("${clickhouse.password}")
 public void setpassword(string password) {
 this.password = password;
 }

 @value("${clickhouse.initialsize}")
 public void setinitialsize(integer initialsize) {
 this.initialsize = initialsize;
 }

 @value("${clickhouse.maxactive}")
 public void setmaxactive(integer maxactive) {
 this.maxactive = maxactive;
 }

 @value("${clickhouse.minidle}")
 public void setminidle(integer minidle) {
 this.minidle = minidle;
 }

 @value("${clickhouse.maxwait}")
 public void setmaxwait(integer maxwait) {
 this.maxwait = maxwait;
 }
}

4、druid连接池配置

import com.alibaba.druid.pool.druiddatasource;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;

import javax.sql.datasource;

@configuration
public class druidconfig {

 @autowired
 private chparam chparam;

 @bean
 public datasource datasource() {
 druiddatasource datasource = new druiddatasource();
 datasource.seturl(chparam.geturl());
 datasource.setdriverclassname(chparam.getdriverclassname());
 datasource.setinitialsize(chparam.getinitialsize());
 datasource.setminidle(chparam.getminidle());
 datasource.setmaxactive(chparam.getmaxactive());
 datasource.setmaxwait(chparam.getmaxwait());
 datasource.setpassword(chparam.getpassword());
 return datasource;
 }
}

5、mapper.xml

<?xml version="1.0" encoding="utf-8"?>
<!doctype mapper public "-//mybatis.org//dtd mapper 3.0//en" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.wyu.tt16clickhouse.usermapper">

 <select id="queryuser" resulttype="com.wyu.tt16clickhouse.entity.user">
 select userid, appid, version, regtime from `default`.`user`
 </select>

 <insert id="insertuser" parametertype="com.wyu.tt16clickhouse.entity.user">
  insert into `default`.`user` (`userid`, `appid`, `version`, `regtime`)
  values (#{userid}, #{appid}, #{version}, #{regtime})
 </insert>
</mapper>

6、mapper接口

@mapper
public interface usermapper {

 list<user> queryuser();

 integer insertuser(user user);
}

7.controller接口

@slf4j
@restcontroller
public class usercontroller {

 @autowired
 private usermapper usermapper;

 @requestmapping("/queryuser")
 public object query(){
 list userlist = usermapper.queryuser();
 log.info(userlist.tostring());
 return userlist.tostring();
 }

 @requestmapping("/insertuser")
 public object insertuser(){
 user user = new user();
 user.setappid("ss");
 user.setregtime(new date());
 user.setuserid(777744);
 user.setversion("3.2");
 integer flag = usermapper.insertuser(user);
 return flag;
 }
}

8.创建一个clickhouse表,然后插入几条数据测试一下

create table user
(
 userid int32,
 appid string,
 version string,
 regtime date
)
 engine = mergetree partition by toyyyymm(regtime) order by userid settings index_granularity = 8192;
 
insert into default.user (userid, appid, version, regtime) values (123457, 'rr', '3.6', '2020-01-07');
insert into default.user (userid, appid, version, regtime) values (43234, 'hh', '2.5', '2020-06-06');
insert into default.user (userid, appid, version, regtime) values (1234, 'tt', '2.4', '2020-07-24');
insert into default.user (userid, appid, version, regtime) values (12345, 'rr', '2.5', '2020-07-29');
insert into default.user (userid, appid, version, regtime) values (123456, 'tt', '2.1', '2020-07-09');
insert into default.user (userid, appid, version, regtime) values (234561, 'gg', '3.0', '2020-07-31'); 

9.测试

springboot+mybatis配置clickhouse实现插入查询功能

参考文章:springboot2 整合 clickhouse数据库案例解析

总结

到此这篇关于springboot+mybatis配置clickhouse实现插入查询功能的文章就介绍到这了,更多相关springboot+mybatis配置clickhouse内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!