mybatis-plus clickhouse支持分页

本文介绍了如何在Spring Boot项目中通过Mybatis-Plus 3.4.2版本集成ClickHouse数据库,详细步骤包括添加相关依赖、配置Druid连接池和Mybatis-Plus拦截器,以及展示分页查询的示例代码。
摘要由CSDN通过智能技术生成

事情不是太复杂,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略。

JDBC(Java Database Connectivity)是Java语言访问数据库的标准接口,而ClickHouse是一个开源的列式数据库管理系统。通过JDBC连接ClickHouse,可以在Java应用程序中进行数据的读取和写入操作。 要连接ClickHouse数据库,首先需要下载并导入ClickHouse JDBC驱动程序。可以从ClickHouse官方网站或Maven中央仓库获取最新的JDBC驱动程序。 接下来,可以使用以下步骤来连接ClickHouse数据库: 1. 加载驱动程序:使用`Class.forName()`方法加载ClickHouse的JDBC驱动程序。例如: ```java Class.forName("ru.yandex.clickhouse.ClickHouseDriver"); ``` 2. 建立连接:使用`DriverManager.getConnection()`方法建立与ClickHouse数据库的连接。需要提供ClickHouse数据库的URL、用户名和密码。例如: ```java String url = "jdbc:clickhouse://localhost:8123/mydatabase"; String username = "myusername"; String password = "mypassword"; Connection connection = DriverManager.getConnection(url, username, password); ``` 3. 执行SQL语句:使用`connection.createStatement()`方法创建Statement对象,并使用该对象执行SQL语句。例如: ```java Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("SELECT * FROM mytable"); ``` 4. 处理结果:通过`ResultSet`对象可以获取查询结果。可以使用`resultSet.next()`方法遍历结果集,并使用`resultSet.getXXX()`方法获取具体的字段值。例如: ```java while (resultSet.next()) { int id = resultSet.getInt("id"); String name = resultSet.getString("name"); // 处理结果... } ``` 5. 关闭连接:在使用完数据库连接后,需要关闭连接以释放资源。可以使用`connection.close()`方法关闭连接。例如: ```java connection.close(); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值