spring boot: 使用MyBatis从hive中读取数据

一、hive表:

启动hiveserver2

二、添加mybatis starter和hive依赖

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>hivejdbc</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.6</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.1</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>


        <dependency>
            <groupId>org.apache.hive</groupId>
            <artifactId>hive-jdbc</artifactId>
            <version>2.1.1</version>
            <exclusions>
                <exclusion>
                    <groupId>org.eclipse.jetty</groupId>
                    <artifactId>jetty-runner</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.hive/hive-exec -->
        <dependency>
            <groupId>org.apache.hive</groupId>
            <artifactId>hive-exec</artifactId>
            <version>3.0.0</version>
        </dependency>
        
    </dependencies>

</project>

三、配置文件application.properties中配置数据源:

复制代码
server.port=9202
spring.datasource.driverClassName=org.apache.hive.jdbc.HiveDriver
spring.datasource.url=jdbc:hive2://xx.xx.xx.xx:10000/default
spring.datasource.username=
spring.datasource.password=

四、定义mapper

复制代码
package cn.edu.tju.mapper;

import cn.edu.tju.domain.UserInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

@Mapper
public interface UserInfoMapper {
    @Select("select * from userinfo")
    List<Map<String,Object>> getUserList();


}

五、定义controller,注入mapper并使用

复制代码
package cn.edu.tju.controller;

import cn.edu.tju.domain.UserInfo;
import cn.edu.tju.mapper.UserInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class UserInfoController {
    @Autowired
    private UserInfoMapper userInfoMapper;

    @RequestMapping("/getUserInfo")
    public String getUserInfo() {
        List<Map<String, Object>> userList = userInfoMapper.getUserList();
        int size = userList.size();
        //System.out.println(size);
        for (int i = 0; i < size; i ++ ){
            Map userInfo = (Map)userList.get(i).get("userinfo");
            System.out.println(userInfo.get("x"));
            System.out.println(userInfo.get("y"));
        }

            return "ok";
    }


}
相关推荐
小糊涂灬4 小时前
Mybatis+mysql 一对多查询问题
mysql·mybatis
一定要AK4 小时前
MyBatis 从入门到精通
mybatis
白露与泡影5 小时前
Spring Boot 缓存架构:一行配置切换 Caffeine 与 Redis,透明支持多租户隔离
spring boot·缓存·架构
indexsunny6 小时前
互联网大厂Java面试实战:从Spring Boot到微服务架构的技术问答
java·spring boot·redis·微服务·面试·kafka·spring security
计算机学姐6 小时前
基于SpringBoot的在线学习网站平台【个性化推荐+数据可视化+课程章节学习】
java·vue.js·spring boot·后端·学习·mysql·信息可视化
星晨雪海6 小时前
Spring Boot 常用注解
java·spring boot·后端
96776 小时前
mybatis的作用+sql怎么写
java·开发语言·mybatis
二进制person6 小时前
JavaEE进阶 --Spring Framework、Spring Boot和Spring Web MVC(3)
spring boot·spring·java-ee
xufengzhu6 小时前
Springboot项目信创选型
java·spring boot·后端
一 乐6 小时前
非遗文化传承网站|基于springboot + vue非遗文化传承网站系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·非遗文化传承网站