下面是对你提供的论坛系统主要功能的总结,包括功能描述、设计思路、处理流程和核心代码示例。每个功能都是围绕帖子(Post)、评论(Comment)、点赞(LikePost)、收藏(FavoritePost)、举报(ReportPost)等模块展开的。
1. 发布帖子
功能描述
用户发布新的帖子,包含帖子标题和内容。
设计思路
- 前端通过表单输入帖子信息,调用
/forum/post POST 接口,发送帖子数据。
- 后端接收请求体中的
Post 对象,调用业务服务插入数据库。
- 返回成功或失败消息给前端。
处理流程
- 用户填写帖子信息,提交请求。
- 前端向
/forum/post 发送 POST 请求,请求体为 JSON 格式帖子对象。
- 后端控制器接收请求,调用
ForumService.createPost()。
- Service 层调用 Mapper 执行数据库插入。
- 返回插入结果,控制器根据结果构建返回消息。
- 前端显示操作成功或失败提示。
核心代码
// Controller 接口 - 接收新帖子请求
@PostMapping("/post")
public String createPost(@RequestBody Post post) {
int result = forumService.createPost(post);
return result > 0 ? "Post created successfully" : "Post creation failed";
}
// Service 层 - 调用 Mapper 插入
public int createPost(Post post) {
return forumMapper.insertPost(post);
}
// Mapper XML - SQL 插入帖子,自动生成主键
<insert id="insertPost" parameterType="com.example.springboot.entity.Forum.Post" useGeneratedKeys="true" keyProperty="id">
INSERT INTO post(user_id,title,content,created_at,updated_at)
VALUES(#{userId}, #{title}, #{content}, NOW(), NOW())
</insert>
前端调用示例(JavaScript):
fetch('/forum/post', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
userId: 123,
title: "My First Post",
content: "Hello, this is a new post!"
})
}).then(res => res.text())
.then(message => alert(message));
2. 删除帖子
功能描述
用户删除自己的帖子,管理员可删除任意帖子。
设计思路
- 前端发 DELETE 请求到
/forum/post/{id}。
- 后端业务层判断权限(示例中需要在业务加判断,代码槽位未实现)。
- 调用 Mapper 删除帖子记录。
处理流程
- 前端点击删除按钮,调用 DELETE
/forum/post/{id}。
- 后端控制器接收帖子ID,调用 Service 删除帖子。
- Service 层调用 Mapper 删除,返回删除行数。
- 返回删除成功与否消息。
核心代码
// Controller
@DeleteMapping("/post/{id}")
public String deletePost(@PathVariable Integer id) {
int result = forumService.deletePost(id);
return result > 0 ? "Post deleted successfully" : "Post deletion failed";
}
// Service
public int deletePost(Integer postId) {
// 业务逻辑可加权限校验,示例无
return forumMapper.deletePostById(postId);
}
// Mapper XML
<delete id="deletePostById" parameterType="int">
DELETE FROM post WHERE id = #{id}
</delete>
3. 修改帖子
功能描述
用户修改已有帖子的标题和内容。
设计思路
- 前端发送 PUT 请求
/forum/post ,附带帖子ID和修改信息。
- 后端接收请求体,调用业务层更新帖子。
- 返回操作结果。
处理流程
- 前端展示编辑页面,提交表单触发 PUT 请求。
- 接收到修改请求,Service 调用 Mapper 更新帖子数据。
- 返回操作状态给前端。
核心代码
// Controller
@PutMapping("/post")
public String updatePost(@RequestBody Post post) {
int result = forumService.updatePost(post);
return result > 0 ? "Post updated successfully" : "Post update failed";
}
// Service
public int updatePost(Post post) {
return forumMapper.updatePost(post);
}
// Mapper XML
<update id="updatePost" parameterType="com.example.springboot.entity.Forum.Post">
UPDATE post
SET title = #{title}, content = #{content}, updated_at = NOW()
WHERE id = #{id}
</update>
4. 根据帖子ID获取帖子详情
功能描述
查询帖子详情及相关信息,如作者名、点赞数等。
设计思路
- 前端通过 GET
/forum/post/{id} 获取帖子详情。
- 后端执行带联表和聚合的SQL,返回含点赞数的帖子详情。
处理流程
- 前端页面请求帖子详情。
- Controller 通过帖子ID调用 Service 查询帖子。
- Service 调用 Mapper 获取帖子详细信息(包括作者、点赞数)。
- 返回给前端展示。
核心代码
// Controller
@GetMapping("/post/{id}")
public Post getPostById(@PathVariable Integer id) {
return forumService.getPostById(id);
}
// Service
public Post getPostById(Integer postId) {
return forumMapper.selectPostById(postId);
}
// Mapper XML - 联表查询并统计点赞数
<select id="selectPostById" parameterType="int" resultMap="postResultMap">
SELECT
p.id,
p.user_id AS userId,
u.username,
p.title,
p.content,
p.created_at AS createdAt,
p.updated_at AS updatedAt,
IFNULL(like_counts.count, 0) AS likeCount
FROM post p
LEFT JOIN user u ON p.user_id = u.id
LEFT JOIN (
SELECT post_id, COUNT(*) AS count FROM like_post GROUP BY post_id
) like_counts ON like_counts.post_id = p.id
WHERE p.id = #{id}
</select>
5. 模糊搜索帖子标题
功能描述
根据关键词模糊匹配帖子标题,实现搜索功能。
设计思路
- 前端调用 GET
/forum/posts?title=xxx,传入搜索关键词。
- 后端执行模糊匹配 SQL,返回匹配的帖子列表。
处理流程
- 用户输入关键词,触发搜索请求。
- Controller 调用 Service,Service 调用 Mapper 执行模糊匹配查询。
- 返回帖子列表给前端显示。
核心代码
// Controller
@GetMapping("/posts")
public List<Post> searchPostsByTitle(@RequestParam String title) {
return forumService.searchPostsByTitle(title);
}
// Service
public List<Post> searchPostsByTitle(String title) {
return forumMapper.selectPostsByTitleLike(title);
}
// Mapper XML
<select id="selectPostsByTitleLike" parameterType="string" resultType="com.example.springboot.entity.Forum.Post">
SELECT
p.id,
p.user_id AS userId,
u.username,
p.title,
p.content,
p.created_at AS createdAt,
p.updated_at AS updatedAt
FROM post p
LEFT JOIN user u ON p.user_id = u.id
WHERE p.title LIKE CONCAT('%', #{title}, '%')
ORDER BY p.created_at DESC
</select>
6. 评论及回复管理(新增、删除、查看)
功能描述
用户对帖子评论,支持回复其他评论;用户和管理员可删除评论。
设计思路
- 发布评论调用 POST
/forum/comment 接口,发送评论内容及关联帖子ID、父评论ID(回复时)。
- 删除功能由用户调用 DELETE
/forum/comment/{id},管理员可调用 /admin/comment/{id} 删除评论及其子评论。
- 获取某帖子的所有评论及回复通过 GET
/forum/comments/{postId}。
处理流程
- 新增评论:
- 前端提交评论,调用接口传递评论信息。
- Service 插入数据库。
- 返回结果。
- 删除评论:
- 前端调用删除接口传评论ID。
- 管理员删除使用递归删除子评论。
- 删除成功返回消息。
- 查看评论:
- 前端请求帖子评论列表。
- 后端查询评论和回复,返回给前端。
核心代码
// 新增评论
@PostMapping("/comment")
public String createComment(@RequestBody Comment comment) {
int result = forumService.createComment(comment);
return result > 0 ? "Comment added successfully" : "Comment adding failed";
}
public int createComment(Comment comment) {
return forumMapper.insertComment(comment);
}
<insert id="insertComment" parameterType="com.example.springboot.entity.Forum.Comment" useGeneratedKeys="true" keyProperty="id">
INSERT INTO comment(post_id, user_id, parent_comment_id, content, created_at, updated_at)
VALUES(#{postId}, #{userId}, #{parentCommentId}, #{content}, NOW(), NOW())
</insert>
// 删除评论 (普通用户)
@DeleteMapping("/comment/{id}")
public String deleteComment(@PathVariable Integer id) {
int result = forumService.deleteComment(id);
return result > 0 ? "Comment deleted successfully" : "Comment deletion failed";
}
public int deleteComment(Integer commentId) {
return forumMapper.deleteCommentById(commentId);
}
// 管理员删除评论及子评论
@DeleteMapping("/admin/comment/{id}")
public String adminDeleteComment(@PathVariable Integer id) {
int result = forumService.adminDeleteComment(id);
return result > 0 ? "评论及其子评论删除成功" : "删除失败";
}
public int adminDeleteComment(Integer commentId) {
List<Comment> children = forumMapper.selectChildCommentsByParentId(commentId);
int count = 0;
for (Comment child : children) {
count += adminDeleteComment(child.getId());
}
count += forumMapper.deleteCommentById(commentId);
return count;
}
// 查询评论列表(含用户名等)
@GetMapping("/comments/{postId}")
public List<Comment> getCommentsByPostId(@PathVariable Integer postId) {
return forumService.getCommentsByPostId(postId);
}
public List<Comment> getCommentsByPostId(Integer postId) {
return forumMapper.selectCommentsByPostId(postId);
}
<select id="selectCommentsByPostId" parameterType="int" resultType="com.example.springboot.entity.Forum.Comment">
SELECT
c.id,
c.post_id AS postId,
c.user_id AS userId,
u.username,
c.parent_comment_id AS parentCommentId,
c.content,
c.created_at AS createdAt,
c.updated_at AS updatedAt
FROM comment c
LEFT JOIN user u ON c.user_id = u.id
WHERE c.post_id = #{postId}
ORDER BY c.created_at ASC
</select>
7. 点赞及取消点赞帖子
功能描述
用户可以点赞帖子,重复点赞不允许,支持取消点赞。
设计思路
- 点赞调用 POST
/forum/post/like。
- 取消点赞调用 DELETE
/forum/post/like?userId=&postId=。
- 后端判重,避免重复点赞。
- 查询点赞状态和数量。
处理流程
- 用户点击点赞,提交包含 userId、postId 的请求。
- Service 先查询是否已点过赞。
- 未点赞时插入数据表,返回成功。
- 取消点赞时删除对应记录。
- 查询帖子点赞数量动态统计。
核心代码
// 点赞
@PostMapping("/post/like")
public String likePost(@RequestBody LikePost likePost) {
int result = forumService.createLikePost(likePost);
return result > 0 ? "点赞成功" : "已经点赞过或点赞失败";
}
public int createLikePost(LikePost likePost) {
LikePost exist = forumMapper.selectLikePost(likePost.getUserId(), likePost.getPostId());
if (exist != null) return 0;
return forumMapper.insertLikePost(likePost);
}
// 取消点赞
@DeleteMapping("/post/like")
public String unlikePost(@RequestParam Integer userId, @RequestParam Integer postId) {
int result = forumService.deleteLikePost(userId, postId);
return result > 0 ? "取消点赞成功" : "取消点赞失败";
}
public int deleteLikePost(Integer userId, Integer postId) {
return forumMapper.deleteLikePost(userId, postId);
}
// 查询点赞状态
@GetMapping("/post/like/status")
public Map<String, Boolean> getLikeStatus(@RequestParam Integer userId, @RequestParam Integer postId) {
boolean liked = forumService.getLikePost(userId, postId) != null;
return Collections.singletonMap("liked", liked);
}
public LikePost getLikePost(Integer userId, Integer postId) {
return forumMapper.selectLikePost(userId, postId);
}
// 查询点赞数
public int getLikeCountByPostId(Integer postId) {
return forumMapper.selectLikeCountByPostId(postId);
}
8. 收藏及取消收藏帖子
功能描述
用户收藏帖子,支持取消收藏,查询收藏状态。
设计思路
- 收藏通过 POST 请求
/forum/post/favorite。
- 取消收藏通过 DELETE
/forum/post/favorite。
- 查询收藏状态接口
/forum/post/favorite/status。
处理流程
- 收藏时检查是否已收藏,插入收藏记录。
- 取消收藏时删除记录。
- 返回操作结果及收藏状态。
核心代码
// 收藏帖子
@PostMapping("/post/favorite")
public String favoritePost(@RequestBody FavoritePost favoritePost) {
int result = forumService.createFavoritePost(favoritePost);
return result > 0 ? "收藏成功" : "收藏失败或已收藏";
}
public int createFavoritePost(FavoritePost favoritePost) {
FavoritePost exist = forumMapper.selectFavoritePost(favoritePost.getUserId(), favoritePost.getPostId());
if (exist != null) return 0;
return forumMapper.insertFavoritePost(favoritePost);
}
// 取消收藏
@DeleteMapping("/post/favorite")
public String unfavoritePost(@RequestParam Integer userId, @RequestParam Integer postId) {
int result = forumService.deleteFavoritePost(userId, postId);
return result > 0 ? "取消收藏成功" : "取消收藏失败";
}
public int deleteFavoritePost(Integer userId, Integer postId) {
return forumMapper.deleteFavoritePost(userId, postId);
}
// 查询收藏状态
@GetMapping("/post/favorite/status")
public Map<String, Boolean> getFavoriteStatus(@RequestParam Integer userId, @RequestParam Integer postId) {
boolean favorited = forumService.getFavoritePost(userId, postId) != null;
return Collections.singletonMap("favorited", favorited);
}
public FavoritePost getFavoritePost(Integer userId, Integer postId) {
return forumMapper.selectFavoritePost(userId, postId);
}
9. 举报帖子及管理举报
功能描述
用户举报帖子,管理员查看举报信息。
设计思路
- 用户使用 POST
/forum/post/report 提交举报信息。
- 管理员使用 GET
/forum/post/reports/{postId} 查询某帖子的举报列表。
- 同时支持查询所有举报
/forum/post/reports。
处理流程
- 用户提交举报,后端插入举报记录。
- 管理员查询举报,根据帖子ID过滤或者查询所有。
- 显示举报详情方便审核处理。
核心代码
// 举报帖子
@PostMapping("/post/report")
public String reportPost(@RequestBody ReportPost reportPost) {
int result = forumService.createReportPost(reportPost);
return result > 0 ? "举报成功" : "举报失败";
}
public int createReportPost(ReportPost reportPost) {
// 可加重复举报校验,暂略
return forumMapper.insertReportPost(reportPost);
}
// 查询某帖子举报
@GetMapping("/post/reports/{postId}")
public List<ReportPost> getReportsByPostId(@PathVariable Integer postId) {
return forumService.getReportPostsByPostId(postId);
}
public List<ReportPost> getReportPostsByPostId(Integer postId) {
return forumMapper.selectReportPostsByPostId(postId);
}
// 查询所有举报
@GetMapping("/post/reports")
public List<ReportPost> getAllReports() {
return forumService.getAllReportPosts();
}
public List<ReportPost> getAllReportPosts() {
return forumMapper.selectAllReportPosts();
}
10. 查询用户的帖子、点赞帖子、收藏帖子列表
功能描述
查询特定用户发布的帖子,用户点赞过和收藏过的帖子列表,带点赞、收藏状态。
设计思路
- 对应接口分别是:
/forum/user/{userId}/posts 查询用户帖子。
/forum/user/{userId}/liked-posts 查询用户点赞帖子。
/forum/user/{userId}/favorited-posts 查询用户收藏帖子。
- 查询结果默认设置帖子的点赞数、点赞与收藏状态,便于前端展示。
处理流程
- 前端请求特定接口,根据用户ID查询对应帖子列表。
- 后端调用 Mapper 联表查询。
- Service 对帖子列表再补充用户点赞和收藏标记。
- 返回给前端渲染。
核心代码
// 查询用户帖子,并补充点赞收藏状态
@GetMapping("/user/{userId}/posts")
public List<Post> getPostsByUserId(@PathVariable Integer userId) {
List<Post> posts = forumService.getPostsByUserId(userId);
// 业务层补全点赞、收藏状态
posts.forEach(post -> {
int likeCount = forumService.getLikeCountByPostId(post.getId());
LikePost likePost = forumService.getLikePost(userId, post.getId());
post.setLiked(likePost != null);
FavoritePost favPost = forumService.getFavoritePost(userId, post.getId());
post.setFavorited(favPost != null);
});
return posts;
}
public List<Post> getPostsByUserId(Integer userId) {
List<Post> posts = forumMapper.selectPostsByUserId(userId);
enrichPostsWithLikeFavorite(userId, posts);
return posts;
}
// 查询用户点赞的帖子列表
@GetMapping("/user/{userId}/liked-posts")
public List<Post> getLikedPostsByUserId(@PathVariable Integer userId) {
return forumService.getLikedPostsByUserId(userId);
}
public List<Post> getLikedPostsByUserId(Integer userId) {
List<Post> posts = forumMapper.selectLikedPostsByUserId(userId);
enrichPostsWithLikeFavorite(userId, posts);
return posts;
}
// 查询用户收藏的帖子列表
@GetMapping("/user/{userId}/favorited-posts")
public List<Post> getFavoritedPostsByUserId(@PathVariable Integer userId) {
return forumService.getFavoritedPostsByUserId(userId);
}
public List<Post> getFavoritedPostsByUserId(Integer userId) {
List<Post> posts = forumMapper.selectFavoritedPostsByUserId(userId);
enrichPostsWithLikeFavorite(userId, posts);
return posts;
}
// 补充点赞数、点赞状态、收藏状态
private void enrichPostsWithLikeFavorite(Integer userId, List<Post> posts) {
if (posts == null || posts.isEmpty()) return;
boolean isLikedList = true;
for (Post post : posts) {
int likeCount = forumMapper.selectLikeCountByPostId(post.getId());
post.setLikeCount(likeCount);
if(isLikedList) {
post.setLiked(true); // 对点赞过的帖子列表,直接给true
} else {
LikePost likePost = forumMapper.selectLikePost(userId, post.getId());
post.setLiked(likePost != null);
}
FavoritePost favPost = forumMapper.selectFavoritePost(userId, post.getId());
post.setFavorited(favPost != null);
if(isLikedList && !post.getLiked()){
isLikedList = false;
}
}
}
总结说明
- 架构清晰:采用 Controller -> Service -> Mapper(MyBatis)分层设计,职责分明。
- 数据模型完整:Posts、Comments、Likes、Favorites、Reports 均有对应的实体、Mapper和业务处理。
- 接口丰富:提供了完整的帖子管理、评论管理、点赞、收藏、举报功能。
- 权限预留:虽然示例代码权限判断未完善,但业务层已有删除帖子及评论的接口,便于后续权限扩展。
- 扩展性良好:通过丰富返回的数据结构(如帖子点赞数、点赞状态、收藏状态),方便前端提供良好的交互体验。
如果你需要某一功能更详细的流程图或前端示例,也可以告诉我,我可以进一步补充。