Spring Boot基础

一、Spring Boot 介绍

Spring Boot 是一个快速开发框架。它不仅继承了Spring框架原有的优秀特性,而且还通过简化配置来进一步简化了Spring应用的整个搭建和开发过程。

二、如何使用

1.新建Maven工程,导入依赖:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.1.RELEASE</version>
</parent>

首先导入父包,接下来导入它的子包:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

2.创建实体类:

package com.wts.entity;

import lombok.Data;

@Data
public class Student {
    private long id;
    private String name;
}

3.创建实例接口:

public interface StudentRepository {
    public Collection<Student> findAll();
    public Student findById(long id);
    public int insertStudent(Student student);
    public int updateStudent(Student student);
    public int deleteStudent(long id);
}

为了简单起见,创建一个实现类来取代数据库查询:

public class StudentRepositoryImpl implements StudentRepository {

    private static Map<Long, Student> studentMap;

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

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

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

    @Override
    public int insertStudent(Student student) {
        studentMap.put(student.getId(), student);
        return 1;
    }

    @Override
    public int updateStudent(Student student) {
        return insertStudent(student);
    }

    @Override
    public int deleteStudent(long id) {
        studentMap.remove(id);
        return 1;
    }
}

4.创建控制器:

 

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

    @Autowired
    private StudentRepository studentRepository;

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

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

    @PostMapping("/insertStudent")
    public int insertStudent(@RequestBody Student student) {
        return studentRepository.insertStudent(student);
    }

    @PutMapping("/updateStudent")
    public int updateStudent(@RequestBody Student student) {
        return studentRepository.updateStudent(student);
    }

    @DeleteMapping("/deleteStudent/{id}")
    public int deleteStudent(@PathVariable("id") long id) {
        return studentRepository.deleteStudent(id);
    }
}

到这里,需要注意前面的repository实现类需要添加一个@Repository注解。

5.创建启动类:

 

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

6.运行,浏览器访问http://localhost:8080/student/findAll

posted @ 2020-07-01 14:08  viewts  阅读(175)  评论(0编辑  收藏  举报