本文章讲解在SpringBoot + MyBatis 配置使用ClickHouse数据库(单机版/集群版)
ClickHouse JDBC驱动:
官方
1. GitHub - ClickHouse/clickhouse-jdbc: JDBC driver for ClickHouse
第三方驱动
2. GitHub - housepower/ClickHouse-Native-JDBC: ClickHouse Native Protocol JDBC implementation
本文中案例使用官方驱动
POM 依赖
<dependency>
<groupId>ru.yandex.clickhouse</groupId>
<artifactId>clickhouse-jdbc</artifactId>
<version>0.3.1</version>
</dependency>
1. YML配置
单机版
spring:
datasource:
driver-class-name: ru.yandex.clickhouse.ClickHouseDriver
url: jdbc:clickhouse://ip:port/库名
user: user
password: password
集群版
spring:
datasource:
driver-class-name: ru.yandex.clickhouse.ClickHouseDriver
url: jdbc:clickhouse://ip:port,ip:port/库名
user: user
password: password
2. 数据库配置属性类
/**
* @author conbing
* @date 2021/11/08 14:43
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class ClickHouseDataSourceProperties extends ClickHouseProperties{
private String url;
}
3. 数据源配置类
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import ru.yandex.clickhouse.BalancedClickhouseDataSource;
import ru.yandex.clickhouse.ClickHouseDataSource;
import javax.sql.DataSource;
import java.util.concurrent.TimeUnit;
/**
* @author conbing
* @date 2021/11/08 14:43
*/
@Configuration
public class DataSourcesConfig {
/**
* 单机版
* ch 数据源
* @return 数据源
*/
@Bean(name = "clickHouseDataSource")
public DataSource clickHouseDataSource(@Qualifier("clickHouseDataSourceProperties") ClickHouseDataSourceProperties clickHouseDataSourceProperties) {
return new ClickHouseDataSource(clickHouseDataSourceProperties.getUrl(), clickHouseDataSourceProperties);
}
/**
* 集群版
* ch 数据源
* @return 数据源
*/
@Bean(name = "clickHouseDataSource")
public DataSource clickHouseDataSource(@Qualifier("clickHouseDataSourceProperties") ClickHouseDataSourceProperties clickHouseDataSourceProperties) {
BalancedClickhouseDataSource balancedClickhouseDataSource = new BalancedClickhouseDataSource
(clickHouseDataSourceProperties.getUrl(), clickHouseDataSourceProperties);
//配置定时任务定时检查可用的数据库连接
balancedClickhouseDataSource.scheduleActualization(10, TimeUnit.SECONDS);
return balancedClickhouseDataSource;
}
/**
* ch 数据库连接配置
* @return 数据库连接配置
*/
@Bean(name = "clickHouseDataSourceProperties")
@ConfigurationProperties("spring.datasource")
public ClickHouseDataSourceProperties clickHouseDataSourceProperties() {
return new ClickHouseDataSourceProperties();
}
}
4. MyBatis配置类
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
/**
* @author conbing
* @description MyBatis数据库配置 clickhouse 不支持事物
* @date 2021/11/08 14:43
*/
@Configuration
// 配置mybatis的接口类放的地方
@MapperScan(basePackages = "com.mapper", sqlSessionFactoryRef = "sqlSessionFactory")
public class ClickHouseForMyBatis {
@Bean(name = "sqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("clickHouseDataSource") DataSource datasource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(datasource);
bean.setMapperLocations(
// 设置mybatis的xml所在位置
new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*.xml"));
return bean.getObject();
}
@Bean("sqlSessionTemplate")
public SqlSessionTemplate sqlsessiontemplate(
@Qualifier("sqlSessionFactory") SqlSessionFactory sessionfactory) {
return new SqlSessionTemplate(sessionfactory);
}
}