mybatis-plus clickhouse支持分页
生活随笔
收集整理的這篇文章主要介紹了
mybatis-plus clickhouse支持分页
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
事情不是太復雜,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 {@Resourceprivate JdbcParamConfig jdbcParamConfig ;@Beanpublic 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 {/*** 新的分頁插件*/@Beanpublic 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 {@Overridepublic 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 {@AutowiredSafeEventAllService 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略。
總結
以上是生活随笔為你收集整理的mybatis-plus clickhouse支持分页的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 设置 缓冲区大小_java –
- 下一篇: 大数据之数据采集