目录
1、概述
Spring Boot应用本质上就是一个基于Spring框架的应用,它是Spring对“约定优先于配置”理念的最佳实践产物,它能够帮助开发者更快速高效地构建基于Spring生态圈的应用。
spring缺点:参考这篇文章Spring MVC 到 Spring Boot 的简化之路 - 掘金
- 配置繁琐;
- 依赖繁琐;
springboot优点:
- 自动配置;
- 起步依赖;依赖传递
- 辅助功能;
SpringBoot并不是对Spring的功能上的增强,而是提供了一种快速使用的Spring的方式。
2、springboot的快速入门
pom.xml
<!-- springboot 工程需要继承的父工程 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
</parent>
<dependencies>
<!-- web开发的起步依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
启动引导类
@SpringBootApplication
public class HelloApplication {
public static void main(String[] args) {
SpringApplication.run(HelloApplication.class, args);
}
}
编写controller接口类
@RestController
public class HelloController {
@GetMapping("/hello")
//@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello () {
return "欢迎来到SpringBoot开发环境";
}
}
3、起步依赖分析
spring-boot-starter-parent:定义了各种技术的版本信息,组合了一套最优的版本。
在starter中,定义了完成该功能需要的坐标合集,大部分版本信息来自于父工程。
spring-boot-starter-web:引入starter后,通过依赖传递,可以获得需要的jar包,并且不会存在版本冲突问题。
4、配置文件(yml格式)
分类
同一级目录的优先级:application.properties > application.yml > application.yaml
4.1 yml语法
YAML是”YAML Ain't markup language"(YAML不是一种标记语言)的缩写,是一种对人类设计友好(方便读写)的数据序列化语言,可以很好地与其它编程语言协同完成日常任务。
语法格式:
- 文件名以.yaml结尾
- 大小写敏感
- 不允许使用Tab。由于Tab的支持性不够普通,因此使用空格。
YAML学习介绍:YAML简介 - 掘金
# 数据格式
name: diaochan
# 1. 对象
person:
# 参数引用
name: ${name} #wangzhaojun
age: 18
# 对象的行内写法
person2: {name: yangyuhuan,age: 20}
# 2. 数组
address:
- beijing
- shanghai
- hefei
# 数组的行内写法
address2: [beijing,shanghai,hefei]
# 3. 纯量
msg1: 'hello \n love1' # 忽略转义字符
msg2: "hello \n love2" # 识别转义字符
4.2 读取配置文件内容
@Value("${name}")
private String username;
@Value("${person.name}")
private String username2;
@Value("${person.age}")
private Integer age;
@Value("${address[0]}")
private String address1;
@Value("${address[1]}")
private String address2;
@Value("${msg1}")
private String msg1;
@Value("${msg2}")
private String msg2;
@Resource
private Environment env;
@GetMapping("/hello3")
public String hello3(){
System.out.println(username);
System.out.println(username2);
System.out.println(age);
System.out.println(address1);
System.out.println(address2);
System.out.println(msg1);
System.out.println(msg2);
System.out.println("========================");
System.out.println(env.getProperty("name"));
System.out.println(env.getProperty("person.name"));
System.out.println(env.getProperty("address[0]"));
System.out.println(env.getProperty("address[1]"));
System.out.println(env.getProperty("msg1"));
System.out.println(env.getProperty("msg2"));
return "四大美女";
}
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String name;
private Integer age;
private String[] address;
//getter/setter 略
//toString 略
@Resource
private Person person;
System.out.println(person);
String[] address = person.getAddress();
for (String s : address) {
System.out.println(s);
}
4.3 profile
Profiles 是 Spring 框架的核心特性,表示一个“环境”的概念,允许开发者将 bean 映射到不同的环境中,然后在不同的环境下激活不同的 Profile 以保证只启动需要的 bean。
1)profile配置方式
方式一:
激活profile
# 激活profile
#spring.profiles.active=dev
spring.profiles.active=pro
方式二:
---
server:
port: 8081
spring:
profiles: dev
---
server:
port: 8082
spring:
profiles: test
---
server:
port: 8083
spring:
profiles: pro
---
# 激活profile
spring:
profiles:
active: pro
2)激活方式
-Dspring.profiles.active=test
--spring.profiles.active=pro
# 配置文件激活profile
spring:
profiles:
active: dev
java -jar .\springboot_profile-0.0.1-SNAPSHOT.jar --spring.profiles.active=pro
4.4、内部加载配置
4.5 外部加载配置
5、springboot整合其他框架
5.1 springboot整合Junit
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringbootJunitApplication.class)
public class UserServiceTest {
@Autowired
private UserService userService;
@Test
public void testAdd(){
userService.add();
}
}
5.2 springboot整合redis
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
@SpringBootTest
class SpringbootRedisApplicationTests {
@Resource
private RedisTemplate redisTemplate;
@Test
void testSet() {
//存储数据
redisTemplate.boundValueOps("name").set("杨玉环");
}
@Test
void testGet(){
//获取数据
Object name = redisTemplate.boundValueOps("name").get();
System.out.println(name);
}
}
# 配置redis
spring:
redis:
host: 127.0.0.1
port: 6379
5.3 springboot整合mybatis
pom.xml
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
@Data
public class User {
private Integer id;
private String username;
private String password;
private String phoneNum;
private Integer status;
private String birth;
}
@Mapper
@Repository
public interface UserMapper {
@Select("select * from t_user")
List<User> getAll();
}
# 配置数据源信息
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/db_test?serverTimezone=UTC
username: root
password: root
# 配置mybatis
mybatis:
mapper-locations: classpath:cn/hfnu/mapper/*Mapper.xml # 映射文件位置
type-aliases-package: cn.hfnu.domain
# config-location: # mybatis核心配置文件
@Resource
private UserMapper userMapper;
@Test
void testGetAll() {
List<User> userList = userMapper.getAll();
userList.forEach(System.out ::println);
}