springboot整合hive
pom.xml
<?xml version="1.0" encoding="UTF-8"?><projectxmlns="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-api
mvc:path match:matching-strategy: ant_path_matcher
jackson: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+8
thymeleaf:#缓冲的配置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.DruidDataSource
driver-class-name: org.apache.hive.jdbc.HiveDriver
url: jdbc:hive2://lihaozhe03:10000/lihaozhe
username: root
password: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.vo
configuration:#配置返回数据库(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 falsespringfox:documentation:enabled:true
工具类
util 包下的工具类
opeapi配置类
swagger配置类
packagecn.lhz.config;importio.swagger.v3.oas.models.OpenAPI;importio.swagger.v3.oas.models.info.Contact;importio.swagger.v3.oas.models.info.Info;importlombok.extern.slf4j.Slf4j;importorg.springframework.boot.web.context.WebServerInitializedEvent;importorg.springframework.context.ApplicationListener;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importjava.net.Inet4Address;importjava.net.UnknownHostException;/**
* @author 李昊哲
* @version 1.0.0
*/@Configuration@Slf4jpublicclassOpenApiConfigimplementsApplicationListener<WebServerInitializedEvent>{@BeanpublicOpenAPIspringOpenAPI(){Contact contact =newContact();
contact.setName("李昊哲");
contact.setUrl("https://space.bilibili.com/480308139");
contact.setEmail("[email protected]");// 访问路径:http://localhost:8080/swagger-ui/index.html// 访问路径:http://localhost:8080/doc.htmlreturnnewOpenAPI().info(newInfo().title("SpringBoot API").description("SpringBoot Simple Application").contact(contact).version("1.0.0"));}@OverridepublicvoidonApplicationEvent(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配置类
packagecn.lhz.config;importcom.baomidou.mybatisplus.annotation.DbType;importcom.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;importcom.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;/**
* @author 李昊哲
* @version 1.0.0
*/@ConfigurationpublicclassMybatisPlusConfig{@BeanpublicMybatisPlusInterceptormybatisPlusInterceptor(){MybatisPlusInterceptor interceptor =newMybatisPlusInterceptor();// 于 v3.5.9 起,PaginationInnerInterceptor 已分离出来。如需使用,则需单独引入 mybatis-plus-jsqlparser 依赖
interceptor.addInnerInterceptor(newPaginationInnerInterceptor(DbType.MYSQL));return interceptor;}}
全局异常处理
packagecn.lhz.config;importcn.lhz.util.response.ResponseResult;importcn.lhz.util.response.ResultCode;importorg.springframework.web.bind.annotation.ControllerAdvice;importorg.springframework.web.bind.annotation.ExceptionHandler;importorg.springframework.web.bind.annotation.RestControllerAdvice;/**
* 全局异常处理
*
* @author 李昊哲
* @version 1.0.0
*/@ControllerAdvice@RestControllerAdvicepublicclassGlobalException{@ExceptionHandler(Exception.class)publicResponseResult<String>defaultErrorHandler(Exception e){returnnewResponseResult<>(ResultCode.EXCEPTION.getCode(), e.getMessage());}}
模型类
packagecn.lhz.dto;importcom.baomidou.mybatisplus.annotation.IdType;importcom.baomidou.mybatisplus.annotation.TableField;importcom.baomidou.mybatisplus.annotation.TableId;importcom.baomidou.mybatisplus.annotation.TableName;importlombok.*;/**
* @author 李昊哲
* @version 1.0.0
*/@Setter@Getter@ToString@NoArgsConstructor@AllArgsConstructor@TableName("person")publicclassPerson{@TableId(type =IdType.NONE)privateString idCard;privateString realName;privateString mobile;privateString provinceCode;}
持久层接口
packagecn.lhz.mapper;importcn.lhz.dto.Person;importcom.baomidou.mybatisplus.core.mapper.BaseMapper;importorg.apache.ibatis.annotations.Mapper;/**
* @author 李昊哲
* @version 1.0.0
*/@MapperpublicinterfacePersonMapperextendsBaseMapper<Person>{}
业务层接口
packagecn.lhz.service;importcn.lhz.dto.Person;importcom.baomidou.mybatisplus.extension.plugins.pagination.Page;importcom.baomidou.mybatisplus.extension.service.IService;/**
* @author 李昊哲
* @version 1.0.0
*/publicinterfacePersonServiceextendsIService<Person>{Page<Person>pageList(int pageNum,int pageSize);}
业务层实现类
packagecn.lhz.service.impl;importcn.lhz.dto.Person;importcn.lhz.mapper.PersonMapper;importcn.lhz.service.PersonService;importcom.baomidou.mybatisplus.core.conditions.query.QueryWrapper;importcom.baomidou.mybatisplus.extension.plugins.pagination.Page;importcom.baomidou.mybatisplus.extension.service.impl.ServiceImpl;importlombok.RequiredArgsConstructor;importorg.springframework.stereotype.Service;/**
* @author 李昊哲
* @version 1.0.0
*/@Service@RequiredArgsConstructorpublicclassPersonServiceImplextendsServiceImpl<PersonMapper,Person>implementsPersonService{privatefinalPersonMapper personMapper;@OverridepublicPage<Person>pageList(int pageNum,int pageSize){return personMapper.selectPage(newPage<>(pageNum, pageSize),newQueryWrapper<>());}}
API接口
packagecn.lhz.controller;importcn.lhz.dto.Person;importcn.lhz.service.PersonService;importcom.baomidou.mybatisplus.extension.plugins.pagination.Page;importlombok.RequiredArgsConstructor;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.bind.annotation.RestController;/**
* @author 李昊哲
* @version 1.0.0
*/@RestController@RequiredArgsConstructor@RequestMapping("/person")publicclassPersonController{privatefinalPersonService personService;@GetMapping("/pageList")publicPage<Person>pageList(@RequestParam(value ="pageNum", defaultValue ="1")int pageNum,@RequestParam(value ="pageSize", defaultValue ="5")int pageSize){return personService.pageList(pageNum, pageSize);}}
测试
本文转载自: https://blog.csdn.net/qq_24330181/article/details/143970281
版权归原作者 李昊哲小课 所有, 如有侵权,请联系我们删除。
版权归原作者 李昊哲小课 所有, 如有侵权,请联系我们删除。