一、项目整合与部署存在的问题

1.1 SSM手动整合存在的问题

  • SSM整合步骤多、配置繁琐
  • 项目进行服务器部署步骤繁琐

1.2 如何简化这些繁琐的配置和部署步骤?

  • SpringBoot就是一个可以简化整合过程中复杂配置的框架

二、SpringBoot简介

2.1 概念

  • 随着动态语⾔的流⾏,Java语⾔的开发就显得格外笨重:配置繁琐、开发效率低、项⽬的部署变得复杂、集
    成第三⽅技术难度⼤。
  • 在这种情况下,SpringBoot就应运⽽⽣。
  • SpringBoot采⽤了 **习惯优于配置/约定⼤于配置** 的理念快速的搭建项⽬的开发环境,我们⽆需或者进⾏很少的相关spring配置就能够快速的将项⽬运⾏起来

2.2 优点

  • 能够快速的搭建项⽬
  • 对主流的开发框架都提供了⽆配置集成(SpringBoot内置了配置)
  • 项⽬可以独⽴运⾏、⽆需单独配置Servlet容器(内置了Tomcat)
  • 极⼤提⾼了开发、部署效率
  • 提供了运⾏时监控系统(⽇志等)
  • 与云原⽣有天然的集成

2.3 缺点

  • 由于配置都是内置的,报错时定位⽐较困难
  • 版本迭代速度⽐较快、有些版本改动还是⽐较⼤(增加学习成本)

三、第一个SpringBoot项目

四、SpringBoot原理

4.1 starter

⼀个starter就是⼀个开发场景的⽀持( 依赖 + 配置)

SpringBoot为我们提供了简化企业级开发绝⼤多数场景的⽀持(提供了多个starter),我们在进⾏项⽬开发

的过程中只需引⼊对应的starter(创建SpringBoot应⽤时可选择),相关的依赖和配置就会被内置到项⽬中

(消除⼈⼯配置)。

4.1.1 starter依赖

⼀个starter依赖表示的不是⼀个依赖,⽽是某种开发环境所需的⼀组依赖

  • Spring Web —- spring-boot-starter-web
  • MyBatis Framework —- mybatis-spring-boot-starter

4.1.2 starter 配置

⼀个starter不仅包含所需依赖,还包含了其所需的对应的配置

4.2 SpringBoot应用的pom文件

4.3 Java配置方式

如果我们需要在SpringBoot应⽤中整合⼀种新的开发场景,只需在pom.xml引⼊对应的starter即可

⼀个starter不仅包含依赖,还包含相应的配置,starter中包含的配置都是通过Java类实现的——Java配置⽅式

4.3.1 Spring版本的发展

随着Spring版本的迭代,配置⽅式也在发⽣变化

  • Spring 1.x
    • 所有的bean的配置只能通过xml完成
  • Spring 2.x
    • 基于JDK1.5对注解的⽀持,Spring 2.x开始⽀持注解
    • 企业开发中到底是⽤xml配置还是⽤注解?
      • 对基础配置、引⽤的第三⽅依赖中的配置使⽤xml完成:例如数据源配置
      • 业务开发使⽤注解:例如controller、service
  • Spring 3.x
    • Spring 开始提供基于Java的配置⽅式
  • Spring 4.x
    • xml、注解、Java

4.4 SpringBoot自动配置

SpringBoot快速入门 - 图1

五、Spring整合JSP

SpringBoot应⽤默认⽀持的动态⽹⻚技术是Thymeleaf,并不⽀持JSP;因此在SpringBoot应⽤想要使⽤JSP需

要通过⼿动整合来实现5

5.1 添加依赖

  1. <dependency>
  2. <groupId>org.apache.tomcat.embed</groupId>
  3. <artifactId>tomcat-embed-jasper</artifactId>
  4. <version>9.0.45</version>
  5. </dependency> <dependency>
  6. <groupId>javax.servlet</groupId>
  7. <artifactId>jstl</artifactId>
  8. <version>1.2</version>
  9. </dependency>

5.2 创建JSP页面

  • 修改pom文件打包的方式
  • 在main中创建webapp目录
  • 在webapp创建.jsp⻚⾯

5.3 将JSP页面放在WEB—INF中访问

  • 将JSP⽂件存放到WEB-INF⽬录
  • 在application.yml⽂件配置SpringMVC视图解析⽅式:

    spring:
     mvc:
         view:
             prefix: /WEB-INF/
             suffix: .jsp
    
  • 创建PageController

    @Controller
    public class PageController {
      @RequestMapping("/index.html")
      public String index() {
          return "index";
      }
    }
    

六、基于SpringBoot的SSM整合

七、Thymeleaf

Thymeleaf是⼀种类似于JSP的动态⽹⻚技术

7.1 Thymeleaf简介

  • JSP必须依赖Tomcat运行,不能直接运行在浏览器中
  • HTML可以直接运行在浏览器中,但是不能接收控制器传递的数据
  • Thymeleaf是一种既保留了HTML的后缀能够直接在浏览器运行的能力,又实现了JSP显示动态数据的功能。

7.2 Thymeleaf的使用

SpringBoot应⽤对Thymeleaf提供了良好的⽀持

7.2.1 添加thymeleaf的starter

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

7.2.2 创建Thymeleaf模板

Thymeleaf模板就是HTML⽂件

  • SpringBoot应⽤中 resources\templates ⽬录就是⽤来存放⻚⾯模板的
  • 重要说明:
    • static ⽬录下的资源被定义静态资源,SpringBoot应⽤默认放⾏;如果将HTML⻚⾯创建static⽬录是可以
      直接访问的
  • templates ⽬录下的⽂件会被定义为动态⽹⻚模板,SpringBoot应⽤会拦截templates中定义的资源;如果将
    HTML⽂件定义在templates⽬录,则必须通过控制器跳转访问。
  • 在templates创建HTML⻚⾯模板
  • 创建PageController,⽤于转发允许”直接访问”的⻚⾯请求
    @Controller
    @RequestMapping("/page")
    public class PageController {
      @RequestMapping("/test.html")
      public String test(){
          return "test";
      }
    }
    

7.3 Thymeleaf的基本语法

th:text

th:inline 内联
  • HTML内联
  • CSS内联
  • JavaScript内联

th:object和*

7.4 Thymeleaf流程控制

7.4.1 th:each循环

<tr th:each="b:${books}">
    <th th:text="${b.bookId}"></th>
    <th th:text="${b.bookName}"></th>
    <th th:text="${b.bookAuthor}"></th>
    <th th:text="${b.bookPrice}"></th>
</tr>

7.4.2 分支

  • th:if 如果条件不满足,则不显示此标签

    <th th:if="${b.bookPrice}>40" style="color: red">太贵!!!</th>
    <th th:if="${b.bookPrice}<=40" style="color: green">推荐购买</th>
    
  • th:switch 和th:case

    <th th:switch="${b.bookPrice}">
     <label th:case="3">建议购买</label>
     <label th:case="4">价格合理</label>
     <label th:case="*">不建议购买</label>
    </th>
    

7.5 碎片使用

7.5.1 碎片的概念

碎片,就是HTML片段,我们可以将多个页面中使用的相同的HTML标签部分单独定义,然后通过 th:include 可以在HTML引入定义的碎片

7.5.2 碎片使用案例

  • 定义碎片 th:fragmeny

    <div th:fragment="fragment1" style="width: 100%;height: 50px;background: pink;color: brown;font-size: 30px">
         cdx的网站
    </div>
    
  • 引用碎片th:includeth:replace

    <div th:replace="header::fragment1"></div>
    <div style="width: 100%;height: 500px">主体内容</div>
    <div th:include="footer::fragment2"></div>
    

八、SpringBoot应用的热部署

8.1 热部署

项目首次部署,服务器启动之后,如果应用发生了变化,而且idea感知到应用的变化,就会自动jar的更新,无需手动再次启动服务器,就可以访问应用的更新。

8.2 热部署配置

8.2.1 IDE配置

SpringBoot快速入门 - 图2

SpringBoot快速入门 - 图3

8.2.2 SpringBoot项目配置

  • 在需要进行热部署的SpringBoot项目中添加依赖

    <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-devtools</artifactId>
    </dependency>
    
  • 配置SpringBoot的Maven插件(fork)

    <configuration>
     <fork>true</fork>
     <excludes>
         <exclude>
             <groupId>org.projectlombok</groupId>
             <artifactId>lombok</artifactId>
         </exclude>
     </excludes>
    </configuration>
    
  • 配置SpringBoot应用的变化更新策略
    SpringBoot快速入门 - 图4