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连接

 配置文件中配置数据库

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();
    }


}

运行:

  • 8
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值