增加table表
CREATE TABLE default.user_info (
id Int64,
user_name String(100),
phone String(100),
email String(100),
create_time DateTime
) ENGINE = Log;
1. 引入JAR包
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- clickhouse -->
<dependency>
<groupId>ru.yandex.clickhouse</groupId>
<artifactId>clickhouse-jdbc</artifactId>
<version>0.1.53</version>
</dependency>
<!-- Druid 数据连接池依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
2. 修改配置文件
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.click.url= jdbc:clickhouse://127.0.0.1:8123/default
spring.datasource.click.username= default
spring.datasource.click.password=
spring.datasource.click.driverClassName= ru.yandex.clickhouse.ClickHouseDriver
spring.datasource.click.initialSize= 10
spring.datasource.click.maxActive= 100
spring.datasource.click.minIdle= 10
spring.datasource.click.maxWait= 6000
3. 增加druid配置
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "spring.datasource.click")
@Data
public class ClickHouseConfig {
private String driverClassName ;
private String url ;
private Integer initialSize ;
private Integer maxActive ;
private Integer minIdle ;
private Integer maxWait ;
}
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
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
public DataSource dataSource() {
System.out.printf(clickHouseConfig.toString());
DruidDataSource datasource = new DruidDataSource();
datasource.setUrl(clickHouseConfig.getUrl());
datasource.setDriverClassName(clickHouseConfig.getDriverClassName());
datasource.setInitialSize(clickHouseConfig.getInitialSize());
datasource.setMinIdle(clickHouseConfig.getMinIdle());
datasource.setMaxActive(clickHouseConfig.getMaxActive());
datasource.setMaxWait(clickHouseConfig.getMaxWait());
return datasource;
}
}
4. 增加service和Mapper
import com.clickhouse.entity.UserInfo;
import io.lettuce.core.dynamic.annotation.Param;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface UserInfoMapper {
@Insert("insert into user_info(id,user_name,phone,email,create_time)values(#{id},#{user_name},#{phone},#{email},#{create_time})")
void saveUserInfo(UserInfo userInfo) ;
@Select("select * from user_info where id=#{id} ")
UserInfo selectById(@Param("id") Long id) ;
@Select("select * from user_info")
List<UserInfo> selectList() ;
}
import com.clickhouse.entity.UserInfo;
import com.clickhouse.mapper.UserInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.util.List;
@Component
public class UserInfoServiceImpl {
@Autowired
UserInfoMapper userInfoMapper;
public void saveUserInfo(){
for (int i=1;i<100;i++){
UserInfo userInfo=new UserInfo();
userInfo.setId((long) i);
userInfo.setPhone("183183"+i);
userInfo.setEmail("76"+i+"@qq.com");
userInfo.setUser_name("华"+i);
userInfo.setCreate_time(LocalDateTime.now());
userInfoMapper.saveUserInfo(userInfo);
}
}
public UserInfo getById(Long id){
return userInfoMapper.selectById(id);
}
public List<UserInfo> getAll(){
return userInfoMapper.selectList();
}
}