Spring Boot 学习

83 阅读1分钟

Spring Boot 学习

案例:初始化一个 Spring Boot 程序

  1. 创建新模块,选择Spring初始化,并配置模块相关基础信息 image.png
  2. 选择当前模块需要使用的技术集 image.png
  3. 开发控制器类 image.png
  4. 运行自动生成的Application类 image.png

1、yaml 数据读取

  • 使用 @Value读取单个数据,属性名引用方式:${一级属性名.二级属性名...} image.png
  • 封装全部数据到 Environment 对象 image.png
  • 使用配置类的形式读取想要的数据(把配置类变成Bean,在读取的地方使用自动装配) image.png

2、多环境配置

image.png

2.1 yml 文件配置

# 设置启动环境
spring:
  profiles:
    active: pro

---

# 开发
server:
  port: 80
spring:
  profiles: dev
---

# 生产
server:
  port: 81
spring:
  profiles: pro

---

# 测试
server:
  port: 82
spring:
  profiles: test

2.2 properties 文件配置

image.png

2.2.1 临时修改配置参数

image.png

3、Maven 和 SpringBoot 多环境开发问题

  1. 在 Maven 中设置多环境属性 image.png
  2. 在 SpringBoot 中引用 Maven 属性 image.png
  3. 对资源文件进行解析 image.png

4、配置文件分类

image.png

5、SpringBoot 整合 MyBatis

  1. 首先新建模块,配置模块相关信息 image.png
  2. 选择当前模块需要使用的技术集(MaBatis、MySQl) image.png
  3. 设置数据源参数 image.png
  4. 将数据层接口配置映射(也就是让SpringBoot扫描到) image.png
  5. 在测试类中注入dao接口,测试功能 image.png
  6. 注意事项 image.png