SpirngBoot
1.SpingBoot简介
优点:
- 简化开发配置
- 开发速度快
特点
-
自动装配,默认配置
-
起步依赖(Start一站式)直接引入就可以了
-
创建独立的Spring应用程序(无序打包,内嵌Tomcat)
-
监控能力强
-
少的XML配置
核心
- IOC和AOP
- 精简高效
- 基于SpringBoot有了Spring Cloud
2.SpringBoot的版本选择
尽量选择稳定版,选择合适的版本,跟其他组件兼容性高的。
-
CURRENT:最新版本
-
GA:稳定版
-
SNAPOSHOT:快照版,随时更新
项目升级,需要参考版本升级文档,最新的不一定是最好的。
3.新建Spring Boot项目
两种方式
(1)SpringBoot官网进行生成(https://start.spring.io/)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nkWrsY8k-1632977668310)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20210930095742145.png)]
(2)IDEA创建SpringBoot项目
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jj0FjeI9-1632977668317)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20210930100026387.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PvGc9OZS-1632977668319)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20210930100434350.png)]
注意创建好工程后
- 检查版本,选用合适项目开发的版本,避免版本不统一造成的失误
- 标签是必要的,表示父启动
- 必要的依赖
- 必要的插件(项目运行和打包有用)
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
4.案例演示
开发一个SpingBoot接口
- 遵循MVC三层架构的开发模式
- 新建一个controller接口
package com.imooc.springboot.controller;
import org.springframework.web.bind.annotation.*;
/**
* 第一个SpringBoot项目案例
*/
@RestController
public class fistSpringBoot {
@GetMapping("/f")
public String firstSpringBoot(){
return "第一个SpringBoot的接口";
}
/**
* GET 请求传参
* @param id
* @return
*/
@GetMapping("/g")
public String get(@RequestParam String id){
return "收到参数"+id;
}
/**
* POST请求及传参
* @param id
* @return
*/
@PostMapping("/p")
public String post(@RequestBody String id){
return"收到参数"+id;
}
}
- 进入主启动类进行项目启动
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
注意:
- 此类是项目自动生成的,不需要编写
- 必须在与启动类同一级目录下进行项目开发,否则无法运行
- POST请求可以用postman工具在bady中传递参数
- POST中参数传递最好用@RequestBody ,比较符合规范。
5.配置文件
1.SpringBoot有两种配置文件
- properties
application.properties
#使用的端口号
server.port=8081
#应用的名称
spring.application.name=first-spring-boot
#配置统一前缀
server.servlet.context-path=/first
- yml:分层级,冒号后面需要空格!空格都需要注意!
application.yml
server:
port: 8082
servlet:
context-path: /first
spring:
application:
name: first-spring-boot
建议使用第一种,yml格式不容易保持。
2.自定义配置
package com.imooc.springboot.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SampleController{
@Value("${shool.sname}")
String name;
@Value("${shool.place}")
String place;
@GetMapping("/s")
public String shcool(){
return "学校名叫"+name+",地点是在"+place;
}
}
application.properties
#自定义配置
shool.sname=家里蹲大学
shool.place=成都
- 静态变量的注入
..........
static String time;
...........
@GetMapping("/static")
public String school(){
return "静态变量"+time;
}
@Value("${time}")
public void setTime(String time) {
SampleController.time = time;
}
注意:要通过set方法进行设置参数,并在配置文件中书写对应的数据。
6.Servic和Dao编写(学生信息查询案例)
1.引入依赖
<!--数据库相关依赖--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency><!--链接数据可的依赖--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
2.数据库的配置
-
数据库的数据准备
-
application.properties
spring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&Unicode=true&characterEncoding=utf-8&useSSL=truespring.datasource.username=rootspring.datasource.password=123456
3.MVC模式开发
- 创建实体类
package com.imooc.springboot.entity;public class Strudent { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Strudent{" + "id=" + id + ", name='" + name + '\'' + '}'; }}
1.模型层Model
- 创建Mapper接口
package com.imooc.springboot.mapper;import com.imooc.springboot.entity.Strudent;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Select;import javax.annotation.Resource;@Mapper@Resourcepublic interface FirstMapper { @Select("select*from student where id = #{id}") Strudent findById(long id );}
@Resource:数据资源,能够进行管理
@Select:用来书写SQL语句
- Service层
package com.imooc.springboot.service;import com.imooc.springboot.entity.Strudent;import com.imooc.springboot.mapper.FirstMapper;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;/** * 查询信息的Service */@Servicepublic class FirstService {@Autowired FirstMapper firstMapper; public Strudent getStu(Integer id){ return firstMapper.findById(id); }}
@Autowired:自动进行装配,实现对象实例化
2.控制层Controller
package com.imooc.springboot.controller;import com.imooc.springboot.service.FirstService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class FirstController { @Autowired FirstService firstService; @GetMapping("/student")public String firstStudent(@RequestParam Integer id){ return firstService.getStu(id).toString(); }}
3.视图层View
输入对应URL进行参数传递