一、ClickHouse中建表,添加数据
二、SpringBoot项目添加mybatis、clickhouse、druid相关依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.6</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
<dependency>
<groupId>ru.yandex.clickhouse</groupId>
<artifactId>clickhouse-jdbc</artifactId>
<version>0.3.2</version>
</dependency>
三、配置文件进行配置数据源(application.yml):
spring:
jpa:
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL5Dialect
datasource:
type: com.alibaba.druid.pool.DruidDataSource
clickhouse:
driverClassName: ru.yandex.clickhouse.ClickHouseDriver
url: jdbc:clickhouse://xxx.xxx.xxx.xxx:8123/helloworld #clickhouse地址
userName: default
password: default
initialSize: 10
maxActive: 100
minIdle: 10
maxWait: 6000
validationQuery: SELECT 1
server:
port: 9234
四、创建实体类映射数据表:
package cn.edu.tju.domain;
public class MyFirstTable {
private int price;
private String addr;
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
@Override
public String toString() {
return "MyFirstTable{" +
"price=" + price +
", addr='" + addr + '\'' +
'}';
}
}
五、定义mapper
package cn.edu.tju.mapper;
import cn.edu.tju.domain.MyFirstTable;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface MyMapper {
@Select("select count(1) from my_first_table")
int getCount();
@Select("select * from my_first_table where price =#{price}")
MyFirstTable getByPrice(int price);
}
六、配置SqlSessionFactory
package cn.edu.tju.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "spring.datasource.clickhouse")
public class ClickHouseConfig {
private String username;
private String password;
private String driverClassName ;
private String url ;
private Integer initialSize ;
private Integer maxActive ;
private Integer minIdle ;
private Integer maxWait ;
private String validationQuery;
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 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 getValidationQuery() {
return validationQuery;
}
public void setValidationQuery(String validationQuery) {
this.validationQuery = validationQuery;
}
}
package cn.edu.tju.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
@Configuration
public class DruidConfig {
@Autowired
private ClickHouseConfig clickHouseConfig;
@Bean(name = "clickHouseDataSource")
public DataSource dataSource() throws ClassNotFoundException {
Class clazz = Class.forName("com.alibaba.druid.pool.DruidDataSource");
DruidDataSource dataSource = (DruidDataSource) DataSourceBuilder
.create()
.driverClassName(clickHouseConfig.getDriverClassName())
.type(clazz)
.url(clickHouseConfig.getUrl())
.username(clickHouseConfig.getUsername())
//.password(clickHouseConfig.getPassword())
.build();
dataSource.setMaxWait(clickHouseConfig.getMaxWait());
dataSource.setValidationQuery(clickHouseConfig.getValidationQuery());
return dataSource;
}
@Bean("sqlSessionFactory")
public SqlSessionFactory clickHouseSqlSessionFactoryBean() throws Exception {
SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
factory.setDataSource(dataSource());
factory.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
return factory.getObject();
}
}
七、controller中注入mapper(此处省略了service)
package cn.edu.tju.controller;
import cn.edu.tju.domain.MyFirstTable;
import cn.edu.tju.mapper.MyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoController {
@Autowired
private MyMapper myMapper;
@RequestMapping("/hello")
public String getInfo(){
int result = myMapper.getCount();
System.out.println(result);
return "hello";
}
@RequestMapping("/hello2")
public String getInfo2(){
MyFirstTable myFirstTable = myMapper.getByPrice(2015);
System.out.println(myFirstTable);
return "hello2";
}
}