SpringBoot: 通过MyBatis访问ClickHouse

一、ClickHouse中建表,添加数据
在这里插入图片描述
在这里插入图片描述
二、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.MySQL5Dialectdatasource:type: com.alibaba.druid.pool.DruidDataSourceclickhouse:driverClassName: ru.yandex.clickhouse.ClickHouseDriverurl: jdbc:clickhouse://xxx.xxx.xxx.xxx:8123/helloworld #clickhouse地址userName: defaultpassword: defaultinitialSize: 10maxActive: 100minIdle: 10maxWait: 6000validationQuery: 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;}@Overridepublic 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 {@Autowiredprivate 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 {@Autowiredprivate 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";}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/595184.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

opencv入门到精通——Canny边缘检测

目录 理论 OpenCV中的Canny Edge检测 附加资源 在本章中&#xff0c;我们将学习 Canny边缘检测的概念 OpenCV函数: cv.Canny() 理论 Canny Edge Detection是一种流行的边缘检测算法。它由John F. Canny发明 1.这是一个多阶段算法&#xff0c;我们将经历每个阶段。 2.降…

原生JS调用OpenAI GPT接口并实现ChatGPT逐字输出效果

效果&#xff1a; 猜你感兴趣&#xff1a;springbootvue实现ChatGPT逐字输出打字效果 附源码&#xff0c;也是小弟原创&#xff0c;感谢支持&#xff01; 没废话&#xff0c;上代码&#xff1a; <!DOCTYPE html> <html lang"en"> <head><me…

09、docker 安装nacos并配置mysql存储配置信息

docker 安装nacos并配置mysql存储配置信息 1、docker启动nacos的各种方式2、Docker安装nacos3、MySQL中新建nacos的数据库4、挂载数据or配置目录5、运行 1、docker启动nacos的各种方式 内嵌derby数据源 docker run -d \ -e PREFER_HOST_MODEhostname \ -e SPRING_DATASOURCE_…

软件测试技术复习点

1 术语含义&#xff08;故障、错误、失效、测试用例&#xff09; 故障&#xff08;Fault&#xff09;&#xff1a;故障是软件中的静态缺陷&#xff1b; 故障屏蔽&#xff1a;软件中的某个故障可能被其他一个或多个故障屏蔽&#xff1b; 错误&#xff08;Error&#xff09;&…

1 电科院FTU检测标准学习笔记-外观检查

作者简介&#xff1a; 本人从事电力系统多年&#xff0c;岗位包含研发&#xff0c;测试&#xff0c;工程等&#xff0c;具有丰富的经验 在配电自动化验收测试以及电科院测试中&#xff0c;本人全程参与&#xff0c;积累了不少现场的经验 目录 **前言****检测大纲****外观与结构…

LeetCode 2397. 被列覆盖的最多行数,状态压缩优化回溯法

一、题目 1、题目描述 给你一个下标从 0 开始、大小为 m x n 的二进制矩阵 matrix &#xff1b;另给你一个整数 numSelect&#xff0c;表示你必须从 matrix 中选择的 不同 列的数量。 如果一行中所有的 1 都被你选中的列所覆盖&#xff0c;则认为这一行被 覆盖 了。 形式上&am…

【目标检测实验系列】YOLOv5模型改进:融合混合注意力机制CBAM,关注通道和空间特征,助力模型高效涨点!(内含源代码,超详细改进代码流程)

自我介绍&#xff1a;本人硕士期间全程放养&#xff0c;目前成果:一篇北大核心CSCD录用,两篇中科院三区已见刊&#xff0c;一篇中科院四区在投。如何找创新点&#xff0c;如何放养过程厚积薄发&#xff0c;如何写中英论文&#xff0c;找期刊等等。本人后续会以自己实战经验详细…

开源协议简介和选择

软件国产化已经提到日程上了&#xff0c;先来研究一下开源协议。 引言 在追求“自由”的开源软件领域的同时不能忽视程序员的权益。为了激发程序员的创造力&#xff0c;现今世界上有超过60种的开源许可协议被开源促进组织&#xff08;Open Source Initiative&#xff09;所认可…

SpingBoot的项目实战--模拟电商【3.购物车模块】

&#x1f973;&#x1f973;Welcome Huihuis Code World ! !&#x1f973;&#x1f973; 接下来看看由辉辉所写的关于SpringBoot电商项目的相关操作吧 目录 &#x1f973;&#x1f973;Welcome Huihuis Code World ! !&#x1f973;&#x1f973; 一.功能需求 二.代码编写 …

Git(2):Git环境的安装

本教程里的git命令例子都是在Git Bash中演示的&#xff0c;会用到一些基本的linux命令&#xff0c;在此为大家提前列举&#xff1a; ls/ll 查看当前目录cat 查看文件内容touch 创建文件vi vi编辑器&#xff08;使用vi编辑器是为了方便展示效果&#xff0c;学员可以记事本、edi…

使用异构图学习破解推荐系统 - 第 1 部分

Lokesh Sharma – Medium 一、说明 所以&#xff0c;这是独家新闻&#xff1a;异质图拥有一个充满潜力的世界&#xff0c;而常规图却无法做到这一点。传统的同构图很难处理不同关系和边类型的复杂性。现在是大炮的时候了——先进的架构可以解决具有多种边缘和关系类型的数据集的…

Nacos学习思维导图

一、服务注册 参考文档&#xff1a;http://www.bryh.cn/a/118936.html https://blog.csdn.net/Saintmm/article/details/121981184 二、服务续约 参考文档&#xff1a;http://www.bryh.cn/a/118936.html https://blog.csdn.net/Saintmm/article/details/121981184 三、服务…

清风数学建模排版

Overview 链接&#xff1a;https://pan.baidu.com/s/11QBw3zBFNicwQWvWCfW1Gg?pwdepnz 提取码&#xff1a;epnz Latex 范文排版练习 b站刘海洋latex工作室&#xff0c;待还愿 Word基础 Word VBA&#xff0c;待还愿 fnF4&#xff1a;重复上一步操作 ctrlY&#xff1a;恢…

DispatcherServlet请求处理流程

前言 DispatcherServlet 是 Spring MVC 的核心类&#xff0c;它本质是一个 Servlet&#xff0c;负责接管 HTTP 请求并把它分发给对应的处理器处理&#xff0c;最后处理响应结果渲染页面。 DispatcherServlet 本身并不复杂&#xff0c;它提供了一个模板方法doDispatch()来处理请…

进阶学习——Linux系统中重点‘进程’

目录 一、程序和进程的关系 1.程序 2.进程 2.1线程 2.2协程 3.进程与线程的区别 4.总结 4.1延伸 5.进程使用内存的问题 5.1内存泄漏——Memory Leak 5.2内存溢出——Memory Overflow 5.3内存不足——OOM&#xff08;out of memory&#xff09; 5.4进程使用内存出现…

如何正确使用docker搭建靶场--pikachu

在Linux中搭建靶场——pikachu 1.开启docker systemctl start docker 2.查看docker状态 systemctl status docker 3.查看docker存在那些镜像 docker images 4.拉取镜像&#xff0c;这里是以pikachu为例因此需要一个php5的版本 &#xff08;1&#xff09;打开代理&#xff…

【Nodejs】基于Promise异步处理的博客demo代码实现

目录 package.json www.js db.js app.js routes/blog.js controllers/blog.js mysql.js responseModel.js 无开发&#xff0c;不安全。 这个demo项目实现了用Promise异步处理http的GET和POST请求&#xff0c;通过mysql的api实现了博客增删改查功能&#xff0c;但因没有…

为什么亚马逊卖家一定要有独立站?新手低成本快速搭建跨境电商独立站完整图文教程

目录 前言&#xff1a;为什么亚马逊卖家一定要有独立站&#xff1f; 为什么不选Shopify建站&#xff1f; 效果展示 一、购买域名 二、购买主机托管 三、搭建网站 前言&#xff1a;为什么亚马逊卖家一定要有独立站&#xff1f; 最近不少卖家朋友来问独立站建站方面的问题…

安全防御之授权和访问控制技术

授权和访问控制技术是安全防御中的重要组成部分&#xff0c;主要用于管理和限制对系统资源&#xff08;如数据、应用程序等&#xff09;的访问。授权控制用户可访问和操作的系统资源&#xff0c;而访问控制技术则负责在授权的基础上&#xff0c;确保只有经过授权的用户才能访问…

前端插件库-VUE3 使用 vue-codemirror 插件

VUE3 插件 vue-codemirror 使用步骤和实例、基于 CodeMirror &#xff0c;适用于 Vue 的 Web 代码编辑器。 第一步&#xff1a;安装 vue-codemirror & codemirror 包 &#xff0c; 以及语言包 npm install codemirror --save npm install vue-codemirror --savenpm insta…