经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Spring Boot » 查看文章
SpringBoot学习笔记 - 构建、简化原理、快速启动、配置文件与多环境配置、技术整合案例
来源:cnblogs  作者:Dandelion_000  时间:2023/2/2 4:27:44  对本文有异议

【前置内容】Spring 学习笔记全系列传送门:

【前置内容】SpingMVC 学习笔记全系列传送门:

1、SpringBoot 简介

1.1 SpringBoot 快速入门

1.1.1 开发步骤

  1. 创建新模块,选择Spring初始化,并配置模块相关基础信息

  2. 选择当前模块需要使用的技术集

  3. 开发控制器类

  4. 运行自动生成的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
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0</modelVersion>
    5. <parent>
    6. <groupId>org.springframework.boot</groupId>
    7. <artifactId>spring-boot-starter-parent</artifactId>
    8. <version>2.6.3</version>
    9. <relativePath/> <!-- lookup parent from repository -->
    10. </parent>
    11. <groupId>priv.dandelion</groupId>
    12. <artifactId>springboot_01_quickstart</artifactId>
    13. <version>0.0.1-SNAPSHOT</version>
    14. <properties>
    15. <java.version>1.8</java.version>
    16. </properties>
    17. <dependencies>
    18. <dependency>
    19. <groupId>org.springframework.boot</groupId>
    20. <artifactId>spring-boot-starter-web</artifactId>
    21. </dependency>
    22. <dependency>
    23. <groupId>org.springframework.boot</groupId>
    24. <artifactId>spring-boot-starter-test</artifactId>
    25. <scope>test</scope>
    26. </dependency>
    27. </dependencies>
    28. <build>
    29. <plugins>
    30. <plugin>
    31. <groupId>org.springframework.boot</groupId>
    32. <artifactId>spring-boot-maven-plugin</artifactId>
    33. </plugin>
    34. </plugins>
    35. </build>
    36. </project>
1.1.1.2 创建 Controller
  1. @RestController
  2. @RequestMapping("/books")
  3. public class BookController {
  4. @GetMapping("/{id}")
  5. public String getById(@PathVariable Integer id) {
  6. System.out.println("id = " + id);
  7. return "hello boot!";
  8. }
  9. }
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 包运行不依赖于 TomcatIdea也可以正常运行
    • 这个 jar 包在运行过程中连接和我们自己程序相同的 Mysql 数据库即可
1.1.4.2 打包
  • 首先必须在 pom.xml 中配置如下插件

    1. <plugin>
    2. <groupId>org.springframework.boot</groupId>
    3. <artifactId>spring-boot-maven-plugin</artifactId>
    4. </plugin>
  • 使用 Mavenpackage 指令打包就会在 target 目录下生成对应的 Jar

  • 注意:

    • 在运行 package 指令之前,最好先运行一次clean指令,防止出现问题

    • 如果配置文件中包含中文,最好在设置中设置编码为 UTF-8 并重新检查配置文件中的中文,避免出现乱码问题导致配置文件无法使用,具体操作如下

      乱码处理

1.1.4.3 启动
  • 进入 jar 包所在目录,使用 cmd 输入命令

    1. 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 的依赖,这些以来就是启动依赖,如下

    1. <parent>
    2. <groupId>org.springframework.boot</groupId>
    3. <artifactId>spring-boot-starter-parent</artifactId>
    4. <version>2.7.8</version>
    5. </parent>
    6. <!--
    7. ...
    8. -->
    9. <dependencies>
    10. <dependency>
    11. <groupId>org.springframework.boot</groupId>
    12. <artifactId>spring-boot-starter-web</artifactId>
    13. </dependency>
    14. <dependency>
    15. <groupId>org.springframework.boot</groupId>
    16. <artifactId>spring-boot-starter-test</artifactId>
    17. <scope>test</scope>
    18. </dependency>
    19. </dependencies>
1.2.1.1 探索父工程(源码不表)
  • 进入父工程 spring-boot-starter-parent 中,其也有一个父工程

  • 进入 spring-boot-starter-parent 的父工程 spring-boot-dependencies 中,其中包含:

    • <properties...> 标签中定义了各个技术软件依赖的版本,避免了我们在使用不同软件技术时考虑版本的兼容问题。在 properties 中可以找到各种技术的版本。
    • <dependencyManagement...> 标签是进行依赖版本锁定,但是并没有导入对应的依赖;如果我们工程需要那个依赖只需要引入依赖的 groupidartifactId 不需要定义 version
    • <build...> 标签中对插件的版本进行了锁定
1.2.1.2 探索依赖
  • 本工程中添加了如下依赖

    1. <dependency>
    2. <groupId>org.springframework.boot</groupId>
    3. <artifactId>spring-boot-starter-web</artifactId>
    4. </dependency>
  • 可以看到 spring-boot-starter-web 引入了如下依赖

    • 引入了 spring-webspring-webmvc 的依赖,这就是为什么我们的工程中没有依赖这两个包还能正常使用 springMVC 中的注解的原因。
    • 而依赖 spring-boot-starter-tomcat ,从名字基本能确认内部依赖了 tomcat,所以我们的工程才能正常启动。
    1. <dependencies>
    2. <dependency>
    3. <groupId>org.springframework.boot</groupId>
    4. <artifactId>spring-boot-starter</artifactId>
    5. <version>2.7.8</version>
    6. <scope>compile</scope>
    7. </dependency>
    8. <dependency>
    9. <groupId>org.springframework.boot</groupId>
    10. <artifactId>spring-boot-starter-json</artifactId>
    11. <version>2.7.8</version>
    12. <scope>compile</scope>
    13. </dependency>
    14. <dependency>
    15. <groupId>org.springframework.boot</groupId>
    16. <artifactId>spring-boot-starter-tomcat</artifactId>
    17. <version>2.7.8</version>
    18. <scope>compile</scope>
    19. </dependency>
    20. <dependency>
    21. <groupId>org.springframework</groupId>
    22. <artifactId>spring-web</artifactId>
    23. <version>5.3.25</version>
    24. <scope>compile</scope>
    25. </dependency>
    26. <dependency>
    27. <groupId>org.springframework</groupId>
    28. <artifactId>spring-webmvc</artifactId>
    29. <version>5.3.25</version>
    30. <scope>compile</scope>
    31. </dependency>
    32. </dependencies>
1.2.1.3 小结
  • starter

    • SpringBoot 中常见项目名称,定义了当前项目使用的所有项目坐标,以达到减少依赖配置的目的
  • parent

    • 所有 SpringBoot 项目要继承的项目,定义了若干个坐标版本号(依赖管理,而非依赖),以达到减少依赖冲突的目的
  • 实际开发

    • 使用任意坐标时,仅书写GAV中的G和A,V由SpringBoot提供

      G:groupid

      A:artifactId

      V:version

    • 如发生坐标错误,再指定version(要小心版本冲突)

1.2.2 程序启动

  • 引导类

    1. @SpringBootApplication
    2. public class Springboot01QuickstartApplication {
    3. public static void main(String[] args) {
    4. SpringApplication.run(Springboot01QuickstartApplication.class, args);
    5. }
    6. }
  • 注意

    • SpringBoot 在创建项目时,采用 jar 的打包方式
    • SpringBoot 的引导类是项目的入口,运行 main 方法就可以启动项目
    • 因为 pom.xml 中配置了 spring-boot-starter-web 依赖,而该依赖中依赖 tomcat ,所以运行 main 方法就可以使用 tomcat 启动工程。

1.2.3 切换 web 服务器

  • 目前启动工程使用的是 tomcat 服务器,spring-boot-starter-web 依赖中依赖了 tomcat

  • 如果要使用其他服务器就需要将 tomcat 排除,更换为 jetty

    1. <dependency>
    2. <groupId>org.springframework.boot</groupId>
    3. <artifactId>spring-boot-starter-web</artifactId>
    4. <exclusions>
    5. <exclusion>
    6. <artifactId>spring-boot-starter-tomcat</artifactId>
    7. <groupId>org.springframework.boot</groupId>
    8. </exclusion>
    9. </exclusions>
    10. </dependency>
    11. <dependency>
    12. <groupId>org.springframework.boot</groupId>
    13. <artifactId>spring-boot-starter-jetty</artifactId>
    14. </dependency>

2、配置文件

2.1 配置文件格式及优先级

  • 当三种配置文件均存在时,优先级application.properties > application.yml > application.yaml
  • SpringBoot 程序的配置文件名必须是 application ,只是后缀名不同
  • application.properties

    1. server.port=80
  • application.yml(常用)

    1. server:
    2. port: 81
  • application.yaml

    1. server:
    2. 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键),空格的个数并不重要,只要保证同层级的左侧对齐即可。

    • 属性值前面添加空格(属性名与属性值之间使用冒号+空格作为分隔)

    • # 表示注释

  • 示例

    1. # 层级关系
    2. enterprise:
    3. name: dandelion
    4. age: 16
    5. tel: 12345456767
    6. # 数组格式
    7. subject:
    8. - Java
    9. - C++
    10. - 算法

2.3 yaml 配置文件数据读取

2.3.1 环境准备

  • 准配配置文件 application.yaml

    1. lesson: SpringBoot
    2. server:
    3. port: 80
    4. enterprise:
    5. name: dandelion
    6. age: 16
    7. tel: 12345456767
    8. subject:
    9. - Java
    10. - C++
    11. - 算法

2.3.2 读取配置数据

2.3.2.1 使用 @Value 注解

直接使用 @Value("${ }") 注解进行注入,用于读取单个数据,参数为${ }包裹的数据名称的字符串

  • Controller 示例

    1. @RestController
    2. @RequestMapping("/books")
    3. public class BookController {
    4. // 读取数据
    5. @Value("${lesson}")
    6. private String lesson;
    7. // 多级数据
    8. @Value("${server.port}")
    9. private Integer port;
    10. // 数组元素
    11. @Value("${enterprise.subject[0]}")
    12. private String subject_00;
    13. @GetMapping("/{id}")
    14. public String getById(@PathVariable Integer id) {
    15. System.out.println(lesson);
    16. System.out.println(port);
    17. System.out.println(subject_00);
    18. return "hello boot!";
    19. }
    20. }
2.3.2.2 Environment 对象

用于获取全部数据,使用时Environment类型的属性进行自动装配,使用其getProperty()方法来获取数据

  • Controller 示例

    1. import org.springframework.core.env.Environment;
    2. // import ...
    3. @RestController
    4. @RequestMapping("/books")
    5. public class BookController {
    6. // 获取全部数据,注入到Environment类型中
    7. @Autowired
    8. private Environment environment;
    9. @GetMapping("/{id}")
    10. public String getById(@PathVariable Integer id) {
    11. //
    12. System.out.println(environment.getProperty("lesson"));
    13. System.out.println(environment.getProperty("server.port"));
    14. System.out.println(environment.getProperty("enterprise.age"));
    15. System.out.println(environment.getProperty("enterprise.subject[1]"));
    16. return "hello boot!";
    17. }
    18. }
2.3.2.3 自定义对象

该方式用于读取任意数据,将其封装为实体类,使用注解绑定获取数据的范围,在获取时,通过自动注入获取该实体类对象并进行操作

  • 定义数据的实体类,和配置文件中保持一致,并实现其 Getter 和 Setter

    • 后续要使用自动装配,故要添加 @Component 注解,将这个类交给 Spring 管理
    • 使用@ConfigurationProperties(prefix = "enterprise")注解,prefix 属性值表示读取配置文件的哪一部分数据,此处代表读取 enterprise中的数据
    1. @Component
    2. @ConfigurationProperties(prefix = "enterprise")
    3. public class Enterprise {
    4. private String name;
    5. private int age;
    6. private String tel;
    7. private String[] subject;
    8. public String getName() {
    9. return name;
    10. }
    11. public void setName(String name) {
    12. this.name = name;
    13. }
    14. public int getAge() {
    15. return age;
    16. }
    17. public void setAge(int age) {
    18. this.age = age;
    19. }
    20. public String getTel() {
    21. return tel;
    22. }
    23. public void setTel(String tel) {
    24. this.tel = tel;
    25. }
    26. public String[] getSubject() {
    27. return subject;
    28. }
    29. public void setSubject(String[] subject) {
    30. this.subject = subject;
    31. }
    32. @Override
    33. public String toString() {
    34. return "Enterprise{" +
    35. "name='" + name + '\'' +
    36. ", age=" + age +
    37. ", tel='" + tel + '\'' +
    38. ", subject=" + Arrays.toString(subject) +
    39. '}';
    40. }
    41. }
  • Controller 示例

    1. @RestController
    2. @RequestMapping("/books")
    3. public class BookController {
    4. @Autowired
    5. private Enterprise enterprise;
    6. @GetMapping("/{id}")
    7. public String getById(@PathVariable Integer id) {
    8. System.out.println(enterprise);
    9. return "hello boot!";
    10. }
    11. }

2.4 多环境配置

2.4.1 yaml 文件

  • 简单书写

    • 使用spring.profiles设定环境的名称
    • 使用---将不同的环境进行分隔
    • 使用spring.profiles.active设置启动项目时使用的环境
    • 公用的配置可以写在第一部分中
    1. #设置启用的环境
    2. spring:
    3. profiles:
    4. active: test
    5. ---
    6. #开发环境
    7. spring:
    8. profiles: dev
    9. server:
    10. port: 80
    11. ---
    12. #测试环境
    13. spring:
    14. profiles: test
    15. server:
    16. port: 81
    17. ---
    18. #生产环境
    19. spring:
    20. profiles: pro
    21. server:
    22. port: 82
  • 标注书写

    简单书写部分中,设定环境的名称的spring.profiles的书写格式并不规范,规范的书写格式如下

    1. #开发环境
    2. spring:
    3. config:
    4. activate:
    5. on-profile: dev
    6. server:
    7. port: 80

2.4.2 properties 文件

  • 与 yaml 文件不同,properties 的多环境配置写在不同的文件中,并在主配置文件中指定使用的环境
  • 多个环境使用文件名进行区分和定义,application-环境名.properties
  • 公用的配置可以写application.properties
  • 主配置文件application.properties

    1. #设置启用的环境
    2. spring.profiles.active=dev
  • 开发环境和测试环境

    • 开发环境application-dev.properties

      1. server.port=80
    • 测试环境application-test.properties

      1. server.port=81

2.4.3 命令行启动参数配置

  • 命令行临时配置

    可以在启动指令后添加参数来临时覆盖配置文件中的内容,参数可以有多个,每一个参数的格式为-- 空格 使用.连接的配置属性名称=属性值,如下

    1. java -jar springboot_01_quickstart-0.0.1-SNAPSHOT.jar -- spring.profiles.active=test -- server.port=88
  • 配置的优先级

    • 如上,命令中添加参数的优先级大于原本配置文件的优先级
    • SpringBoot 官网对于优先级已经进行了说明,参见:

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 实现类(接口不表)

    1. @Service
    2. public class BookServiceImpl implements BookService {
    3. @Override
    4. public void save() {
    5. System.out.println("book save!");
    6. }
    7. }

3.2 测试类编写

  • 测试类中(@SpringBootTest 修饰),将 BookService 注入

    如果测试类和引导类的包名不一致,需要为@SpringBootTest 的 class 属性手动指定引导类的字节码对象,如@SpringBootTest(classes = Springboot02TestApplication.class)

    1. @SpringBootTest
    2. class Springboot02TestApplicationTests {
    3. @Autowired
    4. private BookService bookService;
    5. @Test
    6. void contextLoads() {
    7. bookService.save();
    8. }
    9. }

4、SpringBoot 整合 Mybatis

4.1 回顾 Spring 整合 Mybatis

4.2 SpringBoot 整合Mybatis

4.2.1 创建模块

  • 依赖

    • MyBatis Framework
    • MySQL Driver

4.2.2 定义实体类

  1. public class Book {
  2. private Integer id;
  3. private String name;
  4. private String type;
  5. private String description;
  6. // Getter、Setter、toString...
  7. }

4.2.3 定义 dao

数据库SQL见前置内容,tbl_book 表SQL,此处不再赘述

  • dao (报错)

    1. public interface BookDao {
    2. @Select("select * from tbl_book where id = #{id}")
    3. public Book getById(Integer id);
    4. }
  • 存在问题及解决方案

    • 报错:No qualifying bean of type 'priv.dandelion.dao.BookDao' available

      错误信息显示在 Spring 容器中没有 BookDao 类型的 bean

      1. 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)}
      2. at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1801) ~[spring-beans-5.3.25.jar:5.3.25]
      3. at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1357) ~[spring-beans-5.3.25.jar:5.3.25]
      4. at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1311) ~[spring-beans-5.3.25.jar:5.3.25]
      5. at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:657) ~[spring-beans-5.3.25.jar:5.3.25]
      6. ... 74 common frames omitted
    • 原因

      • Mybatis 会扫描接口并创建接口的代码对象交给 Spring 管理,但是现在并没有告诉 Mybatis 哪个是 dao 接口
      • 注意:Mysql驱动版本大于8.0时,需要在url连接串中配置时区 jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC,或在MySQL数据库端配置时区解决此问题
    • 解决方案:在BookDao 接口上使用 @Mapper

      1. @Mapper
      2. public interface BookDao {
      3. @Select("select * from tbl_book where id = #{id}")
      4. public Book getById(Integer id);
      5. }

4.2.4 定义测试类

  1. @SpringBootTest
  2. class Springboot03MybatisApplicationTests {
  3. @Autowired
  4. private BookDao bookDao;
  5. @Test
  6. void contextLoads() {
  7. Book book = bookDao.getById(1);
  8. System.out.println(book);
  9. }
  10. }

4.2.5 编写配置

  1. spring:
  2. datasource:
  3. driver-class-name: com.mysql.jdbc.Driver
  4. url: jdbc:mysql://localhost:3306/ssm_db
  5. username: root
  6. password: 123456

4.2.6 使用 Druid 数据源

  • 添加依赖

    1. <dependency>
    2. <groupId>com.alibaba</groupId>
    3. <artifactId>druid</artifactId>
    4. <version>1.1.16</version>
    5. </dependency>
  • 添加配置,指定使用 Druid 数据源

    1. spring:
    2. datasource:
    3. driver-class-name: com.mysql.jdbc.Driver
    4. url: jdbc:mysql://localhost:3306/ssm_db
    5. username: root
    6. password: 123456
    7. type: com.alibaba.druid.pool.DruidDataSource

5、基于 SpringBoot 的 SSM 整合案例

  • 本节内容是对上一篇 SSM 整合案例的改进,可参见SpingMVC 学习笔记 - 第二章 -SSM整合案例
  • 开发步骤
    1. pom.xml:配置起步依赖,必要的资源坐标(druid)

    2. application.yml:设置数据源、端口等

    3. 配置类:现在不需要了

    4. dao:设置@Mapper

    5. 测试类

    6. 页面:放置在resources目录下的static目录中

5.1 创建工程

  • 依赖

    • Spring Web
    • MyBatis Framework
    • MySQL Driver
  • 引入Druid

    • 添加依赖

      1. <!-- TODO 添加必要的依赖 -->
      2. <dependency>
      3. <groupId>com.alibaba</groupId>
      4. <artifactId>druid</artifactId>
      5. <version>1.1.16</version>
      6. </dependency>

5.2 后端代码整理

  • config 包,对比 SSM 项目全部删除,现在不需要了

  • entity 包,实体,无变化

  • dao,添加@Mapper注解

    1. // TODO 添加@Mapper
    2. @Mapper
    3. public interface BookDao {
    4. @Insert("insert into tbl_book values(null,#{type},#{name},#{description})")
    5. // @Insert("insert into tbl_book (type,name,description) values(#{type},#{name},#{description})")
    6. public int save(Book book);
    7. @Update("update tbl_book set type = #{type}, name = #{name}, description = #{description} where id = #{id}")
    8. public int update(Book book);
    9. @Delete("delete from tbl_book where id = #{id}")
    10. public int delete(Integer id);
    11. @Select("select * from tbl_book where id = #{id}")
    12. public Book getById(Integer id);
    13. @Select("select * from tbl_book")
    14. public List<Book> getAll();
    15. }
  • service,无变化

  • exception,无变化

  • controller,无变化

  • 测试类,使用了 @SpringBootTest 注解,更换了使用的包

    1. // import org.junit.Test;
    2. import org.junit.jupiter.api.Test;
    3. // import ...
    4. //@RunWith(SpringJUnit4ClassRunner.class)
    5. //@ContextConfiguration(classes = SpringConfig.class)
    6. // TODO 将原先使用的注解更改为 @SpringBootTest
    7. @SpringBootTest
    8. public class BookServiceTest {
    9. @Autowired
    10. private BookService bookService;
    11. // TODO 原先使用的 @Test 注解 现在需要重新导包
    12. @Test
    13. public void testGetById() {
    14. Book byId = bookService.getById(1);
    15. System.out.println(byId);
    16. }
    17. @Test
    18. public void testGetAll() {
    19. List<Book> all = bookService.getAll();
    20. System.out.println(all);
    21. }
    22. }

5.3 配置文件

  1. #TODO 配置端口和数据源相关信息
  2. server:
  3. port: 80
  4. spring:
  5. datasource:
  6. type: com.alibaba.druid.pool.DruidDataSource
  7. driver-class-name: com.mysql.cj.jdbc.Driver
  8. url: jdbc:mysql://localhost:3306/ssm_db
  9. username: root
  10. password: 123456

5.4 静态资源

  • 静态资源存放在 resources 目录下的 static 目录下

  • 配置主页 index.html

    1. <!--TODO 配置主页-->
    2. <script>
    3. document.location.href="pages/books.html";
    4. </script>

5.5 包结构展示

包结构

5.6 其他问题

  • 若再未加入前端代码时启动过服务器,加入前端代码后无法正常访问到页面时
    • 执行 Maven 的 clean 指令,再重新启动服务器(重新打包)即可
  • SpringBoot 版本问题、MySQL时区问题、项目无法构建、配置文件打包乱码等问题,本节笔记中均有提及,可向上查阅

原文链接:https://www.cnblogs.com/dandelion-000-blog/p/17084667.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号