zl程序教程

您现在的位置是:首页 >  后端

当前栏目

SpringBoot 快速入门详解程序员

2023-06-13 09:19:57 时间

SpringBoot 快速入门详解程序员

一、SpringBoot概述

Build Anything with Spring Boot:Spring Boot is the starting point for building all Spring-based applications. Spring Boot is designed to get you up and running as quickly as possible, with minimal upfront configuration of Spring.

官网的原文,意为:SpringBoot是所有基于Spring开发的项目的起点。SpringBoot的设计是为了让你尽可能快的跑起来Spring应用程序并且尽可能减少你的配置文件。

1、什么是SpringBoot

(1)它使用“习惯优于配置”(项目中存在大量的配置,此外还内置一个习惯性的配置,让你无须去关心繁琐的配置文件)的理念让你的项目快速运行起来。

(2)它并不是什么新框架,而是默认配置了很多框架的使用方式。就像Maven整合了所有的jar包一样。SpringBoot整合了所有框架。

2、使用SpringBoot有什么好处

回顾我们之前的 SSM 项目,搭建过程还是比较繁琐的,需要

1)配置 web.xml,加载 spring 和 spring mvc 2)配置数据库连接、配置日志文件 3)配置家在配置文件的读取,开启注解 4)配置mapper文件

而使用 Spring Boot 来开发项目则只需要非常少的几个配置就可以搭建起来一个 Web 项目,并且利用 IDEA 可以自动生成生成,这样极大的提高了开发效率。

划重点:简单、快速、方便地搭建项目;对主流开发框架的无配置集成;极大提高了开发、部署效率。


二、Spring Boot快速搭建

第一步:新建项目

SpringBoot 快速入门详解程序员

修改项目信息

 SpringBoot 快速入门详解程序员

勾选上web模块

 SpringBoot 快速入门详解程序员

选择好项目的位置,点击Finish

 SpringBoot 快速入门详解程序员

如果是第一次配置Spring Boot的话可能需要等待一会IDEA下载相应的依赖包,默认创建好的项目结构如下:

SpringBoot 快速入门详解程序员

项目结构还是看上去挺清爽的,少了很多配置文件,我们来了解一下默认生成的有什么:

SpringbootApplication: 一个带有 main() 方法的类,用于启动应用程序 SpringbootApplicationTests:一个空的 Junit 测试了,它加载了一个使用 Spring Boot 字典配置功能的 Spring 应用程序上下文 application.properties:一个空的 properties 文件,可以根据需要添加配置属性 pom.xml: Maven 构建说明文件

第二步:HelloController

在【com.google.springboot】包下新建一个【HelloController】:


import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; * 测试控制器 * @author: 郭先生 * @create: 2018-05-31-下午 14:46 @RestController public class HelloController { @RequestMapping("/hello") public String hello(){ return "Hello Spring Boot!";

 @RestController 注解: 该注解是 @Controller 和 @ResponseBody 注解的合体版

第三步:利用IDEA启动Spring Boot

我们回到 SpringbootApplication 这个类中,然后右键点击运行:

SpringBoot 快速入门详解程序员

注意:我们之所以在上面的项目中没有手动的去配置 Tomcat 服务器,是因为 Spring Boot 内置了 Tomcat

等待一会儿就会看到下方的成功运行的提示信息:

SpringBoot 快速入门详解程序员

可以看到我们的 Tomcat 运行在 8080 端口,我们来访问 “/hello” 地址试一下:

SpringBoot 快速入门详解程序员

可以看到页面成功显示出我们返回的信息。

三、解析SpringBoot项目 1、解析pom.xml文件

让我们来看看默认生成的 pom.xml 文件中到底有一些什么特别:


 ?xml version="1.0" encoding="UTF-8"? 

 project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" 

 modelVersion 4.0.0 /modelVersion 

 groupId com.google /groupId 

 artifactId springboot /artifactId 

 version 0.0.1-SNAPSHOT /version 

 packaging jar /packaging 

 name springboot /name 

 description Demo project for Spring Boot /description 

 parent 

 groupId org.springframework.boot /groupId 

 artifactId spring-boot-starter-parent /artifactId 

 version 2.0.2.RELEASE /version 

 relativePath/ !-- lookup parent from repository -- 

 /parent 

 properties 

 project.build.sourceEncoding UTF-8 /project.build.sourceEncoding 

 project.reporting.outputEncoding UTF-8 /project.reporting.outputEncoding 

 java.version 1.8 /java.version 

 /properties 

 dependencies 

 dependency 

 groupId org.springframework.boot /groupId 

 artifactId spring-boot-starter-web /artifactId 

 /dependency 

 dependency 

 groupId org.springframework.boot /groupId 

 artifactId spring-boot-starter-test /artifactId 

 scope test /scope 

 /dependency 

 /dependencies 

 build 

 plugins 

 plugin 

 groupId org.springframework.boot /groupId 

 artifactId spring-boot-maven-plugin /artifactId 

 /plugin 

 /plugins 

 /build 


我们可以看到一个比较陌生一些的标签 parent ,这个标签是在配置 Spring Boot 的父级依赖:

 parent 

 groupId org.springframework.boot /groupId 

 artifactId spring-boot-starter-parent /artifactId 

 version 2.0.1.RELEASE /version 

 relativePath/ !-- lookup parent from repository -- 

 /parent 

有了这个,当前的项目才是 Spring Boot 项目,spring-boot-starter-parent 是一个特殊的 starter ,它用来提供相关的 Maven 默认依赖,使用它之后,常用的包依赖就可以省去 version 标签。

关于具体 Spring Boot 提供了哪些 jar 包的依赖,我们可以查看本地 Maven 仓库下:/repository/org/springframework/boot/spring-boot-dependencies/2.0.1.RELEASE/spring-boot-dependencies-2.0.1.RELEASE.pom 文件来查看,挺长的

2、应用入口类

Spring Boot 项目通常有一个名为 *Application 的入口类,入口类里有一个 main 方法, 这个 main 方法其实就是一个标准的 Javay 应用的入口方法。

[email protected]** 是 Spring Boot 的核心注解,它是一个组合注解,该注解组合了:[email protected]@[email protected];** 若不是用 @SpringBootApplication 注解也可以使用这三个注解代替。

其中,[email protected] 让 Spring Boot 根据类路径中的 jar 包依赖为当前项目进行自动配置**,例如,添加了 spring-boot-starter-web 依赖,会自动添加 Tomcat 和 Spring MVC 的依赖,那么 Spring Boot 会对 Tomcat 和 Spring MVC 进行自动配置。 Spring Boot 还会自动扫描 @SpringBootApplication 所在类的同级包以及下级包里的 Bean ,所以入口类建议就配置在 grounpID + arctifactID 组合的包名下(这里为 cn.wmyskxz.springboot 包) 3、SpringBoot的配置文件

Spring Boot 使用一个全局的配置文件 application.properties 或 application.yml,放置在【src/main/resources】目录或者类路径的 /config 下。

Spring Boot 不仅支持常规的 properties 配置文件,还支持 yaml 语言的配置文件。yaml 是以数据为中心的语言,在配置数据的时候具有面向对象的特征。

Spring Boot 的全局配置文件的作用是对一些默认配置的配置值进行修改。


SpringBoot 快速入门详解程序员

我们同样的将 Tomcat 默认端口设置为 8080 ,并将默认的访问路径从 “/” 修改为 “/hello” 时,使用 properties 文件和 yml 文件的区别如上图。

注意: yml 需要在 “:” 后加一个空格,幸好 IDEA 很好地支持了 yml 文件的格式有良好的代码提示;

SpringBoot 快速入门详解程序员

我们可以把配置信息封装成一个类,首先在我们的 name 和 age 前加一个 student 前缀,然后新建一个 StudentProperties 的类用来封装这些信息,并用上两个注解:

@Component:表明当前类是一个 Java Bean @ConfigurationProperties(prefix = student ):表示获取前缀为 sutdent 的配置信息

这样我们就可以在控制器中使用,重启得到正确信息:

SpringBoot 快速入门详解程序员

4、SpringBoot热部署

在目前的 Spring Boot 项目中,当发生了任何修改之后我们都需要重新启动才能够正确的得到效果,这样会略显麻烦,Spring Boot 提供了热部署的方式,当发现任何类发生了改变,就会通过 JVM 类加载的方式,加载最新的类到虚拟机中,这样就不需要重新启动也能看到修改后的效果了。


groupId org.springframework.boot /groupId artifactId spring-boot-devtools /artifactId optional true /optional !-- 这个需要为 true 热部署才有效 -- /dependency

重新启动 Spring Boot ,然后修改任意代码,就能观察到控制台的自动重启现象:

SpringBoot 快速入门详解程序员

5、SpringBoot使用

上面已经完成了 Spring Boot 项目的简单搭建,我们仅仅需要进行一些简单的设置,写一个 HelloController 就能够直接运行了,不要太简单 接下来我们再深入了解一下 Spring Boot 的使用。

Spring Boot 支持 JSP

Spring Boot 的默认视图支持是 Thymeleaf 模板引擎,但是这个我们不熟悉啊,我们还是想要使用 JSP 怎么办呢?


groupId org.mybatis.spring.boot /groupId artifactId mybatis-spring-boot-starter /artifactId version 1.1.1 /version /dependency !-- mysql -- dependency groupId mysql /groupId artifactId mysql-connector-java /artifactId version 5.1.21 /version /dependency

在【cn.wmyskxz.springboot】下新建一个【pojo】包,然后在其下创建一个 Student 类:

public class Student { 

 private Integer id; 

 private Integer student_id; 

 private String name; 

 private Integer age; 

 private String sex; 

 private Date birthday; 

 /* getter and setter */ 

}

在【cn.wmyskxz.springboot】下新建一个【mapper】包,然后在其下创建一个 StudentMapper 映射类:

package cn.wmyskxz.springboot.mapper; 

import cn.wmyskxz.springboot.pojo.Student; 

import org.apache.ibatis.annotations.Mapper; 

import org.apache.ibatis.annotations.Select; 

import java.util.List; 

@Mapper 

public interface StudentMapper { 

 @Select("SELECT * FROM student") 

 List Student findAll(); 

}

在【cn.wmyskxz.springboot】下新建一个【controller】包,然后在其下创建一个 StudentController :

package cn.wmyskxz.springboot.controller; 

import cn.wmyskxz.springboot.mapper.StudentMapper; 

import cn.wmyskxz.springboot.pojo.Student; 

import org.springframework.beans.factory.annotation.Autowired; 

import org.springframework.stereotype.Controller; 

import org.springframework.ui.Model; 

import org.springframework.web.bind.annotation.RequestMapping; 

import java.util.List; 

 * Student 控制器 

 * @author: @我没有三颗心脏 

 * @create: 2018-05-08-下午 20:25 

@Controller 

public class StudentController { 

 @Autowired 

 StudentMapper studentMapper; 

 @RequestMapping("/listStudent") 

 public String listStudent(Model model) { 

 List Student students = studentMapper.findAll(); 

 model.addAttribute("students", students); 

 return "listStudent"; 

}

第五步:编写 listStudent.jsp 文件

我们简化一下 JSP 的文件,仅显示两个字段的数据:

 %@ page language="java" contentType="text/html; charset=UTF-8" 

 pageEncoding="UTF-8"% 

 %@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"% 

 table align=center border=1 cellspacing=0 

 td id /td 

 td name /td 

 /tr 

 c:forEach items="${students}" var="s" varStatus="st" 

 td ${s.id} /td 

 td ${s.name} /td 

 /tr 

 /c:forEach 

 /table