关于 SpringBoot 连接 ClickHouse

本文章讲解在SpringBoot + MyBatis 配置使用ClickHouse数据库(单机版/集群版)

ClickHouse JDBC驱动:


        官方
                1. GitHub - ClickHouse/clickhouse-jdbc: JDBC driver for ClickHouse

        第三方驱动

                1. GitHub - blynkkk/clickhouse4j: Lighter and faster alternative for the official ClickHouse JDBC driver

                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);
    }
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值