• SpringBoot快速复习


    目录​​​​​​​

    新建SpringBoot项目演示

    通过官网新建项目

    IDEA集成的Spring Initializr

    完成第一个接口开发

    项目结构解析

    接口开发

    简单接口演示

    接收参数的接口

    参数写在url中

    此类添加公共前缀

    GetMapping的多url用法

    默认值传参

    配置文件的两种写法

    配置自定义属性

    @Value注解进行绑定

    利用对象配置

    Service和Dao的编写


    新建SpringBoot项目演示

    通过官网新建项目

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

    然后导入即可

    IDEA集成的Spring Initializr

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

    配置各个参数

    选择依赖和版本 

    修改项目名称之后确定即可

    (但是由于某些原因,无法访问到官网)

    完成第一个接口开发

    项目结构解析

    Spring Boot的基础结构共三个文件:入口、配置文件、测试入口

    其中生成的Application和ApplicationTests类都可以直接运行来启动当前创建的项目

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

    接口开发

    新建Controller

    简单接口演示

    @RestController:具有Restful能力的Controller

    Get请求方式,地址为first

    运行成功

    接收参数的接口

    @RequestParam:从请求参数中找到参数并绑定

    启动:(请求时携带参数)

    参数写在url中

    @PathVariable:从url当中寻找参数并绑定

    要将参数放在url当中,就要在其中体现出来,需要加{},在大括号里面添加参数名

    启动:

    此类添加公共前缀

    在此类前面统一添加

    启动

    GetMapping的多url用法

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

    启动

    默认值传参

    required=false:参数不是必须传

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

    配置文件的两种写法

    WEB三层结构:

    Controller层:对外暴露接口

    Service层:在复杂业务层场景下对业务逻辑进行抽象和封装,保持Controller层简洁和独立,抽象出来的service可以被多个controller重复调用,相当于实现了代码复用,具体的业务代码写在service层,而controller写一些逻辑判断。

    DAO层:主要写和数据相关的

    两者格式相互转换:在线yaml转properties

    配置自定义属性

    @Value注解进行绑定

    配置文件

    利用对象配置

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

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

    成功读取:

    Service和Dao的编写

    学生信息查询案例

    建表:

    引入mybatis和mysql连接

     配置文件中配置数据库

    1. spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    2. spring.datasource.username=root
    3. spring.datasource.password=123456
    4. spring.datasource.url=jdbc:mysql://localhost:3306/zqf?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true

    Student实体:

    1. package com.imooc.springbootlearn;
    2. public class Student {
    3. Integer id;
    4. String name;
    5. public Integer getId() {
    6. return id;
    7. }
    8. public void setId(Integer id) {
    9. this.id = id;
    10. }
    11. public String getName() {
    12. return name;
    13. }
    14. public void setName(String name) {
    15. this.name = name;
    16. }
    17. @Override
    18. public String toString() {
    19. return "Student{" +
    20. "id=" + id +
    21. ", name='" + name + '\'' +
    22. '}';
    23. }
    24. }

    DAO:

    1. package com.imooc.springbootlearn;
    2. import org.apache.ibatis.annotations.Mapper;
    3. import org.apache.ibatis.annotations.Select;
    4. import org.springframework.stereotype.Repository;
    5. @Mapper
    6. @Repository
    7. public interface StudentMapper {
    8. @Select("select * from student where id = #{id}")
    9. Student findById(Integer id);
    10. }

    Service:

    1. package com.imooc.springbootlearn;
    2. import org.springframework.beans.factory.annotation.Autowired;
    3. import org.springframework.stereotype.Service;
    4. //调用mapper来操作数据库
    5. @Service
    6. public class StudentService {
    7. @Autowired
    8. StudentMapper studentMapper;
    9. public Student findStudent(Integer id){
    10. return studentMapper.findById(id);
    11. }
    12. }

    Controller:

    1. package com.imooc.springbootlearn;
    2. import org.springframework.beans.factory.annotation.Autowired;
    3. import org.springframework.web.bind.annotation.GetMapping;
    4. import org.springframework.web.bind.annotation.RequestParam;
    5. import org.springframework.web.bind.annotation.RestController;
    6. @RestController
    7. public class StudentController {
    8. @Autowired
    9. StudentService studentService;
    10. @GetMapping("/student")
    11. public String student(@RequestParam Integer num){
    12. Student student = studentService.findStudent(num);
    13. return student.toString();
    14. }
    15. }

    运行:

  • 相关阅读:
    [羊城杯 2022]LRSA
    MCE | 动物实验溶剂大讨论
    HTTP超文本传输协议详解
    Javascript——处理字符串单引号
    【rust】cargo的概念和使用方法
    Java - SpringBoot整合JWT
    表单中某一项点击添加和删除
    浅谈进销存系统对于文具行业的价值
    低代码相关概念及钉钉宜搭初使用
    VB.net实战(VSTO):Excel插件设计Ribbon界面
  • 原文地址:https://blog.csdn.net/m0_52601969/article/details/127402092