• 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

  • 相关阅读:
    EXCEL中用VLOOKUP功能,根据A列的值,把B列也填充上对应的值
    ReNamer批量重命名文件,如何给杂乱无章的文件名重新命名
    小米手机亲情守护(风筝守护)怎么解绑?
    PHP正则表达式遇到的一个utf8乱码坑
    筹米网你用过没?是套路还是真能帮你提前抢购域名?
    CSS选取第一个、最后一个、偶数、奇数、第n个标签元素
    winscp会话超时及尝试关闭优化连接缓冲大小
    Linux下压缩和解压
    一步一步学Linux下vi/vim的使用(案例比纯理论好学)
    Linux使用find命令,搜索文件名中带有通配符*,报错: paths must precede expression
  • 原文地址:https://www.cnblogs.com/viewts/p/13218962.html
Copyright © 2020-2023  润新知