spring官网 spring.io
SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的初始搭建以及开发过程
先创建一个空工程
1:创建一个controller
2.执行main方法
3.浏览器访问地址 localhost:8080/book
将下载的压缩包解压到工作目录即可
与方式一相同,改ip即可
编辑pom.xml
4.0.0 org.springframework.boot spring-boot-starter-parent 2.6.2 com.peppacatt springboot_01_04_quickstart 1.0-SNAPSHOT 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test
添加启动类:
所有spring官方的starter都是以spring-boot-starter开头spring-boot-starter-xxx格式.例如:
org.springframework.boot spring-boot-starter-web
第三方技术的starter以spring-boot-starter结尾xxx-spring-boot-starter格式,例如:
org.mybatis.spring.boot mybatis-spring-boot-starter 2.2.0
找到parent标签,ctrl+鼠标左键 进入
继续点击进入
查看properties中定义的版本
展开properties标签
展开 标签
查看启动类
package com.peppacatt.springboot_01_01_quickstart;
import com.peppacatt.springboot_01_01_quickstart.controller.BookController;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class Springboot0101QuickstartApplication {
public static void main(String[] args) {
ConfigurableApplicationContext cac = SpringApplication.run(Springboot0101QuickstartApplication.class, args);
BookController bookController = cac.getBean(BookController.class);
System.out.println(bookController);
}
}
查看运行结果:
springboot三种配置文件类型,名称都必须为application.xxx
- application.properties
- application.yml
- application.yaml
在开发中只会有一个配置文件,如果配置文件共存,则:
yml语法规则:
数组格式:
①和②两者格式都可以,②是①的缩略版
对象数组格式:
总结
yml:
spring读取:
结果:
总结
总结
1.新建
新建封装类:
package com.peppacatt.springboot_03_yml.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
//定义为spring管理的bean
@Component
//指定加载的数据
@ConfigurationProperties(prefix = "cat")
public class PropertiesCat {
private String name;
private Integer age;
private Integer gender;
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 Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
@Override
public String toString() {
return "YmlCat{" +
"name='" + name + ''' +
", age=" + age +
", gender=" + gender +
'}';
}
}
使用:
springboot已整合
使用案例:
如果测试类不在启动类的包或者子包下面,则需要添加classes
添加依赖:
或者手动添加:
org.mybatis.spring.boot mybatis-spring-boot-starter 2.2.0 mysql mysql-connector-java runtime



