Spring Boot是什么?如何使用Spring Boot快速搭建项目?

Spring Boot 是一个基于 Spring 框架的项目构建工具,旨在简化 Spring 应用程序的开发流程。它通过内置默认配置和自动配置的方式,减少了项目搭建过程中的繁琐步骤,使得开发者可以更快速地启动和运行 Spring 应用程序。Spring Boot 的核心理念是“习惯优于配置”,即通过内置的习惯性配置,让项目能够快速运行起来,而无需过多的手动配置。

Spring Boot 的主要特点

  1. 「简化配置」:Spring Boot 提供了大量默认配置,减少了项目中的样板化配置,如 web.xml、数据库连接配置、日志配置等。
  2. 「自动配置」:Spring Boot 通过扫描类路径中的依赖库,自动配置 Spring 应用程序,减少了手动配置的需要。
  3. 「独立运行」:Spring Boot 支持将应用程序打包为独立的可执行 JAR 文件,简化了部署过程。
  4. 「开箱即用」:Spring Boot 提供了丰富的启动器(Starters),方便开发者快速集成常用的框架和库。
  5. 「热部署」:Spring Boot 支持热部署功能,可以在不重启服务的情况下更新代码。

如何使用 Spring Boot 快速搭建项目

1. 创建 Spring Boot 项目

「方法一:使用 Spring Initializr」

Spring Initializr 是一个在线工具,用于生成 Spring Boot 项目的骨架。你可以通过以下步骤创建项目:

  1. 打开浏览器,访问 https://start.spring.io/ 
  2. 选择构建工具(如 Maven 或 Gradle),开发语言(Java),并选择 Spring Boot 版本。
  3. 添加需要的依赖项(如 Spring Web、Spring Data JPA 等)。
  4. 点击“Generate Project”按钮,下载项目压缩包。
  5. 解压项目文件,并导入到 IDE 中(如 IntelliJ IDEA 或 Eclipse)。

「方法二:使用 IDE(如 IntelliJ IDEA 或 Eclipse)」

  1. 打开 IDE,选择创建新项目。
  2. 选择 Spring Initializr 或其他 Spring Boot 插件。
  3. 配置项目信息(如项目名称、JDK 版本、Spring Boot 版本等)。
  4. 添加需要的依赖项。
  5. 完成项目创建并导入到 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. 运行和测试
  1. 在 IDE 中运行 Application.java 类。
  2. 打开浏览器,访问 [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 是一个强大的工具,

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值