SpringBoot
一、SpringBoot简介
1. SpringBoot入门案例
@RestController
@RequestMapping("/books")
public class BookController {
@GetMapping("/{id}")
public String getById(@PathVariable Integer id){
System.out.println("id ==> "+id);
return "hello , spring boot!";
}
}
2. SpringBoot项目快速启动
3. SpringBoot概述
二、基础配置
1. 配置文件3种格式
2.yaml
读取yml文件信息
#次启动文件
server:
port: 80
logging:
level:
root: info # warn 默认info debug
lesson: SpringBoot
enterprise:
name: itcast
age: 16
tel: 400618
subject:
- Java
- 前端
- 大数据
- 人工智能
- 机器人
第1种方法–读取单个数据
@RestController
@RequestMapping("/books")
public class BookController {
//读取yml数据
@Value("${lesson}")
private String lesson;
@Value("${server.port}")
private Integer port;
@Value("${enterprise.subject[0]}")
private String subject_00;
private Enterprise enterprise;
@GetMapping("/{id}")
public String getById(@PathVariable Integer id) {
System.out.println("id ==> " + id);
System.out.println("lesson = " + lesson);
System.out.println("port = " + port);
System.out.println("subject_00 = " + subject_00);
return "hello , spring boot!" + " " + lesson + " " + port + " " + subject_00;
}
}
第2种方法–封装全部数据到Environment对象
@RestController
@RequestMapping("/books")
public class BookController {
//读取yml数据
@Value("${lesson}")
private String lesson;
@Value("${server.port}")
private Integer port;
@Value("${enterprise.subject[0]}")
private String subject_00;
@Autowired
private Environment environment;
@GetMapping("/{id}")
public String getById(@PathVariable Integer id) {
System.out.println("id ==> " + id);
System.out.println("lesson = " + lesson);
System.out.println("port = " + port);
System.out.println("subject_00 = " + subject_00);
System.out.println("---------------------------------------");
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[0]"));
return "hello , spring boot!" + " " + lesson + " " + port + " " + subject_00;
}
}
第3种方法–自定义对象封装指定数据
- Enterprise.java
@Component //定义成一个bean对象
@ConfigurationProperties(prefix = "enterprise") //读取配置中的属性
public class Enterprise {
private String name;
private Integer age;
private String tel;
private String[] subject;
@Override
public String toString() {
return "Enterprise{" +
"name='" + name + '\'' +
", age=" + age +
", tel='" + tel + '\'' +
", subject=" + Arrays.toString(subject) +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer 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;
}
}
@RestController
@RequestMapping("/books")
public class BookController {
//读取yml数据
@Value("${lesson}")
private String lesson;
@Value("${server.port}")
private Integer port;
@Value("${enterprise.subject[0]}")
private String subject_00;
@Autowired
private Environment environment;
@Autowired
private Enterprise enterprise;
@GetMapping("/{id}")
public String getById(@PathVariable Integer id) {
System.out.println("id ==> " + id);
System.out.println("lesson = " + lesson);
System.out.println("port = " + port);
System.out.println("subject_00 = " + subject_00);
System.out.println("---------------------------------------");
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[0]"));
System.out.println("---------------------------------------");
System.out.println(enterprise.toString());
return "hello , spring boot!" + " " + lesson + " " + port + " " + subject_00;
}
}
<!--自定义对象封装数据警告解决方案-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
3.多环境启动【Maven与SpringBoot关联操作(多环境配置 )】
Maven与SpringBoot多环境兼容
- pom.xml
<?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.5.0</version>
</parent>
<groupId>com.itcast</groupId>
<artifactId>SpringBoot_study_2301</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>
<!--排除依赖-->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--jetty服务器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<!--自定义对象封装数据警告解决方案-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</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>
<!--对资源文件开启默认占位符的解析-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<encoding>UTF-8</encoding>
<useDefaultDelimiters>true</useDefaultDelimiters>
</configuration>
</plugin>
</plugins>
</build>
<!--Maven中设置多环境属性-->
<profiles>
<!--开发环境-->
<profile>
<id>dev</id>
<properties>
<profile.active>dev</profile.active>
</properties>
</profile>
<!--生产环境-->
<profile>
<id>pro</id>
<properties>
<profile.active>pro</profile.active>
</properties>
<!--默认启动生产环境-->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<!--测试环境-->
<profile>
<id>test</id>
<properties>
<profile.active>test</profile.active>
</properties>
</profile>
</profiles>
</project>
- application.yml
#次启动文件
#设置启动的环境
#SpringBoot中引用Maven属性
spring:
profiles:
active: ${profile.active}
---
#开发
server:
port: 80
spring:
config:
activate:
on-profile: dev
---
#生产
spring:
profiles: pro
server:
port: 81
---
#测试
spring:
profiles: test
server:
port: 82
---
logging:
level:
root: info # warn 默认info debug
4. 配置文件分类
三、整合第三方技术
1.整合JUnit
2.基于SpringBoot实现SSM整合
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/ssm_db?serverTimezone=UTC
username: root
password: 123456
type: com.alibaba.druid.pool.DruidDataSource
基于SpringBoot实现SSM整合案例
基于SpringBoot实现SSM整合案例: https://gitee.com/xiaoyuwifi/spring-boot_ssm_01