springboot+mybatis配置clickhouse实现插入查询功能
说明
ClickHouse是一款用于大数据实时分析的列式数据库管理系统,在大数据量查询时有着非常优秀的性能,
但是也有缺点,就是不支持事务,不支持真正的删除/更新,所以笔者只演示插入和查询。
1.添加maven依赖
org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-aop org.springframework.boot spring-boot-starter-test org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.2 com.alibaba druid-spring-boot-starter 1.1.13 commons-lang commons-lang 2.6 ru.yandex.clickhouse clickhouse-jdbc 0.1.53 org.projectlombok lombok true
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、参数配置
importlombok.Data;
importorg.springframework.beans.factory.annotation.Value;
importorg.springframework.stereotype.Component;
@Data
@Component
publicclassChParam{
privateStringdriverClassName;
privateStringurl;
privateStringpassword;
privateIntegerinitialSize;
privateIntegermaxActive;
privateIntegerminIdle;
privateIntegermaxWait;
@Value("${clickhouse.driverClassName}")
publicvoidsetDriverClassName(StringdriverClassName){
this.driverClassName=driverClassName;
}
@Value("${clickhouse.url}")
publicvoidsetUrl(Stringurl){
this.url=url;
}
@Value("${clickhouse.password}")
publicvoidsetPassword(Stringpassword){
this.password=password;
}
@Value("${clickhouse.initialSize}")
publicvoidsetInitialSize(IntegerinitialSize){
this.initialSize=initialSize;
}
@Value("${clickhouse.maxActive}")
publicvoidsetMaxActive(IntegermaxActive){
this.maxActive=maxActive;
}
@Value("${clickhouse.minIdle}")
publicvoidsetMinIdle(IntegerminIdle){
this.minIdle=minIdle;
}
@Value("${clickhouse.maxWait}")
publicvoidsetMaxWait(IntegermaxWait){
this.maxWait=maxWait;
}
}
4、Druid连接池配置
importcom.alibaba.druid.pool.DruidDataSource;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
importjavax.sql.DataSource;
@Configuration
publicclassDruidConfig{
@Autowired
privateChParamchParam;
@Bean
publicDataSourcedataSource(){
DruidDataSourcedatasource=newDruidDataSource();
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());
returndatasource;
}
}
5、Mapper.xml
selectuserId,appId,version,regTimefrom`default`.`user` INSERTINTO`default`.`user`(`userId`,`appId`,`version`,`regTime`) VALUES(#{userId},#{appId},#{version},#{regTime})
6、Mapper接口
@Mapper
publicinterfaceUserMapper{
ListqueryUser();
IntegerinsertUser(Useruser);
}
7.controller接口
@Slf4j
@RestController
publicclassUserController{
@Autowired
privateUserMapperuserMapper;
@RequestMapping("/queryUser")
publicObjectquery(){
ListuserList=userMapper.queryUser();
log.info(userList.toString());
returnuserList.toString();
}
@RequestMapping("/insertUser")
publicObjectinsertUser(){
Useruser=newUser();
user.setAppId("SS");
user.setRegTime(newDate());
user.setUserId(777744);
user.setVersion("3.2");
Integerflag=userMapper.insertUser(user);
returnflag;
}
}
8.创建一个clickhouse表,然后插入几条数据测试一下
createtableuser ( userIdInt32, appIdString, versionString, regTimeDate ) engine=MergeTreePARTITIONBYtoYYYYMM(regTime)ORDERBYuserIdSETTINGSindex_granularity=8192; INSERTINTOdefault.user(userId,appId,version,regTime)VALUES(123457,'RR','3.6','2020-01-07'); INSERTINTOdefault.user(userId,appId,version,regTime)VALUES(43234,'HH','2.5','2020-06-06'); INSERTINTOdefault.user(userId,appId,version,regTime)VALUES(1234,'TT','2.4','2020-07-24'); INSERTINTOdefault.user(userId,appId,version,regTime)VALUES(12345,'RR','2.5','2020-07-29'); INSERTINTOdefault.user(userId,appId,version,regTime)VALUES(123456,'TT','2.1','2020-07-09'); INSERTINTOdefault.user(userId,appId,version,regTime)VALUES(234561,'GG','3.0','2020-07-31');
9.测试
参考文章:SpringBoot2整合ClickHouse数据库案例解析
总结
到此这篇关于springboot+mybatis配置clickhouse实现插入查询功能的文章就介绍到这了,更多相关springboot+mybatis配置clickhouse内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。