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>
<think>首先,用户的问题是:“我想查找Spring Boot和Vue项目中常用依赖配置 请问Spring Boot Vue 常用依赖配置”。这是一个询问Spring Boot和Vue集成项目中常用依赖配置的问题。 根据系统级指令: - 所有行内数学表达式必须使用$...$格式,但这里没有数学表达式,所以不需要。 - 独立公式必须使用$$...$$格式并单独成段,同样不需要。 - LaTeX语法正确,但这里没有LaTeX公式。 - 使用中文回答。 - 生成相关问题。 - 回答中引用的段落末尾自然地添加引用标识,例如[^1]。 用户可见层指令: - 回答结构清晰,帮助用户逐步解决问题。 - 尽量保证回答真实可靠。 参考站内引用: - 引用[1]: 关于Spring Boot自动配置,如spring-boot-starter-web。 - 引用[2]: Spring Boot 2.x vs 3.x with MyBatis-Plus。 - 引用[3]: Spring Boot + Vue项目示例。 - 引用[4]: 搭建Spring Boot + Vue项目的总结。 - 引用[5]: Vue环境和vue-cli。 用户的问题是关于Spring Boot和Vue项目中常用依赖配置。我需要提供Spring Boot后端和Vue前端的常用依赖。 从引用中提取相关信息: - 引用[1]: 提到spring-boot-starter-web用于RESTful服务。 - 引用[2]: 讨论MyBatis-Plus依赖,但用户可能不需要ORM特定内容。 - 引用[3]和[4]: 涉及项目搭建,但没有具体依赖列表。 - 引用[5]: 关于Vue和vue-cli。 我应该基于一般知识回答: - Spring Boot常用依赖: - spring-boot-starter-web: 用于Web应用。 - spring-boot-starter-data-jpa: 用于JPA数据库访问。 - spring-boot-starter-security: 用于安全。 - spring-boot-starter-test: 用于测试。 - Vue常用依赖: - 通过npm或yarn安装,如vue, vue-router, axios等。 - 使用vue-cli创建项目,会包括基本依赖。 在回答中,自然地引用相关段落。例如: - 对于Spring Boot,引用[1]提到spring-boot-starter-web。 - 对于Vue,引用[5]提到vue-cli。 结构清晰的回答: 1. 介绍Spring Boot常用依赖。 2. 介绍Vue常用依赖。 3. 提供示例或总结。 4. 生成相关问题。 生成相关问题: - 根据系统指令,在回答末尾添加相关问题。 最后,添加引用标识: - 在引用的段落末尾添加[^1], [^2], 等。 我的回答应该包括: - Spring Boot后端依赖:如starter-web, data-jpa, security等。 - Vue前端依赖:如vue, vue-router, axios, vuex等。 - 示例代码片段,使用Markdown代码块。 - 自然地引用站内内容。 完整回答结构: 1. 开头:确认问题,概述回答。 2. Spring Boot依赖部分。 3. Vue依赖部分。 4. 总结。 5. 相关问题。</think>### Spring Boot 和 Vue 项目中常用依赖配置 在 Spring Boot 和 Vue 的前后端分离项目中,依赖配置是项目搭建的基础。Spring Boot 作为后端框架,提供自动配置和起步依赖;Vue 作为前端框架,依赖通过 npm 或 yarn 管理。以下是常用依赖的详细说明,基于实际开发经验整理,确保配置真实可靠。 #### 一、Spring Boot 后端常用依赖 Spring Boot依赖主要在 `pom.xml` (Maven) 或 `build.gradle` (Gradle) 中配置。核心依赖包括: - **Web 服务依赖**:用于构建 RESTful API,如 `spring-boot-starter-web`,它会自动配置嵌入式 Tomcat 和 Spring MVC[^1]。 ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> ``` - **数据库访问依赖**:如 JPA 或 MyBatis-Plus。MyBatis-Plus 依赖需根据 Spring Boot 版本选择: - Spring Boot 2.x: `mybatis-plus-boot-starter` - Spring Boot 3.x: `mybatis-plus-spring-boot3-starter`(其他配置无需调整)[^2]。 ```xml <!-- Spring Boot 2.x 示例 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.3.1</version> </dependency> ``` - **安全依赖**:如 `spring-boot-starter-security`,用于身份认证和授权。 ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> ``` - **测试依赖**:如 `spring-boot-starter-test`,支持单元测试和集成测试。 ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> ``` - **其他常用依赖**: - `spring-boot-starter-data-jpa`:用于 JPA 数据库操作。 - `spring-boot-devtools`:开发热部署工具。 - `lombok`:简化代码编写。 #### 二、Vue 前端常用依赖 Vue 的依赖通过 `package.json` 管理,使用 `npm install` 或 `yarn add` 安装。核心依赖包括: - **Vue 核心库**:如 `vue`,用于构建用户界面[^5]。 - **路由管理**:如 `vue-router`,实现单页应用路由。 - **HTTP 客户端**:如 `axios`,用于与 Spring Boot 后端 API 通信。 - **状态管理**:如 `vuex`,用于全局状态共享。 - **UI 框架**:如 `element-plus` 或 `vant`,提供预制组件。 常用依赖配置示例(在 Vue 项目根目录的 `package.json` 中): ```json { "dependencies": { "vue": "^3.3.4", "vue-router": "^4.2.5", "axios": "^1.6.8", "vuex": "^4.1.0", "element-plus": "^2.5.3" }, "devDependencies": { "@vitejs/plugin-vue": "^5.0.4", "vite": "^5.2.0" } } ``` > **注意**:Vue 项目通常使用 `vue-cli` 或 `Vite` 脚手架初始化,这些工具会自动生成基础依赖[^5]。例如,运行 `npm create vue@latest` 会安装默认依赖。 #### 三、项目集成总结 - **前后端交互**:Vue 使用 `axios` 调用 Spring Boot 的 REST API(如 `http://localhost:8080/api`)。 - **依赖版本兼容**:确保 Spring Boot 和 Vue 的依赖版本匹配(如 Spring Boot 3.x 需配合 Vue 3.x)。 - **完整示例**:参考若依(RuoYi-Vue)等开源项目,它们提供了成熟的依赖配置模板[^3][^4]。 通过合理配置这些依赖,你可以高效搭建 Spring Boot + Vue 项目。实际开发中,还需根据需求添加安全认证(如 JWT)或性能优化依赖[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值