概述
1、SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的初始搭建以及开发过程
2、Spring程序缺点
- 配置繁琐
- 依赖设置繁琐
3、SpringBoot程序优点 - 自动配置
- 起步依赖(简化依赖配置)
- 辅助功能(内置服务器,……)
使用
1、新建一个spring项目,导入sprinboot 依赖
<!--版本控制-->
<parent>
<artifactId>spring-boot-dependencies</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.4.5</version>
</parent>
<dependencies>
<!--起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
2、编写启动类·
package soLo.iu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
整合第三方
整合JUnit
- 创建spring项目,导入springboot起步依赖
<!--版本控制-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
2 导JUnit依赖
<!--junit起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
2、编写测试类
SSM整合
- SpringBoot整合Spring(不存在)
- SpringBoot整合SpringMVC(不存在)
- SpringBoot整合MyBatis(主要)
基于springboot整合ssm主要整合的是MyBatis
- 导入mybtis依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
- 导入mysql依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--druid连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.4</version>
</dependency>
- 设置数据源参数
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/demo?serverTimezone=UTC
username: root
password: 1234
- 定义数据层接口与映射配置
@Mapper
public interface BrandMapper {
Brand selectOneById(int id);
}
springboot配置文件
SpringBoot提供了多种属性配置方式,通常有:
- application.properties
server.port=80
- application.yml
server:
port: 81
- application.yaml
server:
port: 82
加载顺序
- application.properties > application.yml > application.yaml
注意事项:
- SpringBoot核心配置文件名为application
- SpringBoot内置属性过多,且所有属性集中在一起修改,在使用时,通过提示键+关键字修改属性
多环境开发配置
多环境启动配置
- yaml文件多环境启动



- properties文件多环境启动
#主启动配置文件 application.properties
spring.profiles.active=pro
#环境分类配置文件 application-pro.properties
server.port=80
#环境分类配置文件 application-dev.properties
server.port=81
#环境分类配置文件application-test.properties
server.port=82
多环境开发控制
Maven与SpringBoot多环境兼容(步骤)
- 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>
-
SpringBoot中引用Maven属性

-
执行Maven打包指令
- Maven指令执行完毕后,生成了对应的包,其中类参与编译,但是配置文件并没有编译,而是复制到包中

- 解决思路:对于源码中非java类的操作要求加载Maven对应的属性,解析${}占位符
- 对资源文件开启对默认占位符的解析
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>utf-8</encoding>
<useDefaultDelimiters>true</useDefaultDelimiters>
</configuration>
</plugin>
</plugins>
</build>
- Maven打包加载到属性,打包顺利通过

7420

被折叠的 条评论
为什么被折叠?



