Spring Boot回顾
一、概述
1、Spring的优缺点
- 优点
- 无需开发ELB,通过IOC和AOP,就可以使用POJO(简单的Java对象)实现ELB的功能
- 缺点:
- 依赖管理导入Maven耗时耗力
- 注解繁琐
2、SpringBoot的特点
- 入门简单,无需配置
- 提供非功能特性(嵌入式服务器)
二、快速入门
1、环境搭建
- 继承起步依赖
- 添加web的起步依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> </parent> <groupId>com.itheima</groupId> <artifactId>SpringBoot_QuickStart</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
- 编写引导类
package com.itheima; @SpringBootApplication public class MySpringBootApplication { public static void main(String[] args) { SpringApplication.run(MySpringBootApplication.class); } }
- 编写Controller
package com.itheima.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class QuickController { @RequestMapping("/quick") @ResponseBody public String quick(){ return "hello Spring Boot"; } }
2、细节
- @SpringBootApplication是引导类,run方法中运行引导类名,通常情况下引导类就是main方法所在的类 热部署:修改Controller源码时,不再需要重启项目即可自动加载
- 添加坐标
<!--热部署配置--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency>
-
- 修改设置,Build,Compiler,勾选Build project automatically
- Ctrl+Shift+Alt+/,选择Registry,勾选app.running
3、IDEA创建SpringBoot工程
- Spring Initializer
- 添加Module时选中Web
- 编写Controller
package com.itheima.controller; @RestController //将controller和responsebody放在一起,不再需要写responsebody public class QuickController { @RequestMapping("/quick2") public String quick(){ return "SpringBoot!!!"; } }
三、原理分析
1、起步依赖-parent
通过二次继承实现版本控制
2、起步依赖--web
内部有web相关的环境,如Spring/Spring MVC和Tomcat
四、配置文件
1、默认配置与覆盖
SpringBoot会从Resources/META-INF目录下加载application.properties或application.yml(application.yaml)文件,用来替换默认配置
加载顺序:
2、yml文件的介绍
- YML文件格式是YAML (YAML Aint Markup Language)编写的文件格式
- YML文件的扩展名可以使用.yml或者.yaml
- 普通数据和对象数据的配置
# 普通数据的配置 name: zhangsan # 对象的配置 # 通过缩进 #person: # name: zhangsan # age: 18 # addr: beijing server: port: 8083 # 行内对象配置(用的少) person: {name: zhangsan,age: 18,addr: beijing}
- 集合的配置
# 配置数组、集合(普通字符串) city: - beijing - tianjin - chongqing - shanghai - jinan # 行内方式 #city: [beijing,jinan,hefei,nanjing] # 数组、集合中存放对象数据 student: - name: tom age: 18 addr: hefei - name: xiaoming age: 17 addr: tianjin # 行内方式 # student: [{name: tom,age: 18,addr: nanjing},{name: xiaoming,age: 18,addr: tianjin}] # map配置 map: key1: value1 key2: value2
3、数据映射
- @Value注解:将配置文件的值映射到Bean属性上
@Controller public class Quick2Controller { @Value("${name}") private String name; @Value("${person.addr}") private String addr; @RequestMapping("/quick2") @ResponseBody public String quick2(){ //获得配置文件的 信息 return "name:"+name+" addr:"+addr; } }
- @ConfigurationProperties注解,放到类前,需要写getter和setter方法
@Controller @ConfigurationProperties(prefix = "person") public class Quick3Controller { //需要提供getset方法 private String name; private String addr; @RequestMapping("/quick3") @ResponseBody public String quick2(){ //获得配置文件的 信息 return "name:"+name+" addr:"+addr; } public String getName() { return name; } public void setName(String name) { this.name = name; }
- configuration-processor执行器,在yml文件中更快得到Bean的属性名
<!--@ConfigurationProperties的执行器的配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
Controller
@Controller @ConfigurationProperties(prefix = "person") public class Quick3Controller { //需要提供getset方法 private String name; private String addr; private Integer age; @RequestMapping("/quick3") @ResponseBody public String quick2(){ //获得配置文件的 信息 return "name:"+name+" addr:"+addr+" age:"+age; } public String getName() { return name; }
配置好后,只需要在yml中使用Alt+/快捷键,即可得到Bean的属性
五、与其他框架的集成
1、集成Mybatis
# 配置mybatis的信息 # 别名配置 #spring集成Mybatis环境 # pojo别名扫描包 mybatis.type-aliases-package=com.itheima.domain #加载Mybatis映射文件 mybatis.mapper-locations=classpath:mapper/*Mapper.xml
@Controller public class MybatisController { @Autowired private UserMapper userMapper; @RequestMapping("/query") @ResponseBody public List<User> queryUserList(){ List<com.itheima.domain.User> users = userMapper.queryUserList(); return users; } }
2、集成Junit
<!--SpringBoot集成Junit测试的起步依赖,IDEA中已经默认导入了--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
@RunWith(SpringRunner.class) @SpringBootTest(classes = SpringbootMybatisApplication.class) public class MybatisTest { @Autowired private UserMapper userMapper; @Test public void test(){ List<User> users = userMapper.queryUserList(); System.out.println(users); } }
3、集成SpringDataJPA
<!--导入起步依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!--添加数据库驱动依赖--> <!-- MySQL连接驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--jdk9需要导入如下坐标--> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.0</version> </dependency>
@RunWith(SpringRunner.class) @SpringBootTest(classes = SpringbootJpaApplicationTests.class) public class JpaTest { @Autowired private UserRepository userRepository; @Test public void test(){ List<User> all = userRepository.findAll(); System.out.println(all); } }
4、集成Redis
本文来自博客园,作者:哥们要飞,转载请注明原文链接:https://www.cnblogs.com/liujinhui/p/14634621.html