SpringBoot学习笔记 - 构建、简化原理、快速启动、配置文件与多环境配置、技术整合案例
【前置内容】Spring 学习笔记全系列传送门:
Spring学习笔记 - 第一章 - IoC(控制反转)、IoC容器、Bean的实例化与生命周期、DI(依赖注入)
Spring学习笔记 - 第二章 - 注解开发、配置管理第三方Bean、注解管理第三方Bean、Spring 整合 MyBatis 和 Junit 案例
Spring学习笔记 - 第三章 - AOP与Spring事务
【前置内容】SpingMVC 学习笔记全系列传送门:
- SpringMVC学习笔记 - 第一章 - 工作流程、Bean加载控制、请求与响应(参数接收与内容返回)、RESTful
- SpringMVC学习笔记 - 第二章 - SSM整合案例 - 技术整合、统一结果封装、统一异常处理、前后联调、拦截器
- 1、SpringBoot 简介
- 1.1 SpringBoot 快速入门
- 1.1.1 开发步骤
- 1.1.1.1 创建新模块
- 1.1.1.2 创建 Controller
- 1.1.1.3 启动服务器
- 1.1.2 开发内容对比
- 1.1.3 官网构建工程
- 1.1.4 SpringBoot 工程快速启动
- 1.1.4.1 问题引入
- 1.1.4.2 打包
- 1.1.4.3 启动
- 1.1.1 开发步骤
- 1.2 SpringBoot 概述
- 1.2.1 起步依赖 —— 核心:Maven 继承
- 1.2.1.1 探索父工程(源码不表)
- 1.2.1.2 探索依赖
- 1.2.1.3 小结
- 1.2.2 程序启动
- 1.2.3 切换 web 服务器
- 1.2.1 起步依赖 —— 核心:Maven 继承
- 1.1 SpringBoot 快速入门
- 2、配置文件
- 2.1 配置文件格式及优先级
- 2.2 yaml 格式
- 2.2.1 概述
- 2.2.2 语法规则
- 2.3 yaml 配置文件数据读取
- 2.3.1 环境准备
- 2.3.2 读取配置数据
- 2.3.2.1 使用 @Value 注解
- 2.3.2.2 Environment 对象
- 2.3.2.3 自定义对象
- 2.4 多环境配置
- 2.4.1 yaml 文件
- 2.4.2 properties 文件
- 2.4.3 命令行启动参数配置
- 2.5 配置文件分类
- 3、SpringBoot 整合 Junit
- 3.1 环境准备
- 3.2 测试类编写
- 4、SpringBoot 整合 Mybatis
- 4.1 回顾 Spring 整合 Mybatis
- 4.2 SpringBoot 整合Mybatis
- 4.2.1 创建模块
- 4.2.2 定义实体类
- 4.2.3 定义 dao
- 4.2.4 定义测试类
- 4.2.5 编写配置
- 4.2.6 使用 Druid 数据源
- 5、基于 SpringBoot 的 SSM 整合案例
- 5.1 创建工程
- 5.2 后端代码整理
- 5.3 配置文件
- 5.4 静态资源
- 5.5 包结构展示
- 5.6 其他问题
1、SpringBoot 简介
1.1 SpringBoot 快速入门
1.1.1 开发步骤
-
创建新模块,选择Spring初始化,并配置模块相关基础信息
-
选择当前模块需要使用的技术集
-
开发控制器类
-
运行自动生成的Application类
1.1.1.1 创建新模块
-
选择
,用来创建Spring Initializr
工程SpringBoot
-
打包方式这里需要设置为
Jar
-
选中
,然后勾选Web
Spring Web
-
Java 版本选 Java8
-
pom.xml 文件预览
需要注意:
- 父包 spring-boot-starter-parent 的版本手动更改为了 2.6.3,jdk版本1.8 与 spring boot 3.0.0 版本不匹配,会报错
- <java.version> 修改为了1.8
<?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.6.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>priv.dandelion</groupId>
<artifactId>springboot_01_quickstart</artifactId>
<version>0.0.1-SNAPSHOT</version>
<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>
1.1.1.2 创建 Controller
@RestController
@RequestMapping("/books")
public class BookController {
@GetMapping("/{id}")
public String getById(@PathVariable Integer id) {
System.out.println("id = " + id);
return "hello boot!";
}
}
1.1.1.3 启动服务器
直接运行引导类即可
1.1.2 开发内容对比
-
坐标
-
程序中的坐标需要自己编写,而且坐标非常多Spring
-
程序中的坐标是我们在创建工程时进行勾选自动生成的SpringBoot
-
-
web3.0配置类
-
程序需要自己编写这个配置类。这个配置类大家之前编写过,肯定感觉很复杂Spring
-
程序不需要我们自己书写SpringBoot
-
-
配置类
注意:基于Idea的
快速构建Spring Initializr
工程时需要联网。SpringBoot
-
程序的配置类需要自己书写。而Spring/SpringMVC
程序则不需要书写。SpringBoot
-
1.1.3 官网构建工程
SpringBoot官网:https://spring.io/projects/spring-boot
-
点击下方的 Quickstart 中的 Spring Initializr,开始快速创建项目,如下图所示
1.1.4 SpringBoot 工程快速启动
1.1.4.1 问题引入
- SpringBoot 程序服务器运行在本机
- 当进行前后端联调时,按理说前端需要连接后端开发的机器,比较麻烦
- 是否有更好的方式?
-
后端可以将
工程打成SpringBoot
包jar
- 该
包运行不依赖于jar
和Tomcat
也可以正常运行Idea
- 这个
包在运行过程中连接和我们自己程序相同的jar
数据库即可Mysql
- 该
1.1.4.2 打包
-
首先必须在
中配置如下插件pom.xml
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin> -
使用
的Maven
指令打包就会在package
目录下生成对应的target
包Jar
-
注意:
-
在运行
指令之前,最好先运行一次package
指令,防止出现问题clean
-
如果配置文件中包含中文,最好在设置中设置编码为 UTF-8 并重新检查配置文件中的中文,避免出现乱码问题导致配置文件无法使用,具体操作如下
-
1.1.4.3 启动
-
进入 jar 包所在目录,使用 cmd 输入命令
java -jar springboot_01_quickstart-0.0.1-SNAPSHOT.jar
1.2 SpringBoot 概述
原始
环境搭建和开发存在以下问题:Spring
- 配置繁琐
- 依赖设置繁琐
程序优点恰巧就是针对SpringBoot
的缺点Spring
- 自动配置。这个是用来解决
程序配置繁琐的问题Spring
- 起步依赖。这个是用来解决
程序依赖设置繁琐的问题Spring
- 辅助功能(内置服务器,...)。在启动
程序时既没有使用本地的SpringBoot
也没有使用tomcat
插件,而是使用tomcat
内置的服务器。SpringBoot
1.2.1 起步依赖 —— 核心:Maven 继承
以后需要使用技术,只需要引入该技术对应的起步依赖即可
-
使用
方式创建的Spring Initializr
工程的的Maven
配置文件中自动生成了很多包含pom.xml
的依赖,这些以来就是 启动依赖 ,如下starter
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.8</version>
</parent>
<!--
...
-->
<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>
1.2.1.1 探索父工程(源码不表)
-
进入父工程 spring-boot-starter-parent 中,其也有一个父工程
-
进入 spring-boot-starter-parent 的父工程 spring-boot-dependencies 中,其中包含:
-
标签中定义了各个技术软件依赖的版本,避免了我们在使用不同软件技术时考虑版本的兼容问题。在<properties...>
中可以找到各种技术的版本。properties
-
标签是进行依赖版本锁定,但是并没有导入对应的依赖;如果我们工程需要那个依赖只需要引入依赖的<dependencyManagement...>
和groupid
不需要定义artifactId
。version
-
标签中对插件的版本进行了锁定<build...>
-
1.2.1.2 探索依赖
-
本工程中添加了如下依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> -
可以看到 spring-boot-starter-web 引入了如下依赖
- 引入了
和spring-web
的依赖,这就是为什么我们的工程中没有依赖这两个包还能正常使用spring-webmvc
中的注解的原因。springMVC
- 而依赖
,从名字基本能确认内部依赖了spring-boot-starter-tomcat
,所以我们的工程才能正常启动。tomcat
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.7.8</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
<version>2.7.8</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.7.8</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.25</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.25</version>
<scope>compile</scope>
</dependency>
</dependencies> - 引入了
1.2.1.3 小结
-
starter
-
中常见项目名称,定义了当前项目使用的所有项目坐标,以达到减少依赖配置的目的SpringBoot
-
-
parent
- 所有
项目要继承的项目,定义了若干个坐标版本号(依赖管理,而非依赖),以达到减少依赖冲突的目的SpringBoot
- 所有
-
实际开发
-
使用任意坐标时,仅书写GAV中的G和A,V由SpringBoot提供
G:groupid
A:artifactId
V:version
-
如发生坐标错误,再指定version(要小心版本冲突)
-
1.2.2 程序启动
-
引导类
@SpringBootApplication
public class Springboot01QuickstartApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot01QuickstartApplication.class, args);
}
} -
注意
-
在创建项目时,采用 jar 的打包方式SpringBoot
-
的引导类是项目的入口,运行SpringBoot
方法就可以启动项目main
- 因为
中配置了pom.xml
依赖,而该依赖中依赖spring-boot-starter-web
,所以运行tomcat
方法就可以使用main
启动工程。tomcat
-
1.2.3 切换 web 服务器
-
目前启动工程使用的是
服务器,tomcat
依赖中依赖了spring-boot-starter-web
tomcat
-
如果要使用其他服务器就需要将 tomcat 排除,更换为
jetty
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
2、配置文件
2.1 配置文件格式及优先级
- 当三种配置文件均存在时,优先级
>application.properties
>application.yml
application.yaml
程序的配置文件名必须是SpringBoot
,只是后缀名不同application
-
application.properties
server.port=80
-
(常用)application.yml
server:
port: 81 -
application.yaml
server:
port: 82
2.2 yaml 格式
2.2.1 概述
-
特点
-
YAML(YAML Ain't Markup Language),一种数据序列化格式。
-
相比于 xml 和 properties 而言重数据,轻格式
-
-
优点
-
容易阅读
-
类型的配置文件比yaml
类型的配置文件更容易阅读,结构更加清晰xml
-
-
容易与脚本语言交互
-
以数据为核心,重数据轻格式
-
更注重数据,而yaml
更注重格式xml
-
-
-
扩展名
-
(主流).yml
-
.yaml
-
2.2.2 语法规则
-
规则
-
大小写敏感
-
属性层级关系使用多行描述,每行结尾使用冒号结束
-
使用缩进表示层级关系,同层级左侧对齐,只允许使用空格(不允许使用Tab键),空格的个数并不重要,只要保证同层级的左侧对齐即可。
-
属性值前面添加空格(属性名与属性值之间使用冒号+空格作为分隔)
-
# 表示注释
-
-
示例
# 层级关系
enterprise:
name: dandelion
age: 16
tel: 12345456767
# 数组格式
subject:
- Java
- C++
- 算法
2.3 yaml 配置文件数据读取
2.3.1 环境准备
-
准配配置文件
application.yaml
lesson: SpringBoot
server:
port: 80
enterprise:
name: dandelion
age: 16
tel: 12345456767
subject:
- Java
- C++
- 算法
2.3.2 读取配置数据
2.3.2.1 使用 @Value 注解
直接使用
注解进行注入,用于读取单个数据,参数为@Value("${ }")
包裹的数据名称的字符串${ }
-
Controller 示例
@RestController
@RequestMapping("/books")
public class BookController {
// 读取数据
@Value("${lesson}")
private String lesson;
// 多级数据
@Value("${server.port}")
private Integer port;
// 数组元素
@Value("${enterprise.subject[0]}")
private String subject_00;
@GetMapping("/{id}")
public String getById(@PathVariable Integer id) {
System.out.println(lesson);
System.out.println(port);
System.out.println(subject_00);
return "hello boot!";
}
}
2.3.2.2 Environment 对象
用于获取全部数据,使用时 对
类型的属性进行自动装配 ,使用其Environment
方法来获取数据getProperty()
-
Controller 示例
import org.springframework.core.env.Environment;
// import ...
@RestController
@RequestMapping("/books")
public class BookController {
// 获取全部数据,注入到Environment类型中
@Autowired
private Environment environment;
@GetMapping("/{id}")
public String getById(@PathVariable Integer id) {
//
System.out.println(environment.getProperty("lesson"));
System.out.println(environment.getProperty("server.port"));
System.out.println(environment.getProperty("enterprise.age"));
System.out.println(environment.getProperty("enterprise.subject[1]"));
return "hello boot!";
}
}
2.3.2.3 自定义对象
该方式用于读取任意数据,将其封装为实体类,使用注解绑定获取数据的范围,在获取时,通过自动注入获取该实体类对象并进行操作
-
定义数据的实体类,和配置文件中保持一致,并实现其 Getter 和 Setter
- 后续要使用自动装配,故要添加 @Component 注解,将这个类交给 Spring 管理
- 使用
注解,@ConfigurationProperties(prefix = "enterprise")
属性值表示读取配置文件的哪一部分数据,此处代表读取prefix
中的数据enterprise
@Component
@ConfigurationProperties(prefix = "enterprise")
public class Enterprise {
private String name;
private int age;
private String tel;
private String[] subject;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String[] getSubject() {
return subject;
}
public void setSubject(String[] subject) {
this.subject = subject;
}
@Override
public String toString() {
return "Enterprise{" +
"name='" + name + '\'' +
", age=" + age +
", tel='" + tel + '\'' +
", subject=" + Arrays.toString(subject) +
'}';
}
} -
Controller 示例
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private Enterprise enterprise;
@GetMapping("/{id}")
public String getById(@PathVariable Integer id) {
System.out.println(enterprise);
return "hello boot!";
}
}
2.4 多环境配置
2.4.1 yaml 文件
-
简单书写
- 使用
设定环境的名称spring.profiles
- 使用
将不同的环境进行分隔---
- 使用
设置启动项目时使用的环境spring.profiles.active
- 公用的配置可以写在第一部分中
#设置启用的环境
spring:
profiles:
active: test
---
#开发环境
spring:
profiles: dev
server:
port: 80
---
#测试环境
spring:
profiles: test
server:
port: 81
---
#生产环境
spring:
profiles: pro
server:
port: 82 - 使用
-
标注书写
简单书写部分中,设定环境的名称的
的书写格式并不规范,规范的书写格式如下spring.profiles
#开发环境
spring:
config:
activate:
on-profile: dev
server:
port: 80
2.4.2 properties 文件
- 与 yaml 文件不同,properties 的多环境配置写在不同的文件中,并在主配置文件中指定使用的环境
- 多个环境使用文件名进行区分和定义,
application-环境名.properties
- 公用的配置可以写
中application.properties
-
主配置文件
application.properties
#设置启用的环境
spring.profiles.active=dev -
开发环境和测试环境
-
开发环境
application-dev.properties
server.port=80
-
测试环境
application-test.properties
server.port=81
-
2.4.3 命令行启动参数配置
-
命令行临时配置
可以在启动指令后添加参数来临时覆盖配置文件中的内容,参数可以有多个,每一个参数的格式为
,如下-- 空格 使用.连接的配置属性名称=属性值
java -jar springboot_01_quickstart-0.0.1-SNAPSHOT.jar -- spring.profiles.active=test -- server.port=88
-
配置的优先级
- 如上,命令中添加参数的优先级大于原本配置文件的优先级
-
官网对于优先级已经进行了说明,参见:SpringBoot
- 官方文档:Externalized Configuration
- 中文手册:外化配置
2.5 配置文件分类
-
命令行启动参数配置存在问题
- 由于测试环境和开发环境的很多配置都不相同,所以测试人员在运行我们的工程时需要临时修改很多配置,可能参数过多,过于复杂
-
解决方案:额外的配置类
-
SpringBoot 配置文件中 4 级配置文件位置及其优先级(优先级逐级提升):
- 1级:classpath:application.yml 【最低】
- 2级:classpath:config/application.yml
- 3级:file :application.yml
- 4级:file :config/application.yml 【最高】
-
说明
- classpath 指的是项目的 resources 目录下,file 指的是打好的 jar 包所在的目录下
- file 中的配置文件一般用于系统打包后临时设置通用属性,classpath 中的配置文件一般用于开发阶段设置通用属性
- file 中的配置文件的优先级高于 classpath 中的配置文件
- 同一分类的配置中,config 文件夹中的配置文件的优先级高于 config文件夹外的配置文件
-
3、SpringBoot 整合 Junit
Spring 整合 Junit 回顾
3.1 环境准备
-
创建新的模块,不需要依赖
-
Service 实现类(接口不表)
@Service
public class BookServiceImpl implements BookService {
@Override
public void save() {
System.out.println("book save!");
}
}
3.2 测试类编写
-
测试类中(
修饰),将@SpringBootTest
注入BookService
如果测试类和引导类的包名不一致,需要为
的 class 属性手动指定引导类的字节码对象,如@SpringBootTest
@SpringBootTest(classes = Springboot02TestApplication.class)
@SpringBootTest
class Springboot02TestApplicationTests {
@Autowired
private BookService bookService;
@Test
void contextLoads() {
bookService.save();
}
}
4、SpringBoot 整合 Mybatis
4.1 回顾 Spring 整合 Mybatis
-
Spring 整合 MyBatis
- 整合思路
- 整合步骤
4.2 SpringBoot 整合Mybatis
4.2.1 创建模块
-
依赖
- MyBatis Framework
- MySQL Driver
4.2.2 定义实体类
public class Book {
private Integer id;
private String name;
private String type;
private String description;
// Getter、Setter、toString...
}
4.2.3 定义 dao
数据库SQL见前置内容,tbl_book 表SQL,此处不再赘述
-
dao (报错)
public interface BookDao {
@Select("select * from tbl_book where id = #{id}")
public Book getById(Integer id);
} -
存在问题及解决方案
-
报错:
No qualifying bean of type 'priv.dandelion.dao.BookDao' available
错误信息显示在
容器中没有Spring
类型的BookDao
bean
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'priv.dandelion.dao.BookDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1801) ~[spring-beans-5.3.25.jar:5.3.25]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1357) ~[spring-beans-5.3.25.jar:5.3.25]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1311) ~[spring-beans-5.3.25.jar:5.3.25]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:657) ~[spring-beans-5.3.25.jar:5.3.25]
... 74 common frames omitted -
原因
-
会扫描接口并创建接口的代码对象交给Mybatis
管理,但是现在并没有告诉Spring
哪个是Mybatis
接口dao
- 注意: Mysql驱动版本大于8.0时,需要在url连接串中配置时区
,或在MySQL数据库端配置时区解决此问题jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC
-
-
解决方案:在
接口上使用BookDao
@Mapper
@Mapper
public interface BookDao {
@Select("select * from tbl_book where id = #{id}")
public Book getById(Integer id);
}
-
4.2.4 定义测试类
@SpringBootTest
class Springboot03MybatisApplicationTests {
@Autowired
private BookDao bookDao;
@Test
void contextLoads() {
Book book = bookDao.getById(1);
System.out.println(book);
}
}
4.2.5 编写配置
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm_db
username: root
password: 123456
4.2.6 使用 Druid 数据源
-
添加依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency> -
添加配置,指定使用 Druid 数据源
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm_db
username: root
password: 123456
type: com.alibaba.druid.pool.DruidDataSource
5、基于 SpringBoot 的 SSM 整合案例
- 本节内容是对上一篇 SSM 整合案例的改进,可参见SpingMVC 学习笔记 - 第二章 -SSM整合案例
- 开发步骤
pom.xml:配置起步依赖,必要的资源坐标(druid)
application.yml:设置数据源、端口等
配置类:现在不需要了
dao:设置@Mapper
测试类
页面:放置在resources目录下的static目录中
5.1 创建工程
-
依赖
- Spring Web
- MyBatis Framework
- MySQL Driver
-
引入Druid
-
添加依赖
<!-- TODO 添加必要的依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
-
5.2 后端代码整理
-
config 包,对比 SSM 项目全部删除,现在不需要了
-
entity 包,实体,无变化
-
dao,添加
注解@Mapper
// TODO 添加@Mapper
@Mapper
public interface BookDao {
@Insert("insert into tbl_book values(null,#{type},#{name},#{description})")
// @Insert("insert into tbl_book (type,name,description) values(#{type},#{name},#{description})")
public int save(Book book);
@Update("update tbl_book set type = #{type}, name = #{name}, description = #{description} where id = #{id}")
public int update(Book book);
@Delete("delete from tbl_book where id = #{id}")
public int delete(Integer id);
@Select("select * from tbl_book where id = #{id}")
public Book getById(Integer id);
@Select("select * from tbl_book")
public List<Book> getAll();
} -
service,无变化
-
exception,无变化
-
controller,无变化
-
测试类,使用了 @SpringBootTest 注解,更换了使用的包
// import org.junit.Test;
import org.junit.jupiter.api.Test;
// import ...
//@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration(classes = SpringConfig.class)
// TODO 将原先使用的注解更改为 @SpringBootTest
@SpringBootTest
public class BookServiceTest {
@Autowired
private BookService bookService;
// TODO 原先使用的 @Test 注解 现在需要重新导包
@Test
public void testGetById() {
Book byId = bookService.getById(1);
System.out.println(byId);
}
@Test
public void testGetAll() {
List<Book> all = bookService.getAll();
System.out.println(all);
}
}
5.3 配置文件
#TODO 配置端口和数据源相关信息
server:
port: 80
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm_db
username: root
password: 123456
5.4 静态资源
-
静态资源存放在 resources 目录下的 static 目录下
-
配置主页
index.html
<!--TODO 配置主页-->
<script>
document.location.href="pages/books.html";
</script>
5.5 包结构展示
5.6 其他问题
- 若再未加入前端代码时启动过服务器,加入前端代码后无法正常访问到页面时
- 执行 Maven 的 clean 指令,再重新启动服务器(重新打包)即可
- SpringBoot 版本问题、MySQL时区问题、项目无法构建、配置文件打包乱码等问题,本节笔记中均有提及,可向上查阅