• Spring Boot 5 创建个人中心页面(API+Vue)


    一.注意点

    1.为什么后端要验证该Bot是否属于该用户?
    答:如果不认证,前端就不需要的登录就能修改后端数据库了,添加验证,防止破坏数据库。
    2.什么时候前端用headers?
    答:访问后端没有开放权限的页面时
    3.PostMapping&&GetMapping
    答:PostMapping涉及改动数据库
    GetMapping不改动数据库

    二.API

    1.创建表(略)

    2.创建pojo&&mapper

    package com.kob.backend.pojo;
    
    import com.baomidou.mybatisplus.annotation.IdType;
    import com.baomidou.mybatisplus.annotation.TableId;
    import com.fasterxml.jackson.annotation.JsonFormat;
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import java.util.Date;
    
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class Bot {
        @TableId(type = IdType.AUTO)
        private Integer id;
        private Integer userId;
        private String title;
        private String description;
        private String content;
        private Integer rating;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        private Date createtime;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        private Date modifytime;
    }
    
    
    • 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
    
    package com.kob.backend.mapper;
    
    import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    import com.kob.backend.pojo.Bot;
    import org.apache.ibatis.annotations.Mapper;
    
    
    @Mapper
    public interface BotMapper extends BaseMapper<Bot> {
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    3.创建接口(四个)[AddService,GetListService,RemoveService,UpdateService]

    package com.kob.backend.service.user.bot;
    
    import org.springframework.stereotype.Service;
    
    import java.util.Map;
    
    
    public interface AddService {
        public Map<String ,String> add(Map<String,String> data);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    package com.kob.backend.service.user.bot;
    
    import com.kob.backend.pojo.Bot;
    import java.util.List;
    
    public interface GetListService {
        public List<Bot> getList();
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    package com.kob.backend.service.user.bot;
    
    import java.util.Map;
    
    public interface RemoveService {
        public Map<String,String> remove(Map<String,String> data);
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    package com.kob.backend.service.user.bot;
    
    import java.util.Map;
    
    public interface UpdateService {
        public Map<String,String> update(Map<String,String> data);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    4.实现接口(四个))[AddServiceImpl,GetListServiceImpl,RemoveServiceImpl,UpdateServiceImpl]

    package com.kob.backend.service.impl.bot;
    
    import com.kob.backend.mapper.BotMapper;
    import com.kob.backend.pojo.Bot;
    import com.kob.backend.pojo.User;
    import com.kob.backend.service.impl.utils.UserDetailsImpl;
    import com.kob.backend.service.user.bot.AddService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
    import org.springframework.security.core.context.SecurityContextHolder;
    import org.springframework.stereotype.Service;
    
    import java.util.Date;
    import java.util.HashMap;
    import java.util.Map;
    
    
    @Service
    public class AddServiceImpl implements AddService {
    
        @Autowired
        private BotMapper botMapper;
    
        @Override
        public Map<String, String> add(Map<String, String> data) {
            //认证+获取user
            UsernamePasswordAuthenticationToken authenticationToken =
                    (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
            UserDetailsImpl loginUser = (UserDetailsImpl) authenticationToken.getPrincipal();
            User user = loginUser.getUser();
    
            //传入参数
            String title = data.get("title");
            String description = data.get("description");
            String content = data.get("content");
    
            //定义返回
            Map<String,String> map = new HashMap<>();
    
            if(title == null || title.length() == 0){
                map.put("error_message","标题不能为空");
                return map;
            }
            if(title.length() > 100){
                map.put("error_message","标题长度不能大于100");
                return map;
            }
            if(description == null || description.length() == 0){
                description = "这个用户很懒,没有添加描述~~";
            }
            if(description.length() > 300){
                map.put("error_message","Bot描述不能大于300");
                return map;
            }
            if(content == null || content.length() == 0){
                map.put("error_message","代码不能为空");
                return map;
            }
            if(content.length() > 10000){
                map.put("error_message","代码长度不能超过10000");
                return map;
            }
    
            Date now = new Date();
            Bot bot = new Bot(null,user.getId(),title,description,content,1500,now,now);
            botMapper.insert(bot);
    
            map.put("error_message","success");
            return map;
        }
    }
    
    
    
    • 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
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    package com.kob.backend.service.impl.bot;
    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import com.kob.backend.mapper.BotMapper;
    import com.kob.backend.pojo.Bot;
    import com.kob.backend.pojo.User;
    import com.kob.backend.service.impl.utils.UserDetailsImpl;
    import com.kob.backend.service.user.bot.GetListService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
    import org.springframework.security.core.context.SecurityContextHolder;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    
    @Service
    public class GetListServiceImpl implements GetListService {
        @Autowired
        private BotMapper botMapper;
    
        @Override
        public List<Bot> getList()
        {
            UsernamePasswordAuthenticationToken authenticationToken =
                    (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
            UserDetailsImpl loginUser = (UserDetailsImpl) authenticationToken.getPrincipal();
            User user = loginUser.getUser();
    
            QueryWrapper<Bot> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id",user.getId());
    
            List<Bot> bots = botMapper.selectList(queryWrapper);
    
            return bots;
        }
    }
    
    
    
    • 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
    
    package com.kob.backend.service.impl.bot;
    
    import com.kob.backend.mapper.BotMapper;
    import com.kob.backend.pojo.Bot;
    import com.kob.backend.pojo.User;
    import com.kob.backend.service.impl.utils.UserDetailsImpl;
    import com.kob.backend.service.user.bot.RemoveService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
    import org.springframework.security.core.context.SecurityContextHolder;
    import org.springframework.stereotype.Service;
    
    import java.util.HashMap;
    import java.util.Map;
    
    @Service
    public class RemoveServiceImpl implements RemoveService {
    
        @Autowired
        private BotMapper botMapper;
    
        @Override
        public Map<String, String> remove(Map<String, String> data) {
            //认证+获取用户
            UsernamePasswordAuthenticationToken authenticationToken =
                    (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
            UserDetailsImpl loginUser = (UserDetailsImpl)authenticationToken.getPrincipal();
            User user = loginUser.getUser();
    
            Map<String,String> map = new HashMap<>();
            int bot_id = Integer.parseInt(data.get("bot_id"));
            Bot bot = botMapper.selectById(bot_id);
    
            if(bot == null){
                map.put("error_message","Bot不存在或已删除");
                return map;
            }
            if(!bot.getUserId().equals(user.getId())){
                map.put("error_message","没有权限删除该Bot");
                return map;
            }
            botMapper.deleteById(bot_id);
    
            map.put("error_message","success");
    
            return map;
        }
    }
    
    
    • 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
    package com.kob.backend.service.impl.bot;
    
    import com.kob.backend.mapper.BotMapper;
    import com.kob.backend.pojo.Bot;
    import com.kob.backend.pojo.User;
    import com.kob.backend.service.impl.utils.UserDetailsImpl;
    import com.kob.backend.service.user.bot.UpdateService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
    import org.springframework.security.core.context.SecurityContextHolder;
    import org.springframework.stereotype.Service;
    
    import java.util.Date;
    import java.util.HashMap;
    import java.util.Map;
    
    @Service
    public class UpdateServiceImpl implements UpdateService {
    
        @Autowired
        private BotMapper botMapper;
    
        @Override
        public Map<String, String> update(Map<String, String> data) {
            UsernamePasswordAuthenticationToken authenticationToken =
                    (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
            UserDetailsImpl loginUser = (UserDetailsImpl) authenticationToken.getPrincipal();
            User user = loginUser.getUser();
    
            int bot_id = Integer.parseInt(data.get("bot_id"));
    
            //传入参数
            String title = data.get("title");
            String description = data.get("description");
            String content = data.get("content");
    
            //定义返回
            Map<String,String> map = new HashMap<>();
    
            if(title == null || title.length() == 0){
                map.put("error_message","标题不能为空");
                return map;
            }
            if(title.length() > 100){
                map.put("error_message","标题长度不能大于100");
                return map;
            }
            if(description == null || description.length() == 0){
                description = "这个用户很懒,没有添加描述~~";
            }
            if(description.length() > 300){
                map.put("error_message","Bot描述不能大于300");
                return map;
            }
            if(content == null || content.length() == 0){
                map.put("error_message","代码不能为空");
                return map;
            }
            if(content.length() > 10000){
                map.put("error_message","代码长度不能超过10000");
                return map;
            }
    
    
            Bot bot = botMapper.selectById(bot_id);
            if(bot == null){
                map.put("error_message","Bot不存在或已删除");
                return map;
            }
            if(!bot.getUserId().equals(user.getId())){
                map.put("error_message","没有权限修改该Bot");
                return map;
            }
    
    
            Date now = new Date();
            Bot new_bot = new Bot(
                    bot.getId(),
                    user.getId(),
                    title,
                    description,
                    content,
                    bot.getRating(),
                    bot.getCreatetime(),
                    now
            );
            botMapper.updateById(new_bot);
    
            map.put("error_message","success");
            return map;
        }
    }
    
    
    
    • 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
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94

    5.四个Controller

    
    package com.kob.backend.controller.user.bot;
    
    import com.kob.backend.service.user.bot.AddService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.Map;
    
    @RestController
    public class AddController {
    
        @Autowired
        private AddService addService;
    
        @PostMapping("/user/bot/add/")
        public Map<String ,String> add(@RequestParam Map<String,String> data)
        {
            return addService.add(data);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    package com.kob.backend.controller.user.bot;
    
    import com.kob.backend.pojo.Bot;
    import com.kob.backend.service.user.bot.GetListService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.List;
    
    @RestController
    public class GetListController{
        @Autowired
        private GetListService getListService;
    
        @GetMapping("/user/bot/getlist/")
        public List<Bot> getList()
        {
            return getListService.getList();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    package com.kob.backend.controller.user.bot;
    
    import com.kob.backend.service.user.bot.RemoveService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.Map;
    
    @RestController
    public class RemoveController {
        @Autowired
        private RemoveService removeService;
    
        @PostMapping("/user/bot/remove/")
        public Map<String,String> remove(@RequestParam  Map<String,String> data)
        {
            return removeService.remove(data);
        }
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    
    package com.kob.backend.controller.user.bot;
    
    import com.kob.backend.service.user.bot.UpdateService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.Map;
    
    @RestController
    public class UpdateController {
        @Autowired
        private UpdateService updateService;
    
        @PostMapping("/user/bot/update/")
        public Map<String,String> update(@RequestParam Map<String,String> data){
            return updateService.update(data);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    6.前端调试

    UserBotIndexView.vue
    
    ...
    
    <script>
    import ContentField from '../../../components/ContentField.vue';
    import $ from 'jquery';
    import { useStore } from 'vuex';
    
    export default {
        components: {
            ContentField,
        },
        setup() {
            const store = useStore();
            // $.ajax({
            //     url: "http://127.0.0.1:3000/user/bot/add/",
            //     type: "POST",
            //     data: {
            //         title: "Bot的标题",
            //         description: "Bot的描述",
            //         content: "Bot的代码",
            //     },
            //     headers: {
            //         Authorization: "Bearer " + store.state.user.token,
            //     },
            //     success(resp) {
            //         console.log(resp);
            //     },
            //     error(resp) {
            //         console.log(resp);
            //     }
            // });
    
            // $.ajax({
            //     url: "http://127.0.0.1:3000/user/bot/remove/",
            //     type: "POST",
            //     data: {
            //         bot_id: 5,
            //     },
            //     headers: {
            //         Authorization: "Bearer " + store.state.user.token,
            //     },
            //     success(resp) {
            //         console.log(resp);
            //     },
            //     error(resp) {
            //         console.log(resp);
            //     }
            // });
    
            // $.ajax({
            //     url: "http://127.0.0.1:3000/user/bot/update/",
            //     type: "POST",
            //     data: {
            //         bot_id: 1,
            //         title: "Bot的标题123",
            //         description: "Bot的描述123",
            //         content: "Bot的代码123",
            //     },
            //     headers: {
            //         Authorization: "Bearer " + store.state.user.token,
            //     },
            //     success(resp) {
            //         console.log(resp);
            //     },
            //     error(resp) {
            //         console.log(resp);
            //     }
            // });
    
            $.ajax({
                url: "http://127.0.0.1:3000/user/bot/getlist/",
                type: "get",
                headers: {
                    Authorization: "Bearer " + store.state.user.token,
                },
                success(resp) {
                    console.log(resp);
                },
                error(resp) {
                    console.log(resp);
                }
            });
        }
    }
    </script>
    
    • 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
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87

    三:前端:实现个人页面(略)

  • 相关阅读:
    [资源推荐]langchain、LLM相关
    Unity ILRuntime热更新开发原则与接口如何绑定
    Python学习之——环境配置
    Java高级应用——异常处理
    jeecg启动微服务并注册到本地nacos
    C++ 嵌套循环
    软件设计师-UML基础教程
    青少年python系列 43.导入类
    [附源码]java毕业设计ssm学生成绩考核管理系统
    17.2、JavaWeb-简介、JDBC的缺点、入门使用、mybatis的使用、条件查询、mybatis的参数传递
  • 原文地址:https://blog.csdn.net/qq_52384627/article/details/126397654