Spring Boot 是一个基于 Spring 框架的项目构建工具,旨在简化 Spring 应用程序的开发流程。它通过内置默认配置和自动配置的方式,减少了项目搭建过程中的繁琐步骤,使得开发者可以更快速地启动和运行 Spring 应用程序。Spring Boot 的核心理念是“习惯优于配置”,即通过内置的习惯性配置,让项目能够快速运行起来,而无需过多的手动配置。
Spring Boot 的主要特点
- 「简化配置」:Spring Boot 提供了大量默认配置,减少了项目中的样板化配置,如 web.xml、数据库连接配置、日志配置等。
- 「自动配置」:Spring Boot 通过扫描类路径中的依赖库,自动配置 Spring 应用程序,减少了手动配置的需要。
- 「独立运行」:Spring Boot 支持将应用程序打包为独立的可执行 JAR 文件,简化了部署过程。
- 「开箱即用」:Spring Boot 提供了丰富的启动器(Starters),方便开发者快速集成常用的框架和库。
- 「热部署」:Spring Boot 支持热部署功能,可以在不重启服务的情况下更新代码。
如何使用 Spring Boot 快速搭建项目
1. 创建 Spring Boot 项目
「方法一:使用 Spring Initializr」
Spring Initializr 是一个在线工具,用于生成 Spring Boot 项目的骨架。你可以通过以下步骤创建项目:
- 打开浏览器,访问 https://start.spring.io/ 。
- 选择构建工具(如 Maven 或 Gradle),开发语言(Java),并选择 Spring Boot 版本。
- 添加需要的依赖项(如 Spring Web、Spring Data JPA 等)。
- 点击“Generate Project”按钮,下载项目压缩包。
- 解压项目文件,并导入到 IDE 中(如 IntelliJ IDEA 或 Eclipse)。
「方法二:使用 IDE(如 IntelliJ IDEA 或 Eclipse)」
- 打开 IDE,选择创建新项目。
- 选择 Spring Initializr 或其他 Spring Boot 插件。
- 配置项目信息(如项目名称、JDK 版本、Spring Boot 版本等)。
- 添加需要的依赖项。
- 完成项目创建并导入到 IDE 中。
2. 项目结构和依赖管理
Spring Boot 项目的典型结构如下:
src/
└── main/
├── java/
│ └── com/
│ └── example/
│ └── Application.java
├── resources/
│ ├── application.properties
│ └── static/
│ ├── css/
│ └── js/
└── webapp/
└── index.html
src/main/java
:存放 Java 源代码。src/main/resources
:存放资源文件,如配置文件、静态资源等。src/main/webapp
:存放 Web 应用的 HTML 文件。
在 pom.xml
文件中,你可以添加所需的依赖项。例如:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
3. 编写主类和控制器
在 src/main/java
目录下创建主类 Application.java
:
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
在 src/main/java
目录下创建控制器类 HelloController.java
:
package com.example;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello() {
return "Hello, Spring Boot!";
}
}
4. 运行和测试
- 在 IDE 中运行
Application.java
类。 - 打开浏览器,访问
[http://localhost:8080/hello ](http://localhost:8080/hello )
,你应该能看到“Hello, Spring Boot!”的输出。
5. 高级功能
「热部署」
Spring Boot 支持热部署功能,可以在不重启服务的情况下更新代码。你可以在 application.properties
文件中启用热部署:
spring DevTools restart enabled=true
「集成 JSP 视图」
在 pom.xml
文件中添加 JSP 相关依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
在 src/main/resources/templates
目录下创建 JSP 文件 index.jsp
:
<!DOCTYPE html>
<html>
<head>
<title>Hello Spring Boot</title>
</head>
<body>
<h1>Hello, Spring Boot!</h1>
</body>
</html>
在 src/main/java
目录下创建控制器类 IndexController.java
:
package com.example;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class IndexController {
@GetMapping("/")
public String index(Model model) {
model.addAttribute("message", "Hello, Spring Boot!");
return "index";
}
}
运行项目后,访问 [http://localhost:8080 ](http://localhost:8080 )
,你应该能看到 JSP 页面的内容。
「集成 MyBatis」
在 pom.xml
文件中添加 MyBatis 相关依赖:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
在 src/main/resources
目录下创建 MyBatis 配置文件 mybatis-config.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/springboot"/>
<property name="username" value="root"/>
<property name="password" value="password"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/example/Mapper.xml"/>
</mappers>
</configuration>
在 src/main/resources
目录下创建 MyBatis 映射文件 Mapper.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.UserMapper">
<select id="selectUser" resultType="com.example.entity.User">
SELECT * FROM users WHERE id = #{id}
</select>
</mapper>
在 src/main/java
目录下创建实体类 User.java
:
package com.example.entity;
public class User {
private int id;
private String name;
private String email;
// getters and setters
}
在 src/main/java
目录下创建 Mapper 接口 UserMapper.java
:
package com.example.mapper;
import com.example.entity.User;
import org.apache.ibatis.annotations.Select;
public interface UserMapper {
@Select("SELECT * FROM users WHERE id = #{id}")
User selectUser(int id);
}
在 src/main/java
目录下创建 Service 类 UserService.java
:
package com.example.service;
import com.example.entity.User;
import com.example.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User getUser(int id) {
return userMapper.selectUser(id);
}
}
在 src/main/java
目录下创建 Controller 类 UserController.java
:
package com.example.controller;
import com.example.entity.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/user")
public User getUser(@RequestParam int id) {
return userService.getUser(id);
}
}
运行项目后,访问 [http://localhost:8080/user](http://localhost:8080/user)?id=1
,你应该能看到用户信息。
总结
Spring Boot 是一个强大的工具,