Spring Boot项目的常用依赖有哪些?

在Spring Boot项目开发中,依赖的选择取决于具体业务需求。以下是常用的依赖分类及说明,涵盖Web开发、数据访问、工具类等场景:

一、核心基础依赖

  1. Spring Web

    • 作用:提供Web开发支持,包含Spring MVC和内置Tomcat服务器,用于处理HTTP请求、路由映射等。

    • Maven坐标

      xml 复制代码
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
  2. Spring Boot Starter

    • 作用:Spring Boot的核心启动器,提供自动配置、日志、YAML解析等基础功能,其他starter通常依赖它。
    • 说明 :一般无需单独引入,其他starter(如spring-boot-starter-web)会自动包含。

二、数据访问依赖

  1. MySQL Driver

    • 作用:MySQL数据库驱动,用于连接MySQL数据库。

    • Maven坐标

      xml 复制代码
      <dependency>
          <groupId>com.mysql</groupId>
          <artifactId>mysql-connector-j</artifactId>
          <scope>runtime</scope>  <!-- 运行时生效 -->
      </dependency>
  2. MyBatis Framework

    • 作用:轻量级ORM框架,简化数据库操作,支持XML或注解方式编写SQL。

    • Maven坐标

      xml 复制代码
      <dependency>
          <groupId>org.mybatis.spring.boot</groupId>
          <artifactId>mybatis-spring-boot-starter</artifactId>
          <version>3.0.3</version>  <!-- 版本需与Spring Boot兼容 -->
      </dependency>
  3. Spring Data JPA

    • 作用:基于JPA的持久层框架,通过注解自动生成SQL,简化CRUD操作,适合快速开发。

    • Maven坐标

      xml 复制代码
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-data-jpa</artifactId>
      </dependency>
  4. Redis

    • 作用:操作Redis缓存数据库,用于提升读取性能(如会话存储、热点数据缓存)。

    • Maven坐标

      xml 复制代码
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-data-redis</artifactId>
      </dependency>

三、工具类依赖

  1. Lombok

    • 作用 :通过注解(如@Data@Getter)自动生成实体类的getter/setter、构造函数等,减少模板代码。

    • Maven坐标

      xml 复制代码
      <dependency>
          <groupId>org.projectlombok</groupId>
          <artifactId>lombok</artifactId>
          <optional>true</optional>  <!-- 避免传递依赖 -->
      </dependency>
    • 注意:需在IDE中安装Lombok插件才能正常使用。

  2. Spring Boot DevTools

    • 作用:开发工具,支持热部署(修改代码后自动重启),提升开发效率。

    • Maven坐标

      xml 复制代码
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-devtools</artifactId>
          <optional>true</optional>
      </dependency>

四、测试依赖

  1. Spring Boot Starter Test
    • 作用:整合JUnit、Mockito等测试框架,支持单元测试和集成测试。

    • Maven坐标

      xml 复制代码
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
          <scope>test</scope>  <!-- 仅测试环境生效 -->
      </dependency>

五、安全依赖

  1. Spring Security
    • 作用:提供身份认证、授权、防CSRF等安全功能,保护接口访问。

    • Maven坐标

      xml 复制代码
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-security</artifactId>
      </dependency>

六、其他常用依赖

  1. Thymeleaf

    • 作用:模板引擎,用于开发动态HTML页面(适合前后端不分离场景)。

    • Maven坐标

      xml 复制代码
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-thymeleaf</artifactId>
      </dependency>
  2. Validation

    • 作用 :提供参数校验功能(如@NotNull@Size),简化输入验证逻辑。

    • Maven坐标

      xml 复制代码
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-validation</artifactId>
      </dependency>

依赖管理建议

  • 使用Spring Initializr创建项目时,可直接勾选所需依赖,工具会自动处理版本兼容性。
  • 避免引入不必要的依赖,减少项目体积和冲突风险。
  • 版本号通常由spring-boot-dependencies统一管理,无需手动指定(特殊情况除外)。

根据项目类型(如REST接口服务、后台管理系统、微服务等)选择合适的依赖组合,可显著提升开发效率。

相关推荐
王八八。1 天前
linux后台java、postSQL部署命令
java·linux·运维
月落归舟1 天前
MyBatis缓存机制
java·缓存·mybatis
鹿导的通天塔1 天前
99%的人都不知道Codex 的 goal 神技!完整设置及提示词模板教学
后端
huipeng9261 天前
企业级微服务开发实战(一):项目启动与工程化设计
java·开发语言·spring boot·spring cloud·微服务·云原生·架构
爱吃土豆的马铃薯ㅤㅤㅤㅤㅤㅤㅤㅤㅤ1 天前
java实现excel导入、下载模板方法
java·开发语言·excel
段ヤシ.1 天前
回顾Java知识点,面试题汇总Day12(持续更新)
java·mybatis
ltl1 天前
Transformer 原论文怎么训出来的:8 张 P100、12 小时、warmup 4000 步
后端
why技术1 天前
AI Coding开始进入第四个时代,我还没上车呢!
前端·人工智能·后端
java1234_小锋1 天前
Spring AI 2.0 开发Java Agent智能体 - MCP(模型上下文协议)
java·人工智能·spring·spring ai
seven97_top1 天前
两小时入门Sentinel
java·sentinel