基本创建
创建一个MAVEN模块
导入父模块坐标
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.0</version>
</parent>
导入坐标
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
创建启动类
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
SpringBoot在和Spring的区别
导入的依赖变少了:因为在创建SpringBoot项目时需要导入一个父模块,这个模块帮我们整理了很多的坐标,并且已经导入,所有我们继承这个父类就相当于有了这些坐标。
版本号没了:主要也是因为父模块帮我们导入了坐标,所以我们在引用的时候,只需要通过名称导入。
配置类没了:在导入坐标时,对应的坐标模块已经已经将我们所需要的一些固定的模块已经创建
SpringBoot配置文件
properties
application.properties
spring.profiles.active=dev
application.yaml
application.yml(常用)
server:
port: 81
如果有层级关系,下一层级要在前面加一个空格
注意,数据和名称之间要加空格
多环境配置
yml配置文件
#设置启用的环境
spring:
profiles:
active: dev
---
#开发
spring:
profiles: dev
server:
port: 8080
servlet:
#设置路径
context-path: /spring
#设置日志级别
logging:
level:
root: info
---
#生产
spring:
profiles: pro
server:
port: 81
---
#测试
spring:
profiles: test
server:
port: 82
properties配置文件
#文件名application.properties
spring.profiles.active=dev
#文件名application-dev.properties
server.port=8088
#文件名application-pro.properties
server.port=8081
#文件名application-test.properties
server.port=8082
从配置文件中读取数据
stu:
name: zhangsan
age: 18
likes:
- music
- paly
- sing
1.给成员变量赋值
@RestController
@RequestMapping("/boots")
public class BootController {
@Value("${stu.name}")
//直接给成员变量赋值
private String name;
@GetMapping("/{id}")
public String boot(@PathVariable Integer id){
System.out.println(name);
return "boot start";
}
}
2.使用Environment中的方法读取
@RestController
@RequestMapping("/boots")
public class BootController {
@Autowired
//使用Environment中的getProperty方法获取配置文件中的数据
private Environment environment;
@GetMapping("/{id}")
public String boot(@PathVariable Integer id){
System.out.println(environment.getProperty("stu.age"));
return "boot start";
}
}
3.定义实体类封装数据
@Component//让本类被Spring控制
@ConfigurationProperties(prefix = "stu")当前对象从配置中读取属性
public class Enterprise {
private String name;
private Integer age;
private String[] likes;
//get和set.....
}
@RestController
@RequestMapping("/boots")
public class BootController {
@Autowired
//定义一个实体类用来封装配置文件中的数据
private Enterprise enterprise;
@GetMapping("/{id}")
public String boot(@PathVariable Integer id){
enterprise.getLikes();
return "boot start";
}
}
整合junit
导入坐标
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
Spring整合junit需要在测试类上加
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=SpringConfig.class)
SpringBoot整个junit只需要加上
@SpringBootTest
如果main的引导类和test下的测试类包层不一致那么就需要在括号内加入参数
@SpringBootTest(classes=类名.class)
整合Mybatis
需要导入SpringBoot整合Mybatis和SpringBoot整合Mysql的坐标
<!--SpringBoot整合Mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<!--SpringBoot整合Mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
创建实体类
编写yml配置文件
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql:///db1?useSSL=false
username: root
password: 1234
type: com.alibaba.druid.pool.DruidDataSource #数据源类型
如果Mysql驱动版本过低就需要加上cj
driver-class-name: com.mysql.cj.jdbc.Driver
Mysql的驱动高于8.0,SpringBoot版本低于2.4.3(不含)就需要配置时区
url: jdbc:mysql:///db1?serverTimezone=serverTimezone=GMT%2B8
#东八区
创建dao接口
@Mapper//配置映射
public interface BookDao {
@Select("select * from tbl_book where id = #{id}")
Book selectById(Integer id);
}
在Spring整合Mybatis的时候,我们的dao层接口是不需要加注解的
但是在SpringBoot中我们需要加上@Mapper注解,用来配置映射
SpringBoot自动配置的原理
1.springboot基于约定
springboot会读取META-INF中的spring.factories文件
一般起步依赖都会将要加载的配置类写到spring.factories文件中
当spring.factories文件被SpringBoot读取后,对应的配置类则会被加载到IOC容器中
2.具体在哪加载的
@SpringBootApplication ->
// 开启自动装配
@EnableAutoConfiguration ->
// 加载类
@Import({AutoConfigurationImportSelector.class}) ->
// 返回一个类的全名字的字符串数组
public String[] selectImports(AnnotationMetadata annotationMetadata) {
AutoConfigurationImportSelector.AutoConfigurationEntry autoConfigurationEntry = this.getAutoConfigurationEntry(annotationMetadata);
return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
} ->
getAutoConfigurationEntry(AnnotationMetadata annotationMetadata) {
List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes);
return new AutoConfigurationImportSelector.AutoConfigurationEntry(configurations, exclusions);
} ->
List<String> getCandidateConfigurations() {
List<String> configurations = SpringFactoriesLoader.loadFactoryNames(
// 读取META-INF/spring.factories文件中的配置类
this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
// 断言,此集合不为空
Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.");
// 返回配置类全名的集合
return configurations;}