1、采用springboot与clickhouse结合,其实和操作mysql,oracle区别不大。直接上代码开干
2、所采用的环境
jdk1.8
springboot 2.7.10
clickhouse 22.8.3.13
clickhouse 0.5.0
3、项目的pom.xml文件
<dependency>
<groupId>com.clickhouse</groupId>
<artifactId>clickhouse-jdbc</artifactId>
<version>0.5.0</version>
</dependency>
<dependency>
<groupId>com.clickhouse</groupId>
<artifactId>clickhouse-client</artifactId>
<version>0.5.0</version>
</dependency>
<dependency>
<groupId>com.clickhouse</groupId>
<artifactId>clickhouse-http-client</artifactId>
<version>0.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5-fluent</artifactId>
<version>5.1.3</version>
</dependency>
4、application.yml配置
spring:
application:
name: demobigdata
profiles:
active: mybatis
datasource:
type: com.alibaba.druid.pool.DruidDataSource
druid:
# 数据源 clickhouse
clickhouse:
driverClassName: com.clickhouse.jdbc.ClickHouseDriver
url: jdbc:clickhouse://192.168.42.142:8123/bigdata
username: default
password: 123456
initialSize: 10
maxActive: 100
minIdle: 10
maxWait: 6000
5、application-mybatis.yml配置
#mybatis的相关配置
mybatis:
#mapper配置文件
type-aliases-package: org.demoflowable.pojo
mapper-locations: classpath:META-INF/mapper/clickhouse/*.xml
config-location: classpath:META-INF/spring/mybatis-config.xml
6、ClickHouseJdbcParamConfig
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "spring.datasource.druid.clickhouse")
public class ClickHouseJdbcParamConfig {
private String driverClassName;
private String url;
private Integer initialSize;
private Integer maxActive;
private Integer minIdle;
private Integer maxWait;
private String username;
private String password;
public String getDriverClassName() {
return driverClassName;
}
public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public Integer getInitialSize() {
return initialSize;
}
public void setInitialSize(Integer initialSize) {
this.initialSize = initialSize;
}
public Integer getMaxActive() {
return maxActive;
}
public void setMaxActive(Integer maxActive) {
this.maxActive = maxActive;
}
public Integer getMinIdle() {
return minIdle;
}
public void setMinIdle(Integer minIdle) {
this.minIdle = minIdle;
}
public Integer getMaxWait() {
return maxWait;
}
public void setMaxWait(Integer maxWait) {
this.maxWait = maxWait;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
7、ClickHouseConfig
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.alibaba.druid.pool.DruidDataSource;
@Configuration
public class ClickHouseConfig {
@javax.annotation.Resource
private ClickHouseJdbcParamConfig 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());
datasource.setUsername(jdbcParamConfig.getUsername());
datasource.setPassword(jdbcParamConfig.getPassword());
return datasource;
}
}
8、UserInfo
public class UserInfo {
private Integer id;
private String userName;
private String passWord;
private String phone;
private String createDay;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getCreateDay() {
return createDay;
}
public void setCreateDay(String createDay) {
this.createDay = createDay;
}
}
9、UserInfoMapper
import java.util.List;
import org.apache.ibatis.annotations.Param;
import org.demoflowable.pojo.user.po.UserInfo;
public interface UserInfoMapper {
void saveData(UserInfo userInfo);
UserInfo selectById(@Param("id") Integer id);
List<UserInfo> selectList();
}
10、UserInfoService
import java.util.List;
import javax.annotation.Resource;
import org.demoflowable.clickhouse.dao.user.UserInfoMapper;
import org.demoflowable.pojo.user.po.UserInfo;
import org.springframework.stereotype.Service;
@Service
public class UserInfoService {
@Resource
private UserInfoMapper userInfoMapper;
public void saveData(UserInfo userInfo) {
userInfoMapper.saveData(userInfo);
}
public UserInfo selectById(Integer id) {
return userInfoMapper.selectById(id);
}
public List<UserInfo> selectList() {
return userInfoMapper.selectList();
}
}
11、UserInfoController
import java.util.List;
import javax.annotation.Resource;
import org.demoflowable.pojo.user.po.UserInfo;
import org.demoflowable.service.user.UserInfoService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserInfoController {
@Resource
private UserInfoService userInfoService;
/**
* 保存
* @return
*/
@GetMapping("/saveUser")
public String saveUser() {
UserInfo userInfo = new UserInfo();
userInfo.setId(4);
userInfo.setUserName("xiaolin");
userInfo.setPassWord("54321");
userInfo.setPhone("18500909876");
userInfo.setCreateDay("2022-02-06");
userInfoService.saveData(userInfo);
return "success";
}
/**
* 根据id查询
* @param id
* @return
*/
@GetMapping("/getById")
public UserInfo getById(int id) {
return userInfoService.selectById(id);
}
/**
* 查询所有
* @return
*/
@GetMapping("/getList")
public List<UserInfo> getList() {
return userInfoService.selectList();
}
}
12、Application
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @Description: 启动类
*/
@SpringBootApplication
@ComponentScan({ "org.demoflowable" })
@MapperScan(basePackages = "org.demoflowable.*.dao")
public class Application {
/**
* @Title: main
* @Description: 启动类
* @param args
* @date 2023-11-08 23:49:23
*/
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
启动代码成功
12、查看接口数据
数据对应上了,springboot与clickhouse集成成功