gpt4 book ai didi

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

转载 作者:qq735679552 更新时间:2022-09-29 22:32:09 24 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章springboot+mybatis配置clickhouse实现插入查询功能由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

说明 。

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

1.添加maven依赖 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<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、配属数据源 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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、参数配置 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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连接池配置 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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接口 。

?
1
2
3
4
5
6
7
@Mapper
public interface UserMapper {
 
  List<User> queryUser();
 
  Integer insertUser(User user);
}

7.controller接口 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@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表,然后插入几条数据测试一下 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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内容请搜索我以前的文章或继续浏览下面的相关文章希望大家以后多多支持我! 。

原文链接:https://blog.csdn.net/weixin_39025362/article/details/107715706 。

最后此篇关于springboot+mybatis配置clickhouse实现插入查询功能的文章就讲到这里了,如果你想了解更多关于springboot+mybatis配置clickhouse实现插入查询功能的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com