Spring Boot入门教程

186 阅读3分钟

一、Spring Boot 是什么

Spring Boot 是由 Spring 官方提供的一套快速开发框架,旨在简化 Spring 应用程序的搭建和开发。使用 Spring Boot 可以非常轻松地创建一个独立的、基于 Spring 的应用程序,并且可以打包运行。

与传统的 Spring 框架相比,Spring Boot 具有以下特点:

  • 微服务支持:Spring Boot 支持构建微服务应用程序,而且与其他 Spring 生态系统的组件轻松集成。
  • 快速启动:Spring Boot 应用程序具有轻量级、嵌入式服务器,启动速度快,可打包为 jar 或 war 文件运行。
  • 自动配置:Spring Boot 根据应用程序中已添加的库(例如 Spring Data、Spring Web)等自动为应用程序进行配置,开箱即用,无需手动配置。
  • 简化的依赖管理:Spring Boot 简化了依赖的管理,通过 Maven 或 Gradle 中的 starter 坐标来配置所需依赖项,可以避免版本冲突和依赖项引入的错误。

二、准备工作

使用 Spring Boot 进行开发需要安装以下组件:

  • JDK 8或以上版本
  • Eclipse/IntelliJ IDEA/VS Code 等编辑器
  • Maven 或 Gradle 构建工具

在这里我们以 Maven 为例。

三、创建Spring Boot应用程序

在 Eclipse、IntelliJ IDEA 或 VS Code 中,可以通过 Spring Initializr 快速生成一个 Spring Boot 应用程序的基本结构。具体步骤如下:

  1. 打开 start.spring.io/
  2. 选择构建工具(Maven 或 Gradle)、Java 版本
  3. 填写 Group、Artifact、Name、Description 等信息
  4. 添加需要的依赖项(例如 Web、JPA、Security 等)
  5. 点击 Generate,下载并解压生成的项目文件。

四、搭建Spring Boot Web 应用程序

使用 Spring Boot 进行 Web 应用程序开发非常简单,只需向主类中添加 @SpringBootApplication 注释和 @RestController 或 @Controller 注释,就可以创建 RESTful Web 服务。

示例代码如下:

复制代码
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class HelloWorldApplication {
    public static void main(String[] args) {
        SpringApplication.run(HelloWorldApplication.class, args);
    }

    @GetMapping("/hello")
    public String hello() {
        return "Hello, World!";
    }
}

上述代码中,@SpringBootApplication 注释表示该应用程序是 Spring Boot 应用程序,并自动配置其他组件。@RestController 注释表示当前类是一个控制器,处理 HTTP 请求。@GetMapping("/hello") 注释表示将该方法映射到 URL 路径 /hello 上,返回字符串 "Hello, World!"。

运行应用程序后,在浏览器中访问 http://localhost:8080/hello,即可看到 "Hello, World!" 字符串。

五、使用Spring Data JPA 进行数据库交互

Spring Boot 集成了 Spring Data JPA,可以轻松地进行数据库操作。下面是一个简单的例子:

  1. 添加 jpa 依赖

在 pom.xml 文件中添加以下依赖:

复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
  1. 创建实体类

创建一个 User 实体类,包含 id、name 和 email 字段,并添加 @Entity 注释表示该类是一个 JPA 实体:

复制代码
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    private String email;

    // 省略 getter 和 setter 方法
}
  1. 创建 Repository 接口

创建一个 UserRepository 接口来管理 User 实体:

复制代码
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<User, Long> {

}

JpaRepository 继承了 PagingAndSortingRepository,提供了一套 CRUD 操作方法(增删改查)。

  1. 使用 Repository

在 controller 中引入 UserRepository 并使用:

复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired
    private UserRepository userRepository;

    @GetMapping("/users")
    public List<User> getUsers() {
        return userRepository.findAll();
    }
}

上述代码中,@Autowired 注释表示自动注入 UserRepository 对象。在 getUsers() 方法中,调用 userRepository.findAll() 方法就可以查询所有的用户信息了。

到此为止,我们已经完成了一个简单的 Spring Boot 应用程序开发。如果想深入了解更多内容,可以参考官方文档:spring.io/projects/sp…