application.properties

# mysql
spring.datasource.jdbc-url=@config.jdbc.url@
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=@config.jdbc.username@
spring.datasource.password=@config.jdbc.password@

# clickhouse
## clickhouse
spring.clickhouse.jdbc-url=@config.clickhouse.url@
spring.clickhouse.driver-class-name=ru.yandex.clickhouse.ClickHouseDriver
spring.clickhouse.username=
spring.clickhouse.password=

Java 代码配置

主 datasource

package com.uniweibo.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.ClassPathResource;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = {"com.uniweibo.dao.mapper", "com.uniweibo.meitu.dao.mapper", "com.uniweibo.xiaomi.dao.mapper"},
        sqlSessionFactoryRef = "sqlSessionFactory"
)
public class AppDataSourceConfig {
    /** 配置文件名, 放在 classpath */
    private String MYBATIS_CONFIG_FILE = "mybatis-config.xml";

    @Bean
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource() {
        return DataSourceBuilder.create().driverClassName("com.mysql.jdbc.Driver").build();
    }

    @Bean(name = "sqlSessionFactory")
    @Primary
    public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource dataSource) throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setConfigLocation(new ClassPathResource(MYBATIS_CONFIG_FILE));
        sessionFactory.setDataSource(dataSource);
        return sessionFactory.getObject();
    }
}


第二个 datasource

package com.uniweibo.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * @author emacsist
 */
@Configuration
@MapperScan(basePackages = {"com.uniweibo.dao.clickhouse"},
        sqlSessionFactoryRef = "clickHouseSqlSessionFactory"
)
public class ClickHouseDataSourceConfig {

    private static final Logger log = LoggerFactory.getLogger(ClickHouseDataSourceConfig.class);


    @Bean
    @ConfigurationProperties(prefix = "spring.clickhouse")
    public DataSource clickHouseDataSource() {
        final DataSource dataSource =  DataSourceBuilder.create().driverClassName("ru.yandex.clickhouse.ClickHouseDriver").build();
        return dataSource;
    }


    @Bean(name = "clickHouseSqlSessionFactory")
    public SqlSessionFactory clickHouseSqlSessionFactory(@Qualifier("clickHouseDataSource") DataSource clickHouseDataSource) throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(clickHouseDataSource);
        return sessionFactory.getObject();
    }
}

遇到的问题

因为使用的是Spring默认的 HikariCP 连接池. 所以, 要注意上面的配置文件中写的是 jdbc-url. 不然可能会报

jdbcUrl is required with driverClassName.

类似这样子的错误.