zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    sp_executesql 带输出参数的 EXEC 动态执行 (z)
    查端口是否被占用 DOS 命令netstat(Z)
    MS SQL处理树型结构数据 将子节点记录的各项字段值累加赋值给其父节点
    lazarus unidac使用注意事项
    UNIDAC在arm linux运行出错的解决方法
    [转]为Linux下的Lazarus添加中文输入支持
    银河麒麟(aarch64)安装Lazarus步骤
    raspberry 添加拼音五笔输入法
    lazarus跨平台编译步骤
    拷贝构造函数与赋值操作符的具体实现 【转】参考度4.5星
  • 原文地址:https://www.cnblogs.com/viewts/p/13218962.html
Copyright © 2011-2022 走看看