加入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.18</version>
</dependency>
<dependency>
<groupId>ru.yandex.clickhouse</groupId>
<artifactId>clickhouse-jdbc</artifactId>
<version>0.1.53</version>
</dependency>
配置数据源
package tcly.java.crm.config;
import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
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;
import java.util.Objects;
@Configuration
@MapperScan(basePackages = {"tcly.java.crm.dao.crm"},sqlSessionFactoryRef = "sqlSessionFactoryCK")
public class ClickHouseDataSourceConfig {
@Bean(name = "dataSourceCK")
public DataSource dataSource() {
DruidDataSource datasource = new DruidDataSource();
datasource.setDriverClassName("ru.yandex.clickhouse.ClickHouseDriver");
datasource.setUrl("jdbc:clickhouse://10.100.222.112:8123/tclydcdb");
datasource.setUsername("xxxxx");
datasource.setPassword("xxxxx");
return datasource;
}
@Bean(name = "sqlSessionFactoryCK")
public SqlSessionFactory masterSqlSessionFactory(@Qualifier("dataSourceCK")DataSource masterDataSource) throws Exception {
final MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
sessionFactory.setTypeAliasesPackage("tcly.java.crm.model.entity");
sessionFactory.setDataSource(masterDataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mapper/*.xml"));
try {
//开启驼峰命名转换
Objects.requireNonNull(sessionFactory.getObject()).getConfiguration().setMapUnderscoreToCamelCase(true);
return sessionFactory.getObject();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* 返回data数据库的会话模板
* @param sessionFactory sqlSessionFactoryHotel
* @return sqlSessionTemplateHotel
*/
@Bean(name = "sqlSessionTemplateCK")
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactoryCK") SqlSessionFactory sessionFactory) {
return new SqlSessionTemplate(sessionFactory);
}
}