springboot复习(黑马)(持续更新)
Posted 姜小白程序
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot复习(黑马)(持续更新)相关的知识,希望对你有一定的参考价值。
![](https://image.cha138.com/20230309/d3a9968b378b425c900b72371d6327c5.jpg)
学习目标
基于SpringBoot框架的程序开发步骤
熟练使用SpringBoot配置信息修改服务器配置
基于SpringBoot的完成SSM整合项目开发
一、SpringBoot简介
1. 入门案例
问题导入
SpringMVC的HelloWord程序大家还记得吗?
SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的初始搭建以及开发过程
原生开发SpringMVC程序过程
![](https://image.cha138.com/20230309/3612ff04ec3448beace77815ed519dab.jpg)
1.1 入门案例开发步骤
环境准备
![](https://image.cha138.com/20230309/115332d87ab44aee89a4dcede772a556.jpg)
![](https://image.cha138.com/20230309/969b49cdd6924b7ab0113df558697b25.jpg)
![](https://image.cha138.com/20230309/5f9ee4a474d3411aa28de311c4192606.jpg)
![](https://image.cha138.com/20230309/e7f14d6e27b34670b133678faa06ca8b.jpg)
①:创建新模块,选择Spring初始化,并配置模块相关基础信息
![](https://image.cha138.com/20230309/e34504da4ed34c68a67c320197288c1e.jpg)
![](https://image.cha138.com/20230309/81b6d7b559364c93a0e1d44b5af3657b.jpg)
②:选择当前模块需要使用的技术集
![](https://image.cha138.com/20230309/c1e1352fac464005bc9c5469515cb4bb.jpg)
![](https://image.cha138.com/20230309/3ace307310494258946586e05e705e0f.jpg)
![](https://image.cha138.com/20230309/aab44e0811974402a3fa53c1d5aaff43.jpg)
![](https://image.cha138.com/20230309/8e57a11b3e8f49eba023deef3fe02bba.jpg)
③:开发控制器类
@RestController
public class Controller01
@RequestMapping("/sayHi")
public String sayHi()
System.out.println("hi...");
return "hi ... springboot...";
![](https://image.cha138.com/20230309/463013ce4a354177ab824e03dca53411.jpg)
④:运行自动生成的Application类
![](https://image.cha138.com/20230309/be870420d4fa4f8fb2da68f2d4ea9a36.jpg)
![](https://image.cha138.com/20230309/dcf6e51e0c544e738d3af811803e20cf.jpg)
访问页面
![](https://image.cha138.com/20230309/090b859192db477f92fcb184eaea76c8.jpg)
重新启动
![](https://image.cha138.com/20230309/c1a5454c25554f79ba90421dad17b07f.jpg)
![](https://image.cha138.com/20230309/611258c1370c4907885e0965e7411b9f.jpg)
最简SpringBoot程序所包含的基础文件
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.itheima</groupId>
<artifactId>demo1_helloworld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo1_helloworld</name>
<description>Demo project for Spring Boot</description>
<properties>
<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>
</project>
package com.itheima;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/*/*
@SpringBootApplication:
1. 表示这个类是一个springboot应用程序的入口类。
2. 要想让程序启动,只需要在main方法里面写上这样的一句话:
SpringApplication.run(当前类的字节码对象, args);
3. 拓展:
3.1 springboot项目启动的时候,默认会扫描启动类所在的位置,以及它后续的所有子包。
3.2 查找到类里面打的注解 @Controller , @Service , @RequestMapping.
3.3 springApplication.run 传递进去当前类的字节码对象,也是可以确定当前
这个启动器它的包是哪个!
*/
@SpringBootApplication
public class Demo1HelloworldApplication
public static void main(String[] args)
SpringApplication.run(Demo1HelloworldApplication.class, args);
Spring程序与SpringBoot程序对比
![](https://image.cha138.com/20230309/81b7b39d5c884430b5c4d5fb644c2d9a.jpg)
注意事项:
基于idea开发SpringBoot程序需要确保联网且能够加载到程序框架结构
1.2 基于SpringBoot官网创建项目
![](https://image.cha138.com/20230309/d616cd7d702740738ebe18e9e95f53f2.jpg)
保存到桌面
![](https://image.cha138.com/20230309/b741d6292e214cef9afed4f0f93ebfda.jpg)
![](https://image.cha138.com/20230309/ddb8d33c3e5c4ae7b4478f4a2e2f3e42.jpg)
![](https://image.cha138.com/20230309/f958559b5c0a4f3d8e457e8eff9a621a.jpg)
解压完之后 用idea打开他
![](https://image.cha138.com/20230309/15b046776ea04be990d85e61ccb34de8.jpg)
![](https://image.cha138.com/20230309/1f2ac782ba44422da6ccd784c0e3258c.jpg)
![](https://image.cha138.com/20230309/d73e1ae4feef49a68d9b0ec95f1d449f.jpg)
因为用到了mysql 所以得配置数据库
![](https://image.cha138.com/20230309/5afd4ddf737d42baa738f41e8ed0e4fe.jpg)
已经启动
![](https://image.cha138.com/20230309/2494c196fe6e41b8a050ff0ef76ea792.jpg)
1.3 SpringBoot项目快速启动
① 对SpringBoot项目打包(执行Maven构建指令package)
② 执行启动指令
![](https://image.cha138.com/20230309/a07600027aea47a7afca6cc0e57f1be3.jpg)
java -jar ava -jar demo1_helloworld-0.0.1-SNAPSHOT.jar # 项目的名称根据实际情况修改
例子
![](https://image.cha138.com/20230309/1f3d77def0be4b6eb4120fc103ee4c4f.jpg)
![](https://image.cha138.com/20230309/e5dc840cdfe142a5817182f6e6bd66bb.jpg)
![](https://image.cha138.com/20230309/d9d3278be05a4ddc91952019ee0a1363.jpg)
![](https://image.cha138.com/20230309/07c055ad900f4f59bd342813ae8fcb00.jpg)
![](https://image.cha138.com/20230309/108ac18c01ea42fc9e94d3c6dc3204b9.jpg)
![](https://image.cha138.com/20230309/359d14aa84054ded8f43776f6f941aae.jpg)
![](https://image.cha138.com/20230309/9aa7316bf5cc4d9685581842965c6f1b.jpg)
![](https://image.cha138.com/20230309/a77d02a3e72f440cb751a363093c6409.jpg)
![](https://image.cha138.com/20230309/379127273ae74fba9fc65726e197ad99.jpg)
![](https://image.cha138.com/20230309/344f0e2d662c462898905539f4b5f852.jpg)
![](https://image.cha138.com/20230309/f8845efd2bc44ab68ae993d8f50fe729.jpg)
![](https://image.cha138.com/20230309/fa9eb619309a444094a66d964396fe54.jpg)
![](https://image.cha138.com/20230309/3a79fbe514f64bbe8ae0de330fa50a06.jpg)
注意事项:
jar支持命令行启动需要依赖maven插件支持,请确认打包时是否具有SpringBoot对应的maven插件。
![](https://image.cha138.com/20230309/c84a604f0ccf4fc5bc38a547a8329e62.jpg)
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
如果没有这个依赖maven插件 打包就只有4k左右
![](https://image.cha138.com/20230309/ee384037228c4130a8d19dd5517fd34f.jpg)
这个时候跟本运行不了
![](https://image.cha138.com/20230309/1eb4d25314af4fcfa91fb66c4d4aa363.jpg)
2. SpringBoot概述
问题导入
学习了SpringBoot入门案例之后,感觉对比SpringMVC哪一个更加方便简洁?
SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的初始搭建以及开发过程
Spring程序缺点
配置繁琐
依赖设置繁琐
SpringBoot程序优点
自动配置
起步依赖(简化依赖配置)
辅助功能(内置服务器,……)
![](https://image.cha138.com/20230309/d3324c1c29424f92b8ac78a74172bdee.jpg)
![](https://image.cha138.com/20230309/d7e71720ea5340018c9bc616e02d0863.jpg)
28行到225行 <properties></properties>
![](https://image.cha138.com/20230309/a6291cec6a2a46bc889d6cb098f60bb2.jpg)
226行 到2737行 <dependencyManagement> </dependencyManagement>
![](https://image.cha138.com/20230309/504271491764479b9d93c926a760733a.jpg)
![](https://image.cha138.com/20230309/ac8bc25e816d4960827247d61b1301e0.jpg)
2.1 起步依赖
starter
SpringBoot中常见项目名称,定义了当前项目使用的所有项目坐标,以达到减少依赖配置的目的
可以认为这个起步依赖相当于一个开关 我们主要用了这个东西 就相当用了他的全套功能
<!--starter:
1. 在springboot的依赖里面很常见
2. 一般会称之为起步依赖
3. 所谓起步依赖就是说: 只要导入这个起步依赖,起步依赖的背后会包含着一大堆依赖-->
<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>
![](https://image.cha138.com/20230309/41a7f96ee85f448497fbde045bb9b9dc.jpg)
parent
所有SpringBoot项目要继承的项目,定义了若干个坐标版本号(依赖管理,而非依赖),以达到减少依赖冲突的目的
spring-boot-starter-parent(2.5.0)与 spring-boot-starter-parent(2.4.6)共计57处坐标版本不同
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--继承 springboot 父亲-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!--项目本身的信息-->
<groupId>com.itheima</groupId>
<artifactId>demo1_helloworld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo1_helloworld</name>
<description>Demo project for Spring Boot</description>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.5.0</version>
</parent>
<artifactId>spring-boot-starter-parent</artifactId>
<packaging>pom</packaging>
...
</project>
实际开发
使用任意坐标时,仅书写GAV中的G和A,V由SpringBoot提供
如发生坐标错误,再指定version(要小心版本冲突)
如我们要用到Mysql
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
![](https://image.cha138.com/20230309/802933157937497c8ba9ce447b3b9f80.jpg)
![](https://image.cha138.com/20230309/a5fc18e343a14d60a2d7b1f6a1805332.jpg)
当然也不是什么都有管理
比如druid就不被管理
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version>
</dependency>
如果不写版本号的话 就会出现unknown 这个时候 我们就知道在springboot的父亲的父亲里面并没有管理这个依赖 这个时候 我们就能自己添加版本号
![](https://image.cha138.com/20230309/af4ab05074ed45a7b770047a492f33fe.jpg)
自己添加版本号
![](https://image.cha138.com/20230309/30f7d9cec51941a5b17986218fce223a.jpg)
2.2 辅助功能
SpringBoot程序启动
package com.itheima;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/*/*
@SpringBootApplication:
1. 表示这个类是一个springboot应用程序的入口类。
2. 要想让程序启动,只需要在main方法里面写上这样的一句话:
SpringApplication.run(当前类的字节码对象, args);
3. 拓展:
3.1 springboot项目启动的时候,默认会扫描启动类所在的位置,以及它后续的所有子包。
3.2 查找到类里面打的注解 @Controller , @Service , @RequestMapping.
3.3 springApplication.run 传递进去当前类的字节码对象,也是可以确定当前
这个启动器它的包是哪个!
*/
@SpringBootApplication
public class Demo1HelloworldApplication
public static void main(String[] args)
SpringApplication.run(Demo1HelloworldApplication.class, args);
SpringBoot在创建项目时,采用jar的打包方式
SpringBoot的引导类是项目的入口,运行main方法就可以启动项目
使用maven依赖管理变更起步依赖项
Jetty比Tomcat更轻量级,可扩展性更强(相较于Tomcat),谷歌应用引擎(GAE)已经全面切换为Jetty
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--排除tomcat,不用tomcat-->
<!--<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>-->
</dependency>
<!--添加进来jetty-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version>
</dependency>
<!--整合Junit-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
![](https://image.cha138.com/20230309/785e239eb3dc49a68303d221759ddb37.jpg)
二、基础配置
1. 配置文件格式
问题导入
框架常见的配置文件有哪几种形式?
1.1 修改服务器端口
http://localhost:8080/books/1 >>> http://localhost/books/1
以上是关于springboot复习(黑马)(持续更新)的主要内容,如果未能解决你的问题,请参考以下文章