springboot整合hive
pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.18</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>cn.lhz</groupId><artifactId>hive-api</artifactId><version>0.0.1</version><name>hive-api</name><description>hive-api</description><url/><licenses><license/></licenses><developers><developer/></developers><scm><connection/><developerConnection/><tag/><url/></scm><properties><java.version>21</java.version><jdk.version>21</jdk.version><maven.compiler.source>${jdk.version}</maven.compiler.source><maven.compiler.target>${jdk.version}</maven.compiler.target><maven.compiler.compilerVersion>${jdk.version}</maven.compiler.compilerVersion><maven.compiler.encoding>utf-8</maven.compiler.encoding><project.build.sourceEncoding>utf-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><maven.test.failure.ignore>true</maven.test.failure.ignore><maven.test.skip>true</maven.test.skip><commons-io.version>2.17.0</commons-io.version><commons-lang3.version>3.17.0</commons-lang3.version><druid.version>1.2.23</druid.version><dom4j.version>2.1.4</dom4j.version><fastjson.version>2.0.53</fastjson.version><fastjson2.version>2.0.53</fastjson2.version><gson.version>2.11.0</gson.version><generator.version>1.1.2</generator.version><hutool.version>5.8.32</hutool.version><junit5.version>5.11.2</junit5.version><jaxen.version>2.0.0</jaxen.version><jackson.version>2.18.0</jackson.version><lombok.version>1.18.34</lombok.version><mybatis-plus.version>3.5.9</mybatis-plus.version><mybatis-spring.version>3.0.4</mybatis-spring.version><spring-boot.version>2.7.18</spring-boot.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><exclusions><exclusion><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId></exclusion></exclusions></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>${mybatis-plus.version}</version></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus</artifactId><version>${mybatis-plus.version}</version></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-jsqlparser</artifactId><version>${mybatis-plus.version}</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>${hutool.version}</version></dependency><dependency><groupId>com.github.binarywang</groupId><artifactId>java-testdata-generator</artifactId><version>1.1.2</version></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>${commons-lang3.version}</version></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>${commons-io.version}</version></dependency><dependency><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId><version>${gson.version}</version></dependency><dependency><groupId>org.webjars</groupId><artifactId>layui</artifactId><version>2.9.10</version></dependency><!--com.alibaba.druid--><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>${druid.version}</version></dependency><!--swagger3--><dependency><groupId>io.springfox</groupId><artifactId>springfox-boot-starter</artifactId><version>3.0.0</version></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>3.0.0</version></dependency><dependency><groupId>com.github.xiaoymin</groupId><artifactId>swagger-bootstrap-ui</artifactId><version>1.9.6</version></dependency><dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-spring-ui</artifactId><version>3.0.3</version></dependency><!-- hive-jdbc --><dependency><groupId>org.apache.hive</groupId><artifactId>hive-jdbc</artifactId><version>3.1.3</version><exclusions><exclusion><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId></exclusion><exclusion><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-slf4j-impl</artifactId></exclusion><exclusion><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId></exclusion><exclusion><groupId>org.eclipse.jetty.orbit</groupId><artifactId>*</artifactId></exclusion><exclusion><groupId>org.eclipse.jetty.aggregate</groupId><artifactId>*</artifactId></exclusion><exclusion><groupId>tomcat</groupId><artifactId>*</artifactId></exclusion><exclusion><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId></exclusion><exclusion><groupId>org.mortbay.jetty</groupId><artifactId>*</artifactId></exclusion></exclusions></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.13.0</version><configuration><encoding>UTF-8</encoding><source>${jdk.version}</source><target>${jdk.version}</target></configuration></plugin><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins><finalName>${project.name}</finalName></build><repositories><repository><id>public</id><name>aliyun nexus</name><url>https://maven.aliyun.com/repository/public</url><releases><enabled>true</enabled></releases></repository></repositories><pluginRepositories><pluginRepository><id>public</id><name>aliyun nexus</name><url>https://maven.aliyun.com/repository/public</url><releases><enabled>true</enabled></releases><snapshots><enabled>false</enabled></snapshots></pluginRepository></pluginRepositories>
</project>
配置文件
application.yml
server:port: 8080servlet:context-path: /api
spring:application:name: hive-apimvc:path match:matching-strategy: ant_path_matcherjackson:serialization:FAIL_ON_EMPTY_BEANS: false# json 序列化排除值为 null 的属性default-property-inclusion: non_null# 配置 Date 类的时间格式,如果不涉及可以不加date-format: yyyy-MM-dd HH:mm:ss# 配置 Date 类的时区,如果不涉及可以不加time-zone: GMT+8thymeleaf:#缓冲的配置cache: falsecheck-template: truecheck-template-location: true#开启MVC thymeleaf 视图解析enabled: true#模板的模式,支持 HTML, XML TEXT JAVASCRIPTmode: HTML5#编码 可不用配置encoding: UTF-8#配置模板路径,默认是templates,可以不用配置prefix: classpath:templates# 文件后缀名suffix: .html#内容类别,可不用配置servlet:content-type: text/html;charset=utf-8datasource:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: org.apache.hive.jdbc.HiveDriverurl: jdbc:hive2://lihaozhe03:10000/lihaozheusername: rootpassword:
mybatis-plus:# 如果是放在src/main/java目录下 classpath:/com/yourpackage/*/mapper/*Mapper.xml# 如果是放在resource目录 classpath:/mapper/*Mapper.xmlmapper-locations: classpath:mapper/*.xml#实体扫描,多个package用逗号或者分号分隔type-aliases-package: cn.lhz.dto,cn.lhz.voconfiguration:#配置返回数据库(column下划线命名&&返回java实体是驼峰命名),自动匹配无需as(没开启这个,SQL需要写as: select user_id as userId)map-underscore-to-camel-case: truecache-enabled: false#配置JdbcTypeForNull, oracle数据库必须配置jdbc-type-for-null: 'null'log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
#是否激活 swagger true or false
springfox:documentation:enabled: true
工具类
util 包下的工具类
opeapi配置类
swagger配置类
package cn.lhz.config;import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.net.Inet4Address;
import java.net.UnknownHostException;/*** @author 李昊哲* @version 1.0.0*/
@Configuration
@Slf4j
public class OpenApiConfig implements ApplicationListener<WebServerInitializedEvent> {@Beanpublic OpenAPI springOpenAPI() {Contact contact = new Contact();contact.setName("李昊哲");contact.setUrl("https://space.bilibili.com/480308139");contact.setEmail("646269678@qq.com");// 访问路径:http://localhost:8080/swagger-ui/index.html// 访问路径:http://localhost:8080/doc.htmlreturn new OpenAPI().info(new Info().title("SpringBoot API").description("SpringBoot Simple Application").contact(contact).version("1.0.0"));}@Overridepublic void onApplicationEvent(WebServerInitializedEvent event) {try {//获取IPString hostAddress = Inet4Address.getLocalHost().getHostAddress();//获取端口号int port = event.getWebServer().getPort();//获取应用名String applicationName = event.getApplicationContext().getApplicationName();// TODO:这个localhost改成host地址log.info("项目启动启动成功!接口文档地址: http://" + hostAddress + ":" + port + applicationName + "/doc.html");log.info("项目启动启动成功!接口文档地址: http://" + hostAddress + ":" + port + applicationName + "/swagger-ui/index.html");} catch (UnknownHostException e) {e.printStackTrace();}}
}
MybatisPlus配置类
package cn.lhz.config;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;/*** @author 李昊哲* @version 1.0.0*/
@Configuration
public class MybatisPlusConfig {@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();// 于 v3.5.9 起,PaginationInnerInterceptor 已分离出来。如需使用,则需单独引入 mybatis-plus-jsqlparser 依赖interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));return interceptor;}
}
全局异常处理
package cn.lhz.config;import cn.lhz.util.response.ResponseResult;
import cn.lhz.util.response.ResultCode;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;/*** 全局异常处理** @author 李昊哲* @version 1.0.0*/
@ControllerAdvice
@RestControllerAdvice
public class GlobalException {@ExceptionHandler(Exception.class)public ResponseResult<String> defaultErrorHandler(Exception e) {return new ResponseResult<>(ResultCode.EXCEPTION.getCode(), e.getMessage());}
}
模型类
package cn.lhz.dto;import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.*;/*** @author 李昊哲* @version 1.0.0*/
@Setter
@Getter
@ToString
@NoArgsConstructor
@AllArgsConstructor
@TableName("person")
public class Person {@TableId(type = IdType.NONE)private String idCard;private String realName;private String mobile;private String provinceCode;
}
持久层接口
package cn.lhz.mapper;import cn.lhz.dto.Person;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;/*** @author 李昊哲* @version 1.0.0*/
@Mapper
public interface PersonMapper extends BaseMapper<Person> {
}
业务层接口
package cn.lhz.service;import cn.lhz.dto.Person;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;/*** @author 李昊哲* @version 1.0.0*/
public interface PersonService extends IService<Person> {Page<Person> pageList(int pageNum, int pageSize);
}
业务层实现类
package cn.lhz.service.impl;import cn.lhz.dto.Person;
import cn.lhz.mapper.PersonMapper;
import cn.lhz.service.PersonService;
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 lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;/*** @author 李昊哲* @version 1.0.0*/
@Service
@RequiredArgsConstructor
public class PersonServiceImpl extends ServiceImpl<PersonMapper, Person> implements PersonService {private final PersonMapper personMapper;@Overridepublic Page<Person> pageList(int pageNum, int pageSize) {return personMapper.selectPage(new Page<>(pageNum, pageSize), new QueryWrapper<>());}
}
API接口
package cn.lhz.controller;import cn.lhz.dto.Person;
import cn.lhz.service.PersonService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;/*** @author 李昊哲* @version 1.0.0*/
@RestController
@RequiredArgsConstructor
@RequestMapping("/person")
public class PersonController {private final PersonService personService;@GetMapping("/pageList")public Page<Person> pageList(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,@RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {return personService.pageList(pageNum, pageSize);}
}