SpringBoot框架详细知识总结及项目搭建:

1.简介:

springboot是spring家族中的一个全新框架,用来简化spring程序的创建和开发过程。在以往我们通过SpringMVC+Spring+Mybatis框架进行开发的时候,我们需要配置web.xml,spring配置,mybatis配置,然后整合在一起,而springboot抛弃了繁琐的xml配置过程,采用大量默认的配置来简化我们的spring开发过程。

2.特性:

1. 能够快速创建基于spring的程序
2. 能够直接使用Java main方法启动内嵌的Tomcat服务器运行springboot程序,不需要部        署war包
3. 提供约定的starter POM来简化Maven配置,让Maven的配置变得简单
4. 自动化配置,根据项目的Maven依赖配置,springboot自动配置spring、springmvc等
    提供了程序的健康检查功能
5. 基本可以完全不使用xml配合文件,采用注解配置

3.springboot 四大核心:

  自动配置、起步依赖、Actuator、命令行界面

4.SpringBoot入门案例:

(1)创建一个 Module,选择类型为Spring Initializr 快速构建

(2)设置 GAV 坐标及 pom 配置信息 

(3)选择 Spring Boot 版本及依赖 

(4)设置模块名称、Content Root (内容根目录) 路径及模块文件的目录,然后点击finish即可

 (5)项目结构如下:多余的可以删除 

 static:存放静态资源。如图片、CSS、JavaScript 等 。
 templates:存放 Web 页面的模板文件 。
 application.properties或application.yml 用于存放程序的各种依赖模块的配置信息,

           比如服务端口,数据库连接配置等。
Application.java:SpringBoot 程序执行的入口,执行该程序中的 main 方法,

            启动当前SpringBoot项目。

(6)对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">
            <!--maven 项目名称,可以删除-->
         <modelVersion>4.0.0</modelVersion>

            <!--当前项目的坐标以及版本-->
          <groupId>com.demo.springboot</groupId>
          <artifactId>001-demo-first</artifactId>
          <version>1.0-SNAPSHOT</version>

          <!-- springboot 启动时依赖的父工程,所有自己开发的 SpringBoot 都必须的继承 --
          <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.9.RELEASE</version>
          </parent>

            <!--项目编码和jdk版本号-->
     <properties>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>         <project.reporting.outputEncoding>UTF8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
      </properties>

    <dependencies>
        <!-- web支持: 1、web mvc; 2、restful; 3、tomcat支持;
        -SpringBoot 框架 web 项目起步依赖,通过该依赖自动关联其它依赖,
        不需要我们一个一个去添加
        -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- 热启动配置 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

        <!-- 单元测试 -->
        <!--SpringBoot 框架的测试起步依赖,例如:junit 测试,如果不需要的话可以删除-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

</dependencies>
    <build>
        <plugins>
            <!-- 运行jar包插件 -->
            <!--在cmd中输入: java -jar E:\maven\repository\com\bw\spring-boot\spring-boot\0.0.1-SNAPSHOT\spring-boot-0.0.1-SNAPSHOT.jar -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

5.创建一个SpringBootController

 (1)创建SpringBootController 类

注意:新创建的类一定要位于 Application 同级目录或者下级目录,

           否则SpringBoot 加载不到。 

如图:

controller类:

package com.demo.controller;

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,world";
    }
}

Application启动类:

package com.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Hello world!
 *
 */
@SpringBootApplication
public class Application {
    public static void main( String[] args ) {
        SpringApplication.run(Application.class,args);
        System.out.println( "Hello World!" );
    }
}

启动后访问页面:

 6.Springboot 分析:

        (1)spring-boot-starter-parent 是一个 Springboot 的父级依赖,开发 SpringBoot 程序都需要继承该父级项目,它用来提供相关的 Maven 默认依赖,使用它之后,常用的 jar
包依赖可以省去 version 配置 。
        (2)Spring Boot 提供了一些默认的jar 包的依赖,可查看该父级依赖的 pom 文件 。
        (3)如果不想使用某个默认的依赖版本,可以通过 pom.xml 文件的属性配置覆盖各个
依赖项,比如覆盖 Spring 版本: 

 <properties> 
     <spring-framework.version>5.0.0.RELEASE</ spring-framework.version > 
 </properties> 
        (4) @SpringBootApplication 注解是 Spring Boot 项目的核心注解,主要作用是开启
Spring 自动配置,如果在 Application 类上去掉该注解,那么不会启动 SpringBoot程序 。
        (5)main 方法是一个标准的 Java 程序的 main 方法,是boot项目启动运行的入口。
        (6)@Controller 及 @ResponseBody 依然是我们之前的 Spring MVC,因为 Spring Boot的里面依然是使用我们的 SpringMVC + Spring + MyBatis 等框架 。

7.核心配置文件:.properties、.yml、

   (1). 例如:application.properties:

  1. #设置内嵌 Tomcat 端口号:

  2. server.port=9090

  3. #设置项目上下文根:

  4. server.servlet.context-path=/springboot

(2)例如:application.yml  :

        yml 是一种 yaml 格式的配置文件,主要采用一定的空格、换行等格式排版进行配置。它能够直观的被计算机识别数据序列化格式,容易被人类阅读,yaml 类似于 xml,但是语法比 xml 简洁很多,值与前面的冒号配置项必须要有一个空格, yml 后缀也可以使用 yaml 后缀 。

(3)多环境配置(.properties方式

        在实际开发的过程中,我们的项目会经历很多的阶段(开发->测试->上线),每个阶段
的配置也会不同,例如:端口、上下文根、数据库等,那么这个时候为了方便在不同的环境
之间切换,SpringBoot 提供了多环境配置

注意:当两种格式配置文件同时存在时,使用的是.properties 配置文件。

为每个环境创建一个配置文件,命名必须为 application-环境标识 .properties或 .yml

  开发环境:application-dev.properties 

#开发环境
#设置内嵌 Tomcat 默认端口号 
server.port=8080 
#设置项目的上下文根 
server.servlet.context-path=/005-springboot-multi-environment-dev 

 

生产环境:application-product.properties

  1. #生产环境

  2. #配置内嵌 Tomcat 默认端口号

  3. server.port=80

  4. #配置项目上下文根

  5. server.servlet.context-path=/005-springboot-multi-environment-product

测试 环境 :application-test.properties

  1. #测试环境

  2. #配置内嵌 Tomcat 端口号

  3. server.port=8081

  4. #配置项目的上下文根

  5. server.servlet.context-path=/005-springboot-multi-environment-test

在总配置文件 application.properties 进行环境的激活

#springboot 总配置文件 

#激活开发环境 
        #spring: 
        # profiles: 
        #  active: dev  
#激活测试环境 
        #spring: 
        # profiles: 
        #  active: test 
#激活生产环境 
spring: 
 profiles: 
  active: product

8.使用thymeleaf模板引擎

     (1). 导入thymeleaf依赖即可:

<dependency>
   <groupId>org.thymeleaf</groupId>
   <artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
   <groupId>org.thymeleaf.extras</groupId>
   <artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>

   (2).在thymeleaf包下创建一个html页面

注意:需要添加thymeleaf的命名空间才能使用:xmlns:th="http://www.thymeleaf.org"

 (3)thymeleaf的语法:

 常用th命令:

方法    描述


th:id    替换标签id属性
th:text    替换标签文本
th:utext    支持html的文本替换
th:object    替换为对象,标签内可直接引用该对象属性示例
th:value    替换标签value属性
th:with    赋值为局部变量
th:style    替换标签style属性,设置样式
th:onclick    添加点击事件
th:each    集合,数组循环遍历 示例
th:if    条件判断,为true时渲染该标签示例
th:unless    条件判断,为false时渲染该标签 示例
th:href    替换标签href属性
th:switch    多路选择,与th:case配合使用 示例
th:case    th:switch的一个分支 示例
th:selected    为true时selected选择框选中
th:src    替换标签src属性
th:inline    定义js脚本可以使用变量
th:action    替换提交表单的地址
th:remove    删除某个属性 示例
th:attr    设置标签属性,多个属性可以用逗号分隔
th:fragment    布局标签,定义一个代码片段,方便其它地方引用 示例
th:assert    与th:fragment连用,用于参数验证 示例
th:include    布局标签,替换内容到引入的文件,Thymeleaf 3.0 之后不推荐使用 示例
th:insert    布局标签,替换内容到引入的文件 示例
th:replace    布局标签,替换整个标签到引入的文件 示例
th:block    空标签 示例
th:alt-title    替换img标签alt与title属性
th:attr    添加多个属性及属性值 示例
th:attrappend    已有属性值追加 示例
th:classappend    已有class属性值追加 示例
th:inline    设置内联 示例
(2) 常用变量获取方式

方法    描述
${…}    变量表达式,用于变量的获取
@{…}    链接表达式,一般和 th:href、th:src进行结合使用 示例
#{…}    消息表达式,用于从国际化文件中取值
~{…}    代码块表达式,一般和模版布局的语法一起使用 示例
*{…}    选择变量表达式,与th:object连用
[[${…}]]    在页面上直接输出变量,常用于js中获取变量
${${key}}    预处理,把${key}获取的值作为外层变量的key

实例:

(1) 提取变量

通过 th:object 获取一个对象,子标签元素可通过*{变量}获取变量值
        <div th:object="${user}">
            <div th:text="*{name}"></div>
        </div>

(2) 遍历

遍历list为多个div标签,item为遍历出的对象,stat为遍历的状态对象<a href="#stat"></a>
        <div th:each="item,stat: ${list}">
            <span th:text="${item}"></span>
            <span th:text="${stat.index}"></span>
        </div>

(3) 条件

<span th:if="${number} == 24">为true显示</span>
<span th:unless ="${number} == 24">为false显示</span>

 

(4) 多路选择

th:case="*"表示其他情况,放在最后
<div th:switch="${number}">
    <p th:case="'12'">number为12时显示</p>
    <p th:case="'24'">number为24时显示</p>
    <p th:case="*">其他时显示</p>
</div>

(5) 支持方法调用

获取变量字符串值的长度
<div th:text="${name.length()}"></div>
调用Date对象的getTime()
<span th:text="${date.getTime()}"></span>

(7) 运算符

显示数字除6的余数值
<div th:text="${number}%6"></div>
布尔类型取反
<span th:text="not (${number} >13)"></span> 
显示数字是否为2的布尔值
<div th:text="${number} eq 2"></div>
三元运算符,number为2显示是,否则显示否
<div th:text="${number} eq 2 ?'是':'否'"></div>
设置默认值,number为null时显示0
<div th:text="${number}  ?: 0 "></div>

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当前课程中博客项目的实战源码是我在 GitHub上开源项目 My-Blog,目前已有 3000 多个 star:本课程是一个 Spring Boot 技术栈的实战类课程,课程共分为 3 大部分,前面两个部分为基础环境准备和相关概念介绍,第三个部分是 Spring Boot 个人博客项目功能的讲解,通过本课程的学习,不仅仅让你掌握基本的 Spring Boot 开发能力以及 Spring Boot 项目的大部分开发使用场景,同时帮你提前甄别和处理掉将要遇到的技术难点,认真学完这个课程后,你将会对 Spring Boot 有更加深入而全面的了解,同时你也会得到一个大家都在使用的博客系统源码,你可以根据自己的需求和想法进行改造,也可以直接使用它来作为自己的个人网站,这个课程一定会给你带来巨大的收获。作者寄语本课程录制于 2020 年,代码基于 Spring Boot 2.x 版本。到目前为止,Spring Boot 技术栈也有一些版本升级,比如 Spring Boot 2.7 发版、Spring Boot 3.x 版本发布正式版本。对于这些情况,笔者会在本课程实战项目的开源仓库中创建不同的代码分支,保持实战项目的源码更新,保证读者朋友们不会学习过气的知识点。课程特色 课程内容紧贴 Spring Boot 技术栈,涵盖大部分 Spring Boot 使用场景。开发教程详细完整、文档资源齐全、实验过程循序渐进简单明了。实践项目页面美观且实用,交互效果完美。包含从零搭建项目、以及完整的后台管理系统和博客展示系统两个系统的功能开发流程。技术栈新颖且知识点丰富,学习后可以提升大家对于知识的理解和掌握,对于提升你的市场竞争力有一定的帮助。实战项目预览    

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值