首页 > 编程语言 > 详细

Spring Boot基础

时间:2020-07-01 14:16:37      阅读:57      评论:0      收藏:0      [点我收藏+]

一、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

Spring Boot基础

原文:https://www.cnblogs.com/viewts/p/13218962.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!