1. 简介
SpringBoot设计的目的是简化Spring应用的初始搭建以及开发过程。
2. 入门案例
2.1 引入依赖
<?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.lan</groupId>
<artifactId>springboot_01_quickstart</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>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 打包插件,打包成fatjar -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.2 启动类
package com.lan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication // 添加springboot注解
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
3. 配置文件
同目录下的配置文件优先级如下:
application.properties > application.yml > application.yaml
不同目录下的配置文件优先级如下:
classpath:application.yml < classpath:config/application.yml < file:application.yml < file:config/application.yml
3.1 读取配置数据
1. 使用@Value注解即可读取到配置文件中的数据
2. 使用Environment对象
@Autowired
private Environment env;
public void readProperty() {
env.getProperty("server.port");
}
3. 使用@ConfigurationProperties注解读取指定前缀的属性到当前JavaBean中,配合@PropertySource注解,还可以读取指定的配置文件。
@Component
@ConfigurationProperties(prefix="spring.datasource")
@Data
public class DatasourceBean {
private String jdbcUrl;
private String driver;
private String username;
private String password;
}
@Autowried
private DatasourceBean datasource;
public void readProperty() {
datasource.getUsername();
}
注意:@ConfigurationProperties是springboot的注解,@PropertySource是spring的注解
3.2 多环境配置
1. yaml或者yml的实例
# 表示激活dev,激活多个时,用逗号隔开
spring:
profiles:
active: dev
---
#开发
spring:
config:
activate:
on-profile: dev
server:
port: 80
---
#生产
#spring.profiles是过时的配置
spring:
profiles: pro
server:
port: 81
2. yaml、yml或properties
application.properties
#设置启用的环境,不设置时,默认只加载application.properties
#表示加载application-pro.properties
spring.profiles.active=pro
# 其它公共配置...
...
application-dev.properties
server.port=8080
application-pro.properties
server.port=80
3. 通过jvm参数指定-Dspring.profiles.active=xxx,表示激活profile=xxx的配置
4. SpringBoot整合Junit
案例
package com.lan;
import com.lan.service.BookService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
// SpringBoot测试注解,不指定时引导类时,测试类所在包必须是测试类所在包及其子包
@SpringBootTest(classes = Application.class)
class BookServiceTest {
@Autowired
private BookService bookService;
@Test
public void save() {
bookService.save();
}
}
5. 整合MyBatis
5.1 引入依赖
添加mybatis的starter依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
5.2 编写DAO
package com.lan.dao;
import com.lan.domain.Book;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface BookDao {
@Select("select * from tbl_book where id = #{id}")
public Book getById(Integer id);
}
mybatis-starter会自动扫描带@Mapper注解的DAO接口,并且将自动将接口的返回结果类型都扫描到别名中。
假如DAO接口没加@Mapper注解,则可以在配置类或者启动类中添加@MapperScan注解指定扫描的DAO的package。
假如有mapper文件需要扫描,可以在application.yml添加以下配置扫描对应的mapper文件。
mybatis:
mapper-locations: classpath:mapper/*.xml