使⽤ideaspringboot操作clickhouse数据库基于⼤数据分析,本案例为测试案例,真实⼯程基于此原型设计,下述⽂章是安装过程
1.pom配置核⼼配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ckhouse</groupId>
<artifactId>springclickhouse</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springclickhouse</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
springboot aop<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&batis.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数据库 -->
<!-- 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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>
</plugins>
</build>
</project>
2.层级设计
l
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource clickhouse:
driverClassName: ru.yandex.clickhouse.ClickHouseDriver  url: jdbc:clickhouse://192.168.2.243:8123/default
password: 123456
initialSize: 10
maxActive: 100
minIdle: 10
maxWait: 6000
mybatis:
mapper-locations: classpath:mapper/*l
type-aliases-package: com.ckhouse.springclickhouse.pojo server:
port: 8090
4.ChParam
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;
}
}
5.DruidConfig
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import t.annotation.Bean;
import t.annotation.Configuration;
import javax.sql.DataSource;
@Configuration
public class DruidConfig {
@Autowired
private ChParam chParam;
@Bean
public DataSource dataSource() {
DruidDataSource datasource = new DruidDataSource();
datasource.Url());
datasource.DriverClassName());
datasource.InitialSize());
datasource.MinIdle());
datasource.MaxActive());
datasource.MaxWait());
datasource.Password());
return datasource;
}
}
6.UserMapper
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-////DTD Mapper 3.0//EN"
"/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ckhouse.springclickhouse.Dao.UserMapper">
<select id="queryUser" resultType="com.ckhouse.springclickhouse.pojo.User">
select userId, appId, version, regTime from `default`.`user`
</select>
<insert id="insertUser" parameterType="com.ckhouse.springclickhouse.pojo.User">        INSERT INTO `default`.`user` (`userId`, `appId`, `version`, `regTime`)
VALUES (#{userId}, #{appId}, #{version}, #{regTime})
</insert>
</mapper>
7.UserController
import com.ckhouse.springclickhouse.Dao.UserMapper;
import com.ckhouse.springclickhouse.pojo.User;
slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
import java.util.List;
@Slf4j
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@RequestMapping("/queryUser")
public Object query(){
List userList = userMapper.queryUser();
log.String());
String();
}
}
}
8.UserMapper
import com.ckhouse.springclickhouse.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserMapper {
List queryUser();
Integer insertUser(User user);
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。