跳到主要内容 Spring Boot 实现后端 Bot 管理系统的 CRUD 操作 | 极客日志
Java AI java
Spring Boot 实现后端 Bot 管理系统的 CRUD 操作 本文介绍使用 Spring Boot 框架实现后端 API 以处理 Bot 数据的增删改查。内容包括创建项目、编写 Service 和 Controller 接口、实现数据库交互及用户权限校验。通过具体代码示例展示了如何接收前端请求、验证数据合法性并返回结果,同时包含前端测试代码,帮助开发者快速构建基于 Spring Security 认证的 Bot 管理系统。
不知所云 发布于 2026/3/22 更新于 2026/4/16 2 浏览前言
本文通过实际操作,演示如何使用 Spring Boot 框架实现后端 API,处理 Bot 数据的增删改查(CRUD)请求。内容涵盖创建项目、编写 API 接口、实现数据库交互等方面,最终构建一个功能完整的 Bot 管理系统。
实现后端 API
增加一个 Bot
在 service/user/bot 新建接口文件 AddService:
package org.example.backend.service.user.bot;
import java.util.Map;
public interface AddService {
Map<String, String> add (Map<String, String> data) ;
}
在 service/impl/user/bot 新建实现类 AddServiceImpl:
package org.example.backend.service.impl.user.bot;
import org.example.backend.mapper.BotMapper;
import org.example.backend.pojo.Bot;
import org.example.backend.pojo.User;
import org.example.backend.service.impl.utils.UserDetailsImpl;
import org.example.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) {
UsernamePasswordAuthenticationToken (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
(UserDetailsImpl) authenticationToken.getPrincipal();
loginUser.getUser();
data.get( );
data.get( );
data.get( );
Map<String, String> map = <>();
(title == || title.length() == ) {
map.put( , );
map;
}
(title.length() > ) {
map.put( , );
map;
}
(description == || description.length() == ) {
description = ;
}
(description != && description.length() > ) {
map.put( , );
map;
}
(content == || content.length() == ) {
map.put( , );
map;
}
(content.length() > ) {
map.put( , );
map;
}
();
( , user.getId(), title, description, content, , now, now);
botMapper.insert(bot);
map.put( , );
map;
}
}
微信扫一扫,关注极客日志 微信公众号「极客日志」,在微信中扫描左侧二维码关注。展示文案:极客日志 zeeklog
相关免费在线工具 Keycode 信息 查找任何按下的键的javascript键代码、代码、位置和修饰符。 在线工具,Keycode 信息在线工具,online
Escape 与 Native 编解码 JavaScript 字符串转义/反转义;Java 风格 \uXXXX(Native2Ascii)编码与解码。 在线工具,Escape 与 Native 编解码在线工具,online
JavaScript / HTML 格式化 使用 Prettier 在浏览器内格式化 JavaScript 或 HTML 片段。 在线工具,JavaScript / HTML 格式化在线工具,online
JavaScript 压缩与混淆 Terser 压缩、变量名混淆,或 javascript-obfuscator 高强度混淆(体积会增大)。 在线工具,JavaScript 压缩与混淆在线工具,online
RSA密钥对生成器 生成新的随机RSA私钥和公钥pem证书。 在线工具,RSA密钥对生成器在线工具,online
Mermaid 预览与可视化编辑 基于 Mermaid.js 实时预览流程图、时序图等图表,支持源码编辑与即时渲染。 在线工具,Mermaid 预览与可视化编辑在线工具,online
authenticationToken
=
UserDetailsImpl
loginUser
=
User
user
=
String
title
=
"title"
String
description
=
"description"
String
content
=
"content"
new
HashMap
if
null
0
"error_message"
"标题不能为空"
return
if
100
"error_message"
"标题长度不能大于 100"
return
if
null
0
"这个用户很懒,什么也没留下~"
if
null
300
"error_message"
"Bot 的描述长度不能大于 300"
return
if
null
0
"error_message"
"代码不能为空"
return
if
10000
"error_message"
"代码长度不能超过 10000"
return
Date
now
=
new
Date
Bot
bot
=
new
Bot
null
1500
"error_message"
"success"
return
获取当前登录用户信息:通过 SecurityContextHolder 获取认证信息并提取 User 对象。
接收前端数据并验证:检查标题、描述、内容的非空及长度限制。
创建 Bot 对象:设置默认值(如 rating 为 1500),记录创建和更新时间。
插入数据库:调用 Mapper 层保存数据。
在 controller/user/bot 新建 Controller 类 AddController:
package org.example.backend.controller.user.bot;
import org.example.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);
}
}
增加一个 Bot 测试 在前端 web 项目下 kob/web/src/views/user/bot 的文件 UserBotIndexView.vue 中编写测试:
<template>
<ContentField> 我的 Bot </ContentField>
</template>
<script>
import ContentField from'../../../components/ContentField'
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); }
});
}
}
</script>
<style scoped></style>
使用 jQuery 发送 POST 请求,携带 Bearer Token 进行身份验证。
删除一个 Bot 在 service/user/bot 新建接口文件 RemoveService:
package org.example.backend.service.user.bot;
import java.util.Map;
public interface RemoveService {
Map<String, String> remove (Map<String, String> data) ;
}
在 service/impl/user/bot 新建实现类 RemoveServiceImpl:
package org.example.backend.service.impl.user.bot;
import org.example.backend.mapper.BotMapper;
import org.example.backend.pojo.Bot;
import org.example.backend.pojo.User;
import org.example.backend.service.impl.utils.UserDetailsImpl;
import org.example.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();
int bot_id = Integer.parseInt(data.get("bot_id" ));
Bot bot = botMapper.selectById(bot_id);
Map<String, String> map = new HashMap <>();
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;
}
}
获取 bot_id 并查询数据库。
验证 Bot 是否存在。
验证当前用户是否为 Bot 拥有者。
执行删除操作。
在 controller/user/bot 新建 Controller 类 RemoveController:
package org.example.backend.controller.user.bot;
import org.example.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);
}
}
删除一个 Bot 测试 在前端 web 项目下 kob/web/src/views/user/bot 的文件 UserBotIndexView.vue 中编写测试:
<template>
<ContentField> 我的 Bot </ContentField>
</template>
<script>
import ContentField from'../../../components/ContentField'
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/remove/",
type: "POST",
data: {
bot_id: 1,
},
headers: {
Authorization: "Bearer " + store.state.user.token,
},
success(resp) { console.log(resp); },
error(resp) { console.log(resp); }
});
}
}
</script>
<style scoped></style>
修改一个 Bot 在 service/user/bot 新建接口文件 UpdateService:
package org.example.backend.service.user.bot;
import java.util.Map;
public interface UpdateService {
Map<String, String> update (Map<String, String> data) ;
}
在 service/impl/user/bot 新建实现类 UpdateServiceImpl:
package org.example.backend.service.impl.user.bot;
import org.example.backend.mapper.BotMapper;
import org.example.backend.pojo.Bot;
import org.example.backend.pojo.User;
import org.example.backend.service.impl.utils.UserDetailsImpl;
import org.example.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 != null && 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;
}
Bot new_bot = new Bot (bot.getId(), user.getId(), title, description, content, bot.getRating(), bot.getCreatetime(), new Date ());
botMapper.updateById(new_bot);
map.put("error_message" , "success" );
return map;
}
}
在 controller/user/bot 新建 Controller 类 UpdateController:
package org.example.backend.controller.user.bot;
import org.example.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> map) {
return updateService.update(map);
}
}
修改一个 Bot 测试 在前端 web 项目下 kob/web/src/views/user/bot 的文件 UserBotIndexView.vue 中编写测试:
<template>
<ContentField> 我的 Bot </ContentField>
</template>
<script>
import ContentField from'../../../components/ContentField'
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/update/",
type: "POST",
data: {
bot_id: 5,
title: "我是 Bot_5 的标题",
description: "我是 Bot_5 的描述",
content: "我是 Bot_5 的代码",
},
headers: {
Authorization: "Bearer " + store.state.user.token,
},
success(resp) { console.log(resp); },
error(resp) { console.log(resp); }
});
}
}
</script>
<style scoped></style>
查询 Bot 列表 在 service/user/bot 新建接口文件 GetListService:
package org.example.backend.service.user.bot;
import org.example.backend.pojo.Bot;
import java.util.List;
public interface GetListSevice {
List<Bot> getList () ;
}
在 service/impl/user/bot 新建实现类 GetListServiceImpl:
package org.example.backend.service.impl.user.bot;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.backend.mapper.BotMapper;
import org.example.backend.pojo.Bot;
import org.example.backend.pojo.User;
import org.example.backend.service.impl.utils.UserDetailsImpl;
import org.example.backend.service.user.bot.GetListSevice;
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.Collections;
import java.util.List;
@Service
public class GetListServiceImpl implements GetListSevice {
@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());
return botMapper.selectList(queryWrapper);
}
}
在 controller/user/bot 新建 Controller 类 GetListController:
package org.example.backend.controller.user.bot;
import org.example.backend.pojo.Bot;
import org.example.backend.service.user.bot.GetListSevice;
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 GetListSevice getListSevice;
@GetMapping("user/bot/getlist")
public List<Bot> getList () {
return getListSevice.getList();
}
}
查询 Bot 列表测试 在前端 web 项目下 kob/web/src/views/user/bot 的文件 UserBotIndexView.vue 中编写测试:
<template>
<ContentField> 我的 Bot </ContentField>
</template>
<script>
import ContentField from'../../../components/ContentField'
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/getlist/",
type: "get",
headers: {
Authorization: "Bearer " + store.state.user.token,
},
success(resp) { console.log(resp); },
error(resp) { console.log(resp); }
});
}
}
</script>
<style scoped></style>
总结 希望本文能帮助你更好地理解后端 API 开发的基本流程,为今后在实际项目中实现更复杂的功能打下基础。