• springboot快速开发web CRUD


    本文大概500字,阅读大概需要5分钟

    **

    关于Springboot的一些配置。

    一、springboot快速开发web

    可以参考这篇文章,进行快速初始化springboot web。

    1.1、SpringMVC快速使用

    java文件夹下的com.example.demo创建entity文件夹,并且创建User类,写入:

    package com.example.demo.entity;
    
    public class User {
        private Integer id;
        private String username;
        private String address;
    
        public Integer getId() { return id; }
    
        public void setId(Integer id) { this.id = id; }
    
        public String getUsername() { return username; }
    
        public void setUsername(String username) { this.username = username; }
    
        public String getAddress() { return address; }
    
        public void setAddress(String address) { this.address = address; }
    
        @Override
        public String toString() {
            return "User{" +
                    "id=" + id +
                    ", username='" + username + '\'' +
                    ", address='" + address + '\'' +
                    '}';
        }
    
        public User(Integer id, String username, String address) {
            this.id = id;
            this.username = username;
            this.address = address;
        }
    
        public User() {
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    我们先用map来映射查询关系,后续将改成MyBatis整合
    java文件夹下的com.example.demo创建service文件夹,并且创建UserService类,写入:

    package com.example.demo.service;
    import com.example.demo.entity.User;
    import org.springframework.stereotype.Component;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    @Component
    public class UserService {
        private static Map<Integer, User> users = new HashMap<>();
    
        static {
            users.put(1, new User(1, "Jack", "Beijing"));
            users.put(2, new User(2, "Mary", "Beijing"));
            users.put(3, new User(3, "Jane", "Shanghai"));
            users.put(4, new User(4, "Michael", "Shanghai"));
        }
    
        /**
         * 根据id查询用户
         * @param id
         * @return
         */
        public User getUserById(Integer id) {
            return users.get(id);
        }
    
        /**
         * 根据id查询所有用户
         * @return
         */
        public List<User> getAllUser() {
            return new ArrayList(users.values());
        }
    
        /**
         * 更新用户
         * @param user
         * @return
         */
        public void update(User user) {
             users.replace(user.getId(), user);
        }
    
        /**
         * 新增用户
         * @param user
         * @return
         */
        public void add(User user) {
            Integer newId = users.size()+1;
            user.setId(newId);
            users.put(newId, user);
        }
    
        /**
         * 删除用户
         * @param user
         * @return
         */
        public void delete(Integer id) {
            users.keySet().removeIf(key -> key == id);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67

    java文件夹下的com.example.demo创建controller文件夹,并且创建UserController类,写入:

    package com.example.demo.controllers;
    import com.example.demo.entity.Result;
    import com.example.demo.entity.User;
    import com.example.demo.service.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;
    
    @RestController
    @RequestMapping("/user")
    public class UserController {
        // Rest /user/1
        @Autowired
        UserService userService;
    
        // 查询 /user/1
        @GetMapping("/{id}")
        public Result getUser(@PathVariable Integer id) {
            System.out.println(id);
            User user = userService.getUserById(id);
            return new Result<>(200, "查询成功", user);
        }
    
        // 新增 /user/add
        @PostMapping("/add")
        public Result addUser(@RequestBody User user) {
            userService.add(user);
            return new Result<>(200, "添加成功");
        }
    
        // 修改 /user/1
        @PutMapping("/{id}")
        public Result editUser(@RequestBody User user) {
            userService.update(user);
            return new Result<>(200, "修改成功");
        }
    
        // 删除 /user/1
        @DeleteMapping("/{id}")
        public Result deleteUser(@PathVariable Integer id) {
            userService.delete(id);
            return new Result<>(200, "删除成功");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    测试效果:
    在这里插入图片描述

    在学习springboot的路上,如果你觉得本文对你有所帮助的话,那就请关注点赞评论三连吧,谢谢,你的肯定是我写博的另一个支持。

  • 相关阅读:
    编写云计算计费系统
    如何保证分布式情况下的幂等性
    【mmWave】一、IWR6843ISK-ODS毫米波雷达【固件烧写和上手使用】流程
    nvidia-docker安装
    LDAP协议工作原理
    YOLO V8训练自己的数据集并测试
    使用Hadoop MapReduce分析邮件日志提取 id、状态 和 目标邮箱
    ovs vxlan实现代码分析
    Vue2 如何优雅的解决v-for和v-if同时出现
    深入理解数据结构(1)—用链表实现栈
  • 原文地址:https://blog.csdn.net/weixin_44103733/article/details/126747201