一、SpringBoot 框架的特点
1、聊聊 Springboot2.0 特点
1)SpringBoot继承了Spring优秀的基因,上手难度小
2)简化配置,提供各种默认配置来简化项目配置
3)内嵌式容器简化Web项目,简化编码
Spring Boot 则会帮助开发着快速启动一个 web 容器,在 Spring Boot 中,只需要在 pom 文件中添加如下一个 starter-web 依赖即可.
org.springframework.boot spring-boot-starter-web
4)发展趋势看
微服务是未来发展的趋势,项目会从传统架构慢慢转向微服务架构,因为微服务可以使不同的团队专注于更小范围的工作职责、使用独立的技术、更安全更频繁地部署。
2、一张图形容学SpringBoot的心情
![3381dab35a00abb057930c337130ffa2.gif](https://img-blog.csdnimg.cn/img_convert/3381dab35a00abb057930c337130ffa2.gif)
二、搭建SpringBoot的环境
1、创建一个简单的Maven项目
![a31aab5d58f89ec1108439241ca1b60e.png](https://img-blog.csdnimg.cn/img_convert/a31aab5d58f89ec1108439241ca1b60e.png)
2、引入核心依赖
org.springframework.boot spring-boot-starter-web
3、编写配置文件
application.yml
# 端口server: port: 8001
4、启动文件注解
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class HelloApplication { public static void main(String[] args) { SpringApplication.run(HelloApplication.class,args) ; }}
丝毫没有问题,就这样吧启动上面这个类,springboot的基础环境就搭建好了。
想想之前的Spring框架的环境搭建,是不是就是这个感觉:意会一下吧。
三、SpringBoot2.0 几个入门案例
1、创建一个Web接口
import com.boot.hello.entity.ProjectInfo;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/** * SpringBoot 2.0 第一个程序 */@RestControllerpublic class HelloController { @RequestMapping("/getInfo") public ProjectInfo getInfo (){ ProjectInfo info = new ProjectInfo() ; info.setTitle("SpringBoot 2.0 基础教程"); info.setDate("2019-06-05"); info.setAuthor("知了一笑"); return info ; }}
@RestController 注解 等价 @Controller + @ResponseBody 返回Json格式数据。
2、参数映射
1)首先看看SpringBoot 如何区分环境
![0b0e9d15dc2b6a0171d7ec7014498790.png](https://img-blog.csdnimg.cn/img_convert/0b0e9d15dc2b6a0171d7ec7014498790.png)
这里标识配置加载指定的配置文件。
2)参数配置
application-pro.yml
user: author: 知了一笑 title: SpringBoot 2.0 程序开发 time: 2019-07-05
3)参数内容读取
@Componentpublic class ParamConfig { @Value("${user.author}") private String author ; @Value("${user.title}") private String title ; @Value("${user.time}") private String time ;// 省略 get 和 set 方法}
4)调用方式
/** * 环境配置,参数绑定 */@RestControllerpublic class ParamController { @Resource private ParamConfig paramConfig ; @RequestMapping("/getParam") public String getParam (){ return "["+paramConfig.getAuthor()+";"+ paramConfig.getTitle()+";"+ paramConfig.getTime()+"]" ; }}
3、RestFul 风格接口和测试
1)Rest风格接口
/** * Rest 风格接口测试 */@RestController // 等价 @Controller + @ResponseBody 返回Json格式数据@RequestMapping("rest")public class RestApiController { private static final Logger LOG = LoggerFactory.getLogger(RestApiController.class) ; /** * 保存 */ @RequestMapping(value = "/insert