首页 > 其他分享 >SpringBoot: 通过MyBatis访问ClickHouse

SpringBoot: 通过MyBatis访问ClickHouse

时间:2024-01-08 16:32:37浏览次数:28  
标签:return String private org MyBatis import public ClickHouse SpringBoot


一、ClickHouse中建表,添加数据

SpringBoot: 通过MyBatis访问ClickHouse_mybatis


SpringBoot: 通过MyBatis访问ClickHouse_clickhouse_02


二、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";
    }
}


标签:return,String,private,org,MyBatis,import,public,ClickHouse,SpringBoot
From: https://blog.51cto.com/amadeusliu/9146829

相关文章

  • springboot集成swagger knife4j 最详细的步骤 手把手教你继承swagger
    springboot集成swaggerknife4j最详细的步骤手把手教你继承swagger效果图Knife4j介绍Knife4j的前身是swagger-bootstrap-ui,前身swagger-bootstrap-ui是一个纯swagger-ui的ui皮肤项目一开始项目初衷是为了写一个增强版本的swagger的前端ui,但是随着项目的发展,面对越来越多的个性......
  • SpringBoot 接口:响应时间优化9个技巧!
    今天聊聊SpringBoot接口:响应时间优化的9个技巧。在实际开发中,提升接口响应速度是一件挺重要的事,特别是在面临大量用户请求的时候。好了,咱们直接切入正题。本文,已收录于,我的技术网站ddkk.com,有大厂完整面经,工作技术,架构师成长之路,等经验分享在SpringBoot应用中,接口响应时间的优......
  • 光速爱购--靠谱的SpringBoot项目
    简介这是一个靠谱的SpringBoot项目实战,名字叫光速爱购。从零开发项目,视频加文档,十天就能学会开发JavaWeb项目。教程路线是:搭建环境=>安装软件=>创建项目=>添加依赖和配置=>通过表生成代码=>编写Java代码=>代码自测=>前后端联调=>准备找工作。学完即可成为合格的Java开发,心......
  • springboot项目中,Mapper接口映射xml文件出错问题,显示Invalid bound statement
    在进行springboot开发过程中遇到的一个问题,显示org.apache.ibatis.binding.BindingException:Invalidboundstatement(notfound);大概意思应该就是找不到Mapper接口对应的xml映射文件,检查了以后,发现应该是没有正确扫描到映射文件的路径。1.保证java文件夹下面的Mapper接口包名称......
  • MyBatis批量插入数据优化
    背景介绍我们使用了mybatis-plus框架,并采用其中的saveBatch方法进行批量数据插入。然而,通过深入研究源码,我发现这个方法并没有如我期望的那样高效这是因为最终在执行的时候还是通过for循环一条条执行insert,然后再一批的进行flush,默认批的消息为1000为了找到更优秀的解决方案......
  • SpringBoot框架中的DAO层、Entity层、Service层、Controller层
    非原创,看了许多篇博客的总结一般的项目模块中都有DAO、Entity、Service、Controller层。 Entity层是实体层,也就是所谓的model,也称为pojo层,是数据库在项目中的类,该文件包含实体类的属性和对应属性的set、get方法; DAO层=mapper层,现在用Mybatis逆向工程生成的mapper层,其实就是......
  • SpringBoot的Controller,Service,Repository层的使用
    找回熟悉的Controller,ServiceController哪儿去了?对于很多习惯了Spring开发的同学来讲,Controller,Service,DAO这些套路突然间都没了会有不适感。其实呢,这些东西还在,只不过对于较简单的情景下,这些都变成了系统背后帮你做的事情。这一小节我们就先来看看如何将Controller再召唤回......
  • springboot 打包本地jar包或外部依赖打不进去问题
    分为两种情况一、打war包的情况引入依赖<dependency><groupId>com.xxxx</groupId><artifactId>xxxxx</artifactId><version>1.0</version><scope>system</scope><systemPath>${basedir}/lib/xxxxx.jar&l......
  • Springboot 2.7 open api:swagger | knife4j | spring doc
    *[集成SpringDoc接口文档和knife4j|SpringBoot2.7.2实战基础-掘金](https://juejin.cn/post/7201195677128687674)*[Springboot2.7集成Swagger增强版接口框架Knife4j4.3+springdocOpenApi3.0\_knife4jspringboot2.7-CSDN博客](https://blog.csdn.net/Mrqi......
  • 记录Springboot中向企业微信指定人员发送含链接的消息
    背景:从海康智能门禁获取到了进入教室的人脸信息,由此得到一批用户List,等会儿就要实时向这批用户发送消息“***,您已进入**教室,请填写使用情况表<ahref="****">”。  过程:读了微信的开发者文档,摸索着写了测试代码。在debug时,发现微信传来的是{"errcode":60020,"errmsg":"not......