springboot学习1 —— 快速搭建

从b站学习springcloud,现在进行总结,该总结除去了视频中出现的小错误,对有些易错的地方进行了提醒
b站链接:https://www.bilibili.com/video/av55993157
资料链接:
https://pan.baidu.com/s/1o0Aju3IydKA15Vo1pP4z5w
提取码: 21ru

下一节链接:https://blog.csdn.net/qq_40893824/article/details/104656473

下面的内容总结:
创建工程→父pom文件→实体类→接口及实现→启动类→检测→配置文件

实现细节:
1.创建maven工程:


点击next

填入1,2中的名字,点击3next

点击Finish

2.界面:

右下角:

点击Enable Auto-Import自动导入

3.填入父pom文件代码

代码:

    <!-- 继承父包 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.7.RELEASE</version>
    </parent>

    <dependencies>
        <!-- web启动jar -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

4.实体类Student

4.1 pom
在刚刚父pom代码中添加:

代码:

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.8</version>
            <scope>provided</scope>
        </dependency>

4.2创包


填入com.southwind.entity

4.3在包entity新建实体类Student



填入代码:

package com.southwind.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
    private long id;
    private String name;
    private int age;
}

5.在包southwind中创包repository


6.在包repository中创建接口StudentRepository.java



代码:

package com.southwind.repository;

import com.southwind.entity.Student;

import java.util.Collection;

public interface StudentRepository {

    public void saveorupdate(Student student);
    public void deleteById(long id);
    public Collection<Student> findAll();
    public Student findById(long id);

}

7.在包repository中创包impl


7.1在包impl中新建实现类StudentRepositoryImpl

要实现接口的操作要implements StudentRepository
鼠标移至StudentRepositoryImpl,alt+enter后,神奇的一幕:
在这里插入图片描述
进入箭头:

自动填充代码:

代码:

package com.southwind.repository.impl;

import com.southwind.entity.Student;
import com.southwind.repository.StudentRepository;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

@Repository /* 对应 StudentHandler 中的 @Autowired*/
public class StudentRepositoryImpl implements StudentRepository{

    private static Map<Long,Student> studentMap;

    static {
        studentMap = new HashMap<>();
        studentMap.put(1L,new Student(1L,"张三",12));
        studentMap.put(2L,new Student(2L,"李四",22));
        studentMap.put(3L,new Student(3L,"王五",15));
    }

    @Override
    public void saveorupdate(Student student) {
        studentMap.put(student.getId(),student);
    }

    @Override
    public void deleteById(long id) {
        studentMap.remove(id);
    }

    @Override
    public Collection<Student> findAll() {
        return studentMap.values();
    }

    @Override
    public Student findById(long id) {
        return studentMap.get(id);
    }
}

8.在southwind中创包controller
在controller中创建控制类StudentHandler
代码:

package com.southwind.controller;

import com.southwind.entity.Student;
import com.southwind.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

@RestController
@RequestMapping("/student")
public class StudentHandler {

    @Autowired/* StudentRepositoryImpl 中的 @Repository */
    private StudentRepository studentRepository;

    @PostMapping("/save")
    public void save(@RequestBody Student student){
        studentRepository.saveorupdate(student);
    }

    @PutMapping("/update")
    public void update(@RequestBody Student student){
        studentRepository.saveorupdate(student);
    }

    @DeleteMapping("/deleteById/{id}")
    public void deleteById(@PathVariable("id")long id){
        studentRepository.deleteById(id);
    }

    @GetMapping("/findAll")
    public Collection<Student> findAll(){
        return studentRepository.findAll();
    }

    @GetMapping("/findById/{id}")
    public Student findById(@PathVariable("id")long id){
        return studentRepository.findById(id);
    }
}

9.在southwind中新建启动类Application
代码:

package com.southwind;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
/*
@SpringBootApplication 表示当前类是 Spring Boot 的入口
Application 类的存放位置必须是其他相关业务类的存放位置的父级
* */
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

10.检查
启动:



端口是8080

10.1 进入 http://localhost:8080/student/findAll

进入postman
10.2 get → http://localhost:8080/student/findAll

10.3 插入
post → http://localhost:8080/student/save
再 get → http://localhost:8080/student/findAll

10.4修改名字

再 get → http://localhost:8080/student/findAll

10.5 查找
get → http://localhost:8080/student/findById/4

10.6删除
delete → http://localhost:8080/student/deleteById/4
再 get → http://localhost:8080/student/findAll

11.在resources中新建配置文件application.yml


代码:

server:
  port: 9090

重启Application.java

端口就变成了9090了
检查:


Application可以扫描controller、entity、repository中子文件,逻辑上Application的级别别子文件高,这样才能扫描比自己级别低的文件。

下一节链接:https://blog.csdn.net/qq_40893824/article/details/104656473

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_1403034144

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值