新建SpringBoot Maven项目中pom常用依赖配置及常用的依赖的介绍

您所在的位置:网站首页 常用配置文件有哪些 新建SpringBoot Maven项目中pom常用依赖配置及常用的依赖的介绍

新建SpringBoot Maven项目中pom常用依赖配置及常用的依赖的介绍

2023-10-03 04:16| 来源: 网络整理| 查看: 265

完整的pom文件放在后面

一、常用的依赖的介绍

1.springboot项目的总(父)依赖大全

spring-boot-dependencies org.springframework.boot 2.3.3.RELEASE

当我们使用 spring 或 spring-boot 开发项目时,需要引入很多依赖,包括 spring 本身的组件、各种 spring-boot-starter、以及其它第三方依赖(如:slf4j、redis)。依赖多了,版本的选择是个问题,就怕哪个版本选择的不对导致出现一些意想不到的 BUG。 spring-boot-dependencies的作用主要是起到约束版本的作用,在这个包里面声明了各种版本号,供子项目去引用。类似spring-cloud-dependencies和spring-cloud-alibaba-dependencies则是去声明cloud和cloud-alibaba组件的版本。具体有些什么可以点进去看看就知道了。如果当下面的< dependency >中用到就可以不用配置版本号< version >

2.可执行的 Web 应用且内含SpringBoot核心启动器,包含各种springboot的配置日志等,创建项目时会自动引入该依赖

支持注解:@controller、@Service、@Component、@Resource 是spring的,所以spring boot创建完成后就可以使用(由spring-boot-starter支持) 支持注解:@RestController、@RequestMapping、@ResponseBody、@JsonFormat(由spring-boot-starter-web支持)

org.springframework.boot spring-boot-starter-web

spring-boot-starter-web 是什么? spring-boot-starter-web是一个依赖库,Spring Boot 是在 Spring 的基础上创建的一个开原框架,它提供了 spring-boot-starter-web (web场景启动器)来为web开发予以支持。spring-boot-starter-web 为什么提供了嵌入的Servlet容器以及SpringMVC提供了大量自动配置,可以适用于大多数web开发场景。 只要我们在Spring Boot 项目中的 pom.xml 中引入了spring-boot-starter-web依赖,即使不进行任何配置,也可以使用Spring MVC 进行 Web 开发。Spring Web的启动程序使用Spring MVC, REST和Tomcat作为默认的嵌入式服务器。单个spring-boot-starter-web依赖关系可传递地获取与Web开发相关的所有依赖关系。它还减少了构建依赖项计数。

配置了该依赖就不用再配置

org.springframework.boot spring-boot-starter

因为spring-boot-starter-web包含了spring-boot-starter等,可以点进去看看

3.junit测试,创建项目时会自动引入该依赖

用于编写springboot Test测试类SpringBoot Test测试类的使用

org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine

当使用exclusion进行排除则该项目不支持Junit3和Junit4,如果使用就会报错,如下: 在这里插入图片描述 因为SpringBoot推荐用junit5进行单元测试,SpringBoot给我默认提供测试类

//@SpringBootTest默认回去找启动类,默认可以不配置,当项目有多个启动类的时候才需要配置 @SpringBootTest class SpringbootexceptionandjunitApplicationTests { @Test void contextLoads() { } }

4.mysql数据配置

配置mysql依赖时,不写版本号xx.xx.xx的话,就会引入mysql依赖的默认版本 SpringBoot2.1.x以后默认使用的是mysql 8版本, SpringBoot2.1.x之前默认使用的是mysql 5.x版本 在配置数据源的时候,就有差异了: 配置低版本 5.xx.xx: spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=UTF-8&useSSL=false spring.datasource.username=root spring.datasource.password=123456 配置高版本 8.xx.xx: spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/student?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8&useSSL=false spring.datasource.username=root spring.datasource.password=123456

mysql mysql-connector-java

5.mybatis

数据处理层持久层框架,连接数据库 着重点放在了编写sql上,而不是通过jdbc传统方式来不断操作Connection、Statment、ResultSet 注解@Mapper 指定映射接口 application.yaml配置文件中配置自动识别的xml: mybatis: mapper-locations: classpath:mapper/**/*.xml type-aliases-package: run.leave.mapper

org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.2

8.Druid连接池 druid和druid-spring-boot-starter 的区别与报错Cannot resolve configuration property ‘spring.datasource.xxx解决

com.alibaba druid 1.2.2 com.alibaba druid-spring-boot-starter 1.2.2

在yaml文件中配置使用:

spring: datasource: # 数据源基本配置 url: jdbc:mysql://localhost:3306/hotel?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource # 数据源其他配置 druid: # 配置初始化大小、最小、最大线程数 initialSize: 5 minIdle: 5 # CPU核数+1,也可以大些但不要超过20,数据库加锁时连接过多性能下降 maxActive: 20 # 最大等待时间,内网:800,外网:1200(三次握手1s) maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 # 配置一个连接在池中最大空间时间,单位是毫秒 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 testWhileIdle: true # 设置从连接池获取连接时是否检查连接有效性,true检查,false不检查 testOnBorrow: true # 设置从连接池归还连接时是否检查连接有效性,true检查,false不检查 testOnReturn: true # 可以支持PSCache(提升写入、查询效率) poolPreparedStatements: true # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 filters: stat,wall,log4j # 保持长连接 keepAlive: true maxPoolPreparedStatementPerConnectionSize: 20 useGlobalDataSourceStat: true connectionProperties: druid.stargeSql=true;druid.stat.slowSqlMillis=500

7.Json格式转换工具Fastjson

Fastjson 是一个 Java 库,可以将 Java 对象转换为 JSON 格式,当然它也可以将 JSON 字符串转换为 Java 对象。 Java中 Json、String、jsonObject、jsonArray格式之间互相转换

com.alibaba fastjson 1.2.78

8.lombook

lombok最优秀的就是注解了,一个注解就干掉了很多代码 实体类中的注解. @Data :直接可以省略了Get、Set方法 @Slf4j :不需要单独引入日志依赖和配置日志,直接 log.info( ) 打印日志 如何在IDE编译器 中使用lombok插件?? idea中可以直接在编译器中搜索下载,就不多阐述了 eclipse则需要从官网下载lombok.jar包,然后双击启动jar包,逐步操作,指向eclisp.exe,重启eclipse即可

org.projectlombok lombok

9.面向切面编程AOP

支持的注解:@AspectJ、@Pointcut、通知注解(如:@Before、@After等)、@Aspect和自定义注解 spring-boot-starter-aop及其使用场景说明 SpringBoot 中的 Aop 注解使用+ 自定义注解

org.springframework.boot spring-boot-starter-aop

10.Validation校验参数的实现

支持的注解:@Max,@Min等 常用注解和demo

org.springframework.boot spring-boot-starter-validation

11.Actuator 监控

主要是服务器运维使用,开发过程不常用 springboot 监控 Actuator 的设置

org.springframework.boot spring-boot-starter-actuator

12.hutool工具包

提供了很多封装方法供开发者使用

cn.hutool hutool-all 5.4.7

13.jupiter

其依赖包含了junit-jupiter-api、junit-jupiter-engine、junit-vintage-engine Junit-jupiter-api 和 junit-jupiter-engine 的区别 总结Junit4,Junit5,Jupiter之间的联系 值得一看

org.junit.jupiter junit-jupiter test

14.打包配置 用于生成部署到服务器的包 JAVA项目在服务器部署过程

org.springframework.boot spring-boot-maven-plugin repackage

15.多yaml文件配置

指定其使用那个文件,不配置下面的profiles,但创建的文件格式形如这样也是可用的 在这里插入图片描述在这里插入图片描述 在这里插入图片描述

dev true dev pro pro

16.使用properties标签统一编码和JAVA版本

UTF-8 1.8 1.8 1.8

17.mybatis-plus

在mybatis基础上的升级版工具,避免了使用mybatis时需要编写大量的xml文件

com.baomidou mybatis-plus-boot-starter 3.4.2

18.springboot热部署

修改java代码后,不用重启项目就能直接最新测试,省略了不断修改代码不断重启项目的麻烦

org.springframework.boot spring-boot-devtools true 二、完整的pom文件 4.0.0 包名 项目名 项目版本号 spring-boot-dependencies org.springframework.boot 2.3.3.RELEASE UTF-8 1.8 1.8 1.8 mysql mysql-connector-java org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.2 com.alibaba druid 1.2.2 com.alibaba druid-spring-boot-starter 1.2.2 org.springframework.boot spring-boot-starter-aop org.springframework.boot spring-boot-starter-validation org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine org.junit.jupiter junit-jupiter test org.projectlombok lombok cn.hutool hutool-all 5.4.7 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator com.alibaba fastjson 1.2.78 junit junit test org.springframework.boot spring-boot-maven-plugin repackage dev true dev pro pro


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3