事情不是太复杂,mybatis-plus3.X已经支持ClickHouse数据库,只需正确配置其他用法跟用mysql一样。
废话不多说,直接上代码。
1 相关依赖:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<!-- alibaba的druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.4</version>
</dependency>
<dependency>
<groupId>ru.yandex.clickhouse</groupId>
<artifactId>clickhouse-jdbc</artifactId>
<version>0.1.53</version>
</dependency>
2 相关配置:
(1)读取yml配置:
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "spring.datasource.click")
public class JdbcParamConfig {
private String driverClassName ;
private String url ;
private Integer initialSize ;
private Integer maxActive ;
private Integer minIdle ;
private Integer maxWait ;
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;
}
}
(2)Druid连接池的配置
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
import javax.sql.DataSource;
@Configuration
public class DruidPoolConfig {
@Resource
private JdbcParamConfig jdbcParamConfig ;
@Bean
public DataSource dataSource() {
DruidDataSource datasource = new DruidDataSource();
datasource.setUrl(jdbcParamConfig.getUrl());
datasource.setDriverClassName(jdbcParamConfig.getDriverClassName());
datasource.setInitialSize(jdbcParamConfig.getInitialSize());
datasource.setMinIdle(jdbcParamConfig.getMinIdle());
datasource.setMaxActive(jdbcParamConfig.getMaxActive());
datasource.setMaxWait(jdbcParamConfig.getMaxWait());
//datasource.setValidationQuery("select 1");
datasource.setTestOnBorrow(true);
datasource.setTestOnReturn(true);
datasource.setTestWhileIdle(true);
datasource.setTimeBetweenEvictionRunsMillis(15000);
datasource.setMinEvictableIdleTimeMillis(60000);
datasource.setRemoveAbandoned(true);
datasource.setRemoveAbandonedTimeout(3600);
datasource.setLogAbandoned(true);
return datasource;
}
}
(3)mybatis-plus的配置:
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MybatisPlusConfig {
/**
* 新的分页插件
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.CLICK_HOUSE));
return interceptor;
}
}
3 代码:
(1)映射器mapper:
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xlkj.clickhouseservice.beans.SafeEventAll;
public interface SafeEventAllDao extends BaseMapper<SafeEventAll> {
}
(2)service接口:
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.xlkj.clickhouseservice.beans.SafeEventAll;
public interface SafeEventAllService extends IService<SafeEventAll> {
/**
* 分页查询
* @param page 第几页
* @param pageSize 每页条数
* @return Page
*/
Page<SafeEventAll> list(Integer page, Integer pageSize);
}
(3)serviceImpl实现类:
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xlkj.clickhouseservice.beans.SafeEventAll;
import com.xlkj.clickhouseservice.dao.SafeEventAllDao;
import com.xlkj.clickhouseservice.service.SafeEventAllService;
import org.springframework.stereotype.Service;
@Service
public class SafeEventAllServiceImpl extends ServiceImpl<SafeEventAllDao, SafeEventAll> implements SafeEventAllService {
@Override
public Page<SafeEventAll> list(Integer page, Integer pageSize) {
//TODO 将来在这里处理QueryWrapper<SafeEventAll>对象
return this.page(new Page<SafeEventAll>(page,pageSize),new QueryWrapper<SafeEventAll>());
}
}
(4)controller前端控制器:
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xlkj.clickhouseservice.beans.SafeEventAll;
import com.xlkj.clickhouseservice.service.SafeEventAllService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/safeallevent")
public class SafeEventAllController {
@Autowired
SafeEventAllService safeEventAllService;
/**
* 分页查询
* @return
*/
@GetMapping("/list")
public Object list(@RequestParam(value = "page",defaultValue = "1") Integer page,
@RequestParam(value = "page_size",defaultValue = "10") Integer pageSize) {
Page<SafeEventAll> pageObj = safeEventAllService.list(page, pageSize);
return pageObj;
}
}
(5)pojo映射对象略、映射文件xml略。