SpringBoot入门
1.springboot的优点
springboot相较于spring:
- 可以实现自动配置
- 起步依赖(引入少量坐标,就可以导入多个坐标)
- 辅助功能(服务状态信息监控)
2.springboot的快速构建
在idea中,通常springboot工程由两种构建方式,第二种方式较简单
2.1 Maven空工程构建
1.创建普通Maven工程
2.导入坐标
<!--springboot工程需要继承的父工程-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
</parent>
<dependencies>
<!--web开发的起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
3.写业务
@RestController
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentService studentService;
@GetMapping("/findAll")
public List<Student> findAll(){
List<Student> list = studentService.findAll();
return list;
}
4.引导类
@SpringBootApplication
public class SpringbootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootDemoApplication.class, args);
}
}
2.2 通过Spring Initializr构建
该方式需要联网构建。
当springboot工程构建完成过后,相当于完成了spring+springmvc的整合
3.springboot配置介绍
springboot配置文件有三种,
- application.properties
- application.yml
- application.yaml
三种文件的优先级为:application.properties>application.yaml>application.yml
注意:三个文件都能加载,当数据相同时,优先级高的会覆盖低优先级的
3.1 yml的基本语法
- yml中大小写严格区分
- yml中空格敏感,冒号后面必须有空格才能写数据,空格数目不要求,但是必须一致
- 缩进代表层级关系
- 注释使用#
- 与json语法类似
3.2yml的数据格式
对象(map):键值对的集合。
数组:一组按次序排列的值
**纯量:**单个的、不可再分的值
# 对象形式
mysql:
username: root
password: root
mysql1: {username: root, password: root}
# 数组形式
girls:
- cls
- ly
girls1: [cls, ly]
# 常量
msg1: 'hello \n hello'
msg2: "hello \n hello"
3.3springboot如何获取配置文件内容
- @Value
#获取普通配置
@Value("${name}")
private String name;
#获取对象属性
@Value("${person.name}")
private String name2;
#获取数组
@Value("${address[0]}")
private String address1;
#获取纯量
@Value("${msg1}")
private String msg1;
- Environment
@Autowired
private Environment environment;
@GetMapping("/hello")
public String hello(){
System.out.println(username);
System.out.println(environment.getProperty("server.port"));
System.out.println(environment.getProperty("mysql.username"))
}
-
@ConfigurationProperties
-
添加配置到配置文件
-
@Component
-
@ConfigurationProperties(prefix = "person")
-
3.4profiles
用来完成不同环境下,配置动态切换功能的
3.4.1profile配置方式
-
profiles的方式(文件命名不同)
application-dev.properties/yml 开发环境
application-test.properties/yml 测试环境
application-pro.properties/yml 生产环境
spring.profiles.active=pro
- yml文档切分的方式
---
server:
port: 8081
spring:
profiles: dev
---
server:
port: 8082
spring:
profiles: test
---
server:
port: 8083
spring:
profiles: pro
---
spring:
profiles:
active: pro
3.4.2profile激活方式
- 配置文件: 再配置文件中配置:spring.profiles.active=dev
- 虚拟机参数:在VM options 指定:-Dspring.profiles.active=dev
- 命令行参数:java –jar xxx.jar --spring.profiles.active=dev
####3.4.3配置文件加载顺序
- 1.命令行参数
- 2.参数spring.config.location指定文件
- 3.jar包同一目录下config目录下的配置问价
- 4.jar包同一目录下的配置文件
- 5.project下config目录下的配置
- 6.project根目录下的配置
- 7.resource/config目录下的配置
- 8.resource目录下的配置
4.整合其它框架
4.1springboot整合junit
如果测试类和启动类不在同一个包下,则需要添加启动类的字节码文件
-
搭建SpringBoot工程
-
引入starter-test起步依赖
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> -
编写测试类
@RunWith(SpringRunner.class)
@SpringBootTest
class SpringbootDemoApplicationTests {
@Autowired
StudentService studentService;
@Test
public void test1() {
List<Student> list = studentService.findAll();
for (Student student : list) {
System.out.println(student);
}
}
}
###4.2springboot整合mybatis
-
搭建SpringBoot工程
-
引入mybatis起步依赖,添加mysql驱动
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.2</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> -
编写DataSource和MyBatis相关配置
spring: datasource: #type: com.alibaba.druid.pool.DruidDataSource url: jdbc:mysql:///test?serverTimezone=Asia/Shanghai username: root password: root driver-class-name: com.mysql.cj.jdbc.Driver druid: max-active: 30 min-idle: 5 mybatis: type-aliases-package: com.itheima.springboot_demo.domain mapper-locations: classpath:mapper/*.xml -
编写mapper文件