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

这篇具有很好参考价值的文章主要介绍了新建SpringBoot Maven项目中pom常用依赖配置及常用的依赖的介绍。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

完整的pom文件放在后面

一、常用的依赖的介绍

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

<parent>
        <artifactId>spring-boot-dependencies</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.3.3.RELEASE</version>
</parent>

当我们使用 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支持)

		<!--Spring Boot Web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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开发相关的所有依赖关系。它还减少了构建依赖项计数。

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

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

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

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

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

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    <!--junit-vintage-engine提供了Junit3Junit4的运行平台-->
    <!--这个是JUnit5中为了支持使用JUint4所做的一个过度
       也就是说,你只需要在你的JUnit4旧项目中添加这个依赖,
       就能完美过渡,而不用修改之前代码
       这里用不到,自然也就排除了。当然,这里,它无关紧要
			-->
    <exclusions>
        <exclusion>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
        </exclusion>
    </exclusions>
</dependency>

当使用exclusion进行排除则该项目不支持Junit3和Junit4,如果使用就会报错,如下:
新建SpringBoot Maven项目中pom常用依赖配置及常用的依赖的介绍
因为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 连接组件-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

5.mybatis

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

		<!--MyBaits-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.2</version>
        </dependency>

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

		<!--Druid-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.2</version>
        </dependency>

        <!--    Druid Spring Boot 组件-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.2</version>
        </dependency>

在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.stat.mergeSql=true;druid.stat.slowSqlMillis=500
 

7.Json格式转换工具Fastjson

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

		<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.78</version>
        </dependency>

8.lombook

lombok最优秀的就是注解了,一个注解就干掉了很多代码
实体类中的注解.
@Data :直接可以省略了Get、Set方法
@Slf4j :不需要单独引入日志依赖和配置日志,直接 log.info( ) 打印日志

如何在IDE编译器 中使用lombok插件??
idea中可以直接在编译器中搜索下载,就不多阐述了
eclipse则需要从官网下载lombok.jar包,然后双击启动jar包,逐步操作,指向eclisp.exe,重启eclipse即可

		<!--LomBok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

9.面向切面编程AOP

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

		<!--Spring Boot Aop-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

10.Validation校验参数的实现

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

		<!--Spring Validation-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>

11.Actuator 监控

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

		<!--Spring Boot Actuator-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

12.hutool工具包

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

		<!--Hutool-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.4.7</version>
        </dependency>

13.jupiter

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

		<!--Junit-->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <scope>test</scope>
        </dependency>

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

	<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

15.多yaml文件配置

指定其使用那个文件,不配置下面的profiles,但创建的文件格式形如这样也是可用的
新建SpringBoot Maven项目中pom常用依赖配置及常用的依赖的介绍新建SpringBoot Maven项目中pom常用依赖配置及常用的依赖的介绍
新建SpringBoot Maven项目中pom常用依赖配置及常用的依赖的介绍

	<profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <profilesActive>dev</profilesActive>
            </properties>
        </profile>
        <profile>
            <id>pro</id>
            <properties>
                <profilesActive>pro</profilesActive>
            </properties>
        </profile>
    </profiles>

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

<!--统一编码和JAVA版本-->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <java.version>1.8</java.version>
    </properties>

17.mybatis-plus

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

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.2</version>
</dependency>

18.springboot热部署

修改java代码后,不用重启项目就能直接最新测试,省略了不断修改代码不断重启项目的麻烦文章来源地址https://www.toymoban.com/news/detail-461460.html

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
</dependency>

二、完整的pom文件

<?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>包名</groupId>
    <artifactId>项目名</artifactId>
    <version>项目版本号</version>

    <parent>
        <artifactId>spring-boot-dependencies</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.3.3.RELEASE</version>
    </parent>

	<!--统一编码和JAVA版本-->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <!--MySQL 连接组件-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <!--MyBaits-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.2</version>
        </dependency>

        <!--Druid-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.2</version>
        </dependency>

        <!--    Druid Spring Boot 组件-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.2</version>
        </dependency>

        <!--Spring Boot Aop-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

        <!--Spring Validation-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>

        <!--Spring Boot 测试-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!--Junit-->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <scope>test</scope>
        </dependency>

        <!--LomBok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <!--Hutool-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.4.7</version>
        </dependency>

        <!--Spring Boot Web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--Spring Boot Actuator-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.78</version>
        </dependency>


        <!--Junit测试-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <profilesActive>dev</profilesActive>
            </properties>
        </profile>
        <profile>
            <id>pro</id>
            <properties>
                <profilesActive>pro</profilesActive>
            </properties>
        </profile>
    </profiles>

</project>

到了这里,关于新建SpringBoot Maven项目中pom常用依赖配置及常用的依赖的介绍的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • idea新建Springboot项目,设置默认maven和jdk版本

    问题: 由于每次新建Springboot项目,maven总是在c盘和jdk版本不是自己需要的版本。每次都需要自己重新配置。 解决: 为了解决这个问题,设置新建项目时指定默认配置。 一、设置新建项目时,默认指定的Maven版本 1.file–》Other Settings–》settinf for… 2.找到Maven配置。配置上即

    2024年02月16日
    浏览(38)
  • idea maven聚合工程pom依赖

    spring boot 与 spring cloud 与 spring.cloud.alibaba 版本选择

    2024年02月16日
    浏览(36)
  • IntelliJ IDEA中下载Maven依赖、maven导入pom包

    此方法可以在IDEA中重新下载Maven依赖 1.点击IDEA界面右侧Maven Projects 2.点击小M图标(如下图) 3.等待加载完成,有点长,需等待 4.最后点击两个箭头的小圆圈刷新Reimport即可… idea 中 maven pom不自动更新的5种解决方法_建仔的博客专栏-CSDN博客_idea pom 点击加号导入pom文件 点击 M

    2024年02月11日
    浏览(38)
  • maven依赖 pom.xml中systemPath的用法

    今天在给一个小伙伴配置项目中有一个jar包依赖怎么就下载不下来,关键是我使用他的maven本地仓库打包还报错。首先这个依赖maven官网仓库是存在的,也是可以下载的,但是他本地就是下载不下 来。尝试了很多的方法,搞笑的是还给他重新安装了另一个版本的maven。但是本地

    2024年02月07日
    浏览(46)
  • 【IntelliJ IDEA】idea修改设置默认maven,解决每次新建和导入项目都需要重新配置maven

    本文目录 一、开发工具 二、问题描述 三、解决方案 开发工具:IntelliJ IDEA 工具版本:Ultimate 2020.3 使用 idea 开发工具每次打开一个已有项目时,都需要重新配置一下 maven(Maven home path 和 User settings file)。这个问题出现好久了,问题不严重,但是特别烦人。 设置 Maven 路径和

    2024年02月14日
    浏览(48)
  • eclipse中在maven工程 的pom.xml文件中增加依赖的方法

    方法1:直接编辑pom.xml文件增加依赖 直接编辑pom.xml文件肯定是可以了,不管是否在eclipse中,挺方便的。 例如,从maven仓库中找到自己需要依赖的插件,里边已经将依赖的配置代码写好了,直接拷贝到maven工程的pom.xml文件对应位置即可,也很方便: 方法2:利用eclipse的图形化

    2024年02月13日
    浏览(38)
  • 【Maven】Maven的新建、使用、安装配置、集成配置到eclipse,Maven项目测试servlet,Maven容易出现的问题(看这一篇你大概就会了!别不信)

    目录 一、引言 --- maven的介绍 1、什么是 Maven? 2、Maven的作用 3、如何使用 Maven? 4、在什么环境下使用 Maven? 5、Maven的使用效果 二、maven安装及配置 1、下载解压安装 2、配置 ①环境变量配置 ②Maven插件安装与配置 三、集成配置eclipse 四、新建 Maven项目及细节配置 1、Maven项目

    2024年02月04日
    浏览(38)
  • JAVA (MAVEN项目)添加JUnit依赖配置(亲测有效)

    本教程针对Maven项目,Spring Boot或者微服务平台都适用。(亲测有效) File--Seetings--Plugins 安装JUnit和JUnitGenerator V2.0   1、双击选择要测试的类,使用快捷键ctrl+shift+t 2、选择JUnit创建测试类 3、项目运行(右键run或者debug模式运行)   1、新建class,手动添加@Test注解,右键run运行

    2023年04月20日
    浏览(69)
  • Springboot idea 中 maven配置问题,找不到依赖:Could not find artifact xxxx

    现象:当我们从代码仓拉取新项目时,从该项目的开发同事拿到其maven的settings文件,作为项目的maven配置,为了是能找到工程中所依赖的包,能从远程仓下载下来。 然后本地仓的包,也从同事那边拷贝一份过来,直接运行mvn -install 会显示找不到xxxx包 1、本地我们也已经同步

    2024年02月09日
    浏览(31)
  • 简介maven核心:pom项目对象模型

    Maven 意思是知识的积累者,最初是为了简化 Jakarta Turbine 项目中的构建过程。有几个项目,每个项目都有自己的 Ant 构建文件,它们都略有不同。JAR 被检入 CVS。我们想要一种标准的方式来构建项目,清楚地定义项目的组成,发布项目信息的简单方法,以及在多个项目之间共享

    2024年03月09日
    浏览(42)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包