目录
选择各个参数并生成压缩包

然后导入即可

新建项目->选择Spring Initializr->next

配置各个参数

选择依赖和版本

修改项目名称之后确定即可
(但是由于某些原因,无法访问到官网)

Spring Boot的基础结构共三个文件:入口、配置文件、测试入口
其中生成的Application和ApplicationTests类都可以直接运行来启动当前创建的项目

在pom.xml文件中修改SpringBoot版本并reimport

新建Controller
@RestController:具有Restful能力的Controller
Get请求方式,地址为first

运行成功

@RequestParam:从请求参数中找到参数并绑定
启动:(请求时携带参数)

@PathVariable:从url当中寻找参数并绑定
要将参数放在url当中,就要在其中体现出来,需要加{},在大括号里面添加参数名

启动:

在此类前面统一添加

启动

在参数中写入类似数组,这样就可以在同一个方法当中匹配两个不同的url上去

启动


required=false:参数不是必须传
defaultValue:传入默认参数(但是传入的必须是字符串)

WEB三层结构:
Controller层:对外暴露接口
Service层:在复杂业务层场景下对业务逻辑进行抽象和封装,保持Controller层简洁和独立,抽象出来的service可以被多个controller重复调用,相当于实现了代码复用,具体的业务代码写在service层,而controller写一些逻辑判断。
DAO层:主要写和数据相关的

两者格式相互转换:在线yaml转properties
配置文件


新建配置类,并且配置好属性和相应的getter和setter方法

然后可以直接使用这个配置类

成功读取:

学生信息查询案例
建表:

引入mybatis和mysql连接

配置文件中配置数据库
- spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
- spring.datasource.username=root
- spring.datasource.password=123456
- spring.datasource.url=jdbc:mysql://localhost:3306/zqf?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
Student实体:
- package com.imooc.springbootlearn;
-
- public class Student {
- Integer id;
- String name;
-
- public Integer getId() {
- return id;
- }
-
- public void setId(Integer id) {
- this.id = id;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- @Override
- public String toString() {
- return "Student{" +
- "id=" + id +
- ", name='" + name + '\'' +
- '}';
- }
- }
DAO:
- package com.imooc.springbootlearn;
-
- import org.apache.ibatis.annotations.Mapper;
- import org.apache.ibatis.annotations.Select;
- import org.springframework.stereotype.Repository;
-
- @Mapper
- @Repository
- public interface StudentMapper {
- @Select("select * from student where id = #{id}")
- Student findById(Integer id);
- }
Service:
- package com.imooc.springbootlearn;
-
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- //调用mapper来操作数据库
- @Service
- public class StudentService {
- @Autowired
- StudentMapper studentMapper;
-
- public Student findStudent(Integer id){
- return studentMapper.findById(id);
- }
- }
Controller:
- package com.imooc.springbootlearn;
-
- 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;
-
- @RestController
- public class StudentController {
-
- @Autowired
- StudentService studentService;
-
- @GetMapping("/student")
- public String student(@RequestParam Integer num){
- Student student = studentService.findStudent(num);
- return student.toString();
- }
-
-
- }
运行:
