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


    完整的pom文件放在后面

    一、常用的依赖的介绍

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

    <parent>
            <artifactId>spring-boot-dependencies</artifactId>
            <groupId>org.springframework.boot</groupId>
            <version>2.3.3.RELEASE</version>
    </parent>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    当我们使用 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>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    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>
    
    • 1
    • 2
    • 3
    • 4

    因为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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

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

    //@SpringBootTest默认回去找启动类,默认可以不配置,当项目有多个启动类的时候才需要配置
    @SpringBootTest
    class SpringbootexceptionandjunitApplicationTests {
    
       @Test
       void contextLoads() {
       }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    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>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    在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
     
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    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>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    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>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    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>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    10.Validation校验参数的实现

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

    		<!--Spring Validation-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-validation</artifactId>
            </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    11.Actuator 监控

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

    		<!--Spring Boot Actuator-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    12.hutool工具包

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

    		<!--Hutool-->
            <dependency>
                <groupId>cn.hutool</groupId>
                <artifactId>hutool-all</artifactId>
                <version>5.4.7</version>
            </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    15.多yaml文件配置

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

    	<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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    17.mybatis-plus

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

    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.4.2</version>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    18.springboot热部署

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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    二、完整的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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
  • 相关阅读:
    Sql中in和exists详解
    Python函数
    创意涌动:CSDN·大学生博主14天创作挑战赛·第二期,正式开启报名!
    GO语言gin框架实战-01-基本使用
    设备通过国标GB28181/海康Ehome接入EasyCVR,视频无法打开的原因分析及解决方法
    超赞:不愧是阿里内部“Spring boot学习笔记”从头到尾,全是精华
    【办公类-21-05】20240227单个word按“段落数”拆分多个Word(成果汇编 只有段落文字 1拆5)
    小程序源码:云之道知识付费独立线传版V2-2.4.9-多玩法安装简单
    【畅购商城】购物车模块之查看购物车
    线性数据结构集会
  • 原文地址:https://blog.csdn.net/munangs/article/details/126671706