目录
SpringBoot
是对 Spring
开发进行简化的。
那我们先来看看SpringMVC开发中的一些必须流程。

从上面的 SpringMVC
程序开发可以看到,前三步都是在搭建环境,而且这三步基本都是固定的。SpringBoot
就是对这三步进行简化了。
对比一下 Spring
程序和 SpringBoot
程序。

使用SpringBoot后
我们只需要定义Controller和Application
java
@RestController
@RequestMapping("/books")
public class BookController {
@GetMapping("/{id}")
public String getById(@PathVariable Integer id){
System.out.println("id ==> "+id);
return "hello , spring boot!";
}
}
引导类
java
@SpringBootApplication
public class Springboot01QuickstartApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot01QuickstartApplication.class, args);
}
}
SpringBoot配置文件
SpringBoot的配置文件application.yml application这个名称不能改变
java
server:
port: 82
怎么读取yml文件的配置数据
假如yml文件的数据如下:
java
lesson: SpringBoot
server:
port: 80
enterprise:
name: itcast
age: 16
tel: 4006184000
subject:
- Java
- 前端
- 大数据
使用 @Value("表达式")
注解可以从配合文件中读取数据,注解中用于读取属性名引用方式是:${一级属性名.二级属性名......}
java
@RestController
@RequestMapping("/books")
public class BookController {
@Value("${lesson}")
private String lesson;
@Value("${server.port}")
private Integer port;
@Value("${enterprise.subject[0]}")
private String subject_00;
@GetMapping("/{id}")
public String getById(@PathVariable Integer id){
System.out.println(lesson);
System.out.println(port);
System.out.println(subject_00);
return "hello , spring boot!";
}
}
多环境配置
以后在工作中,对于开发环境、测试环境、生产环境的配置肯定都不相同,比如我们开发阶段会在自己的电脑上安装 mysql
,连接自己电脑上的 mysql
即可,但是项目开发完毕后要上线就需要该配置,将环境的配置改为线上环境的。

SpringBoot整合junit
在与引导类对应的测试文件的包目录下,创建测试类,然后在类上添加@SpringBootTest 注解和在方法上添加@Test注解

SpringBoot整合mybatis
1.在创建时勾选需要的模块

2.定义实体类

3.定义dao接口

4.编写数据库配置

5.使用Druid数据源
