当前位置: 首页 > news >正文

Spring boot创建时常用的依赖

新建SpringBoot Maven项目中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>
</dependency>

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>
    <scope>runtime</scope>
</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-->
		<!--可以不配这个因为druid-spring-boot-starter里面已经有了,随便带着一下这个依赖,代码可读性高一点,反正对其他啥也没影响-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.8</version>
        </dependency>

        <!--    Druid Spring Boot 组件-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.8</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,但创建的文件格式形如这样也是可用的
在这里插入图片描述在这里插入图片描述
在这里插入图片描述

<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代码后,不用重启项目就能直接最新测试,省略了不断修改代码不断重启项目的麻烦

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

相关文章:

  • flask_restx 定义任意类型参数
  • MySQL主从延迟分析
  • JVM、MySQL常见面试题(尽力局)
  • 蓝桥杯P19718-回文字符串 题解
  • 从头开始开发基于虹软SDK的人脸识别考勤系统(python+RTSP开源)(二)
  • 【HarmonyOS Next】鸿蒙应用加载SVG文件显示图标
  • ArcGIS Pro字段编号相关代码
  • Kafka常用指令(详细)
  • 2025华为OD机试真题最新题库 (B+C+D+E卷) + 在线OJ在线刷题使用说明(C++、Java、Python合集)(正在更新E卷,目前已收录581道)
  • 【时序图】1.StarUML绿化
  • Scala编程_实现Rational的基本操作
  • 远程监控项目描述以及总体框架
  • C# 泛型中的协变、抗变和裂变:概念与应用
  • SSM框架
  • NLP常见任务专题介绍(1)-关系抽取(Relation Extraction, RE)任务训练模板
  • SSH可以连接成功,但VSCode连接不成功的问题
  • 【GPT入门】第6课 openai接口介绍与参数说明
  • 【09】单片机编程核心技巧:变量赋值,从定义到存储的底层逻辑
  • blender学习25.3.11
  • visual studio 2022最常用的快捷键
  • 一种声音·阿甘本|即将到来的中世纪;“新”与“旧”……
  • 网易一季度净利增长三成,丁磊:高度重视海外游戏市场
  • 澳大利亚首例“漂绿”诉讼开庭:能源巨头因“碳中和”承诺遭起诉
  • 【社论】打破“隐形高墙”,让老年人更好融入社会
  • 张广智︱“编年事辑”:打开学人心路历程的窗户
  • 人民日报评外卖平台被约谈:摒弃恶性竞争,实现行业健康发展