程序员书籍笔记 程序员书籍笔记
  • HTML
  • CSS
  • JavaScript
  • 前端知识
  • Vue
  • MarkDown
  • git
  • Node.js
  • Linux
  • 51单片机
  • 四级
  • 第一学期课程
  • 操作系统
  • 计算机网络
  • 数据结构
  • 计算机组成原理
  • HTML5
  • Electron
  • 日记便签
  • 前端导航
GitHub (opens new window)
  • HTML
  • CSS
  • JavaScript
  • 前端知识
  • Vue
  • MarkDown
  • git
  • Node.js
  • Linux
  • 51单片机
  • 四级
  • 第一学期课程
  • 操作系统
  • 计算机网络
  • 数据结构
  • 计算机组成原理
  • HTML5
  • Electron
  • 日记便签
  • 前端导航
GitHub (opens new window)
  • Vue

  • Nuxt

  • Echarts

  • Node

  • git

  • express

  • 微信小程序

  • Spring

    • Spring基础01
    • Spring基础02
    • Spring基础03
    • SpringMVC基础01
    • SpringMVC基础02
    • Mybatis基础01
    • MybatisPlus基础01
    • SpringBoot快速开始
      • SpringBoot
      • 创建Boot项目
      • 依赖管理
      • 配置文件
        • yaml
        • 多环境配置
        • jar包启动命令
        • 与Maven兼容
      • 整合SSM
  • 后端知识

  • Markdown

  • project

  • 自用文档查询

  • 框架和软件
  • Spring
yuadh
2022-10-16
目录

SpringBoot快速开始

# SpringBoot

# 创建Boot项目

使用 Spring Initializr 工具快速创建 SpringBoot 项目

勾选上 Spring WEB 依赖,会自动导入 spring-boot-starter-parent 依赖

SpringBoot 会自动配置集成相关的环境,现编写控制类测试

@RestController
@RequestMapping("/books")
public class BookController {
    @GetMapping("/{id}")
    public String getById(@PathVariable Integer id) {
        System.out.println("id ==> " + id);
        return "hello , spring boot! ";
    }
}
1
2
3
4
5
6
7
8
9

现在通过网址接口,可以测试项目运行情况

# 依赖管理

starter

spring-boot-starter-parent

Spring Boot 将日常企业应用研发中的各种场景都抽取出来,做成一个个的 starter(启动器),starter 中整合了该场景下各种可能用到的依赖,用户只需要在 Maven 中引入 starter 依赖,SpringBoot 就能自动扫描到要加载的信息并启动相应的默认配置。starter 提供了大量的自动配置,让用户摆脱了处理各种依赖和配置的困扰。所有这些 starter 都遵循着约定成俗的默认配置,并允许用户调整这些配置,即遵循“约定大于配置”的原则。

并不是所有的 starter 都是由 Spring Boot 官方提供的,也有部分 starter 是第三方技术厂商提供的,例如 druid-spring-boot-starter 和 mybatis-spring-boot-starter 等等。当然也存在个别第三方技术,Spring Boot 官方没提供 starter,第三方技术厂商也没有提供 starter。

以 spring-boot-starter-web 为例,它能够为提供 Web 开发场景所需要的几乎所有依赖,因此在使用 Spring Boot 开发 Web 项目时,只需要引入该 Starter 即可,而不需要额外导入 Web 服务器和其他的 Web 依赖。

参考 : Spring Boot starter入门 (biancheng.net) (opens new window)

启动器

spring-boot-starter-web

Spring MVC 是 Spring 提供的一个基于 MVC 设计模式的轻量级 Web 开发框架,其本身就是 Spring 框架的一部分,可以与 Spring 无缝集成,性能方面具有先天的优越性,是当今业界最主流的 Web 开发框架之一。

Spring Boot 是在 Spring 的基础上创建一款开源框架,它提供了 spring-boot-starter-web(Web 场景启动器) 来为 Web 开发予以支持。spring-boot-starter-web 为我们提供了嵌入的 Servlet 容器以及 SpringMVC 的依赖,并为 Spring MVC 提供了大量自动配置,可以适用于大多数 Web 开发场景。

参考:spring-boot-starter-web(Web启动器) (biancheng.net) (opens new window)

# 配置文件

SpringBoot 提供了多种属性配置文件

application.properties > application.yml > application.yaml

示例

yml

server:
  port: 81
1
2

properties

server.port=80
1

# yaml

简单配置编写

server:
  port: 80


lesson: yuadh

likes:
  - java
  - 前端
  - Go

enterprise:
  name: yuadh
  age: 22
  tel: 15507081172
  subject:
    - java
    - 前端
    - 大数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

填装类

@Component
@ConfigurationProperties(prefix = "enterprise")
public class Enterprise {
    private String name;
    private Integer age;
    private String tel;
    private String[] subject;
		//getter setter
}
1
2
3
4
5
6
7
8
9

控制类使用

@Value("${lesson}")
private String lessonName;
@Value("${server.port}")
private int port;
@Value("${likes[1]}")
private String[] likes;
@Autowired
Enterprise enterprise;
1
2
3
4
5
6
7
8

# 多环境配置

spring:
  profiles:
    active: test
---
spring:
  profiles: pro
server:
  port: 80
---
spring:
  profiles: dev
server:
  port: 81
---
spring:
  profiles: test
server:
  port: 82
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# jar包启动命令

java –jar springboot.jar --spring.profiles.active=test
java –jar springboot.jar --server.port=88
java –jar springboot.jar --server.port=88 --spring.profiles.active=test
1
2
3

参考文档:https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config (opens new window)

# 与Maven兼容

maven

<profiles>
    <profile>
        <id>dev_env</id>
        <properties>
            <profile.active>dev</profile.active>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>pro_env</id>
        <properties>
            <profile.active>pro</profile.active>
        </properties>
    </profile>
    <profile>
        <id>test_env</id>
        <properties>
            <profile.active>test</profile.active>
        </properties>
    </profile>
</profiles>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

引用

spring:
 profiles:
  avtive:${profile.active}
1
2
3

打包运行

占位符解析插件

<build>
    <plugins>
        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <configuration>
                <encoding>utf-8</encoding>
                <useDefaultDelimiters>true</useDefaultDelimiters>
            </configuration>
        </plugin>
    </plugins>
</build>
1
2
3
4
5
6
7
8
9
10
11

# 整合SSM

使用 Spring Initializr 工具初始化 boot 项目,选中 Mybatis、MySQL、Web依赖

  1. 配置 sql 数据
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC
    username: root
    password: root
    type: com.alibaba.druid.pool.DruidDataSource
1
2
3
4
5
6
7

SpringBoot版本低于2.4.3(不含),Mysql驱动版本大于8.0时,需要在url连接串中配置时区,或在MySQL数据库端配置时区解决此问题

  1. 导入 druid 数据库连接池包
<!-- todo 1 添加druid连接池依赖-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.2.6</version>
</dependency>
1
2
3
4
5
6
  1. Dao层接口加上 @Mapper 注解
  2. 复制之前代码,可无需配置直接运行
编辑 (opens new window)
上次更新: 2023/02/07, 14:51:48
MybatisPlus基础01
REST风格

← MybatisPlus基础01 REST风格→

Theme by Vdoing | Copyright © 2021-2023 yuadh
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×