Java实现问答点赞与回复功能详解
时间:2025-10-30 08:26:30 205浏览 收藏
大家好,今天本人给大家带来文章《Java实现在线问答点赞与回复方法》,文中内容主要涉及到,如果你对文章方面的知识点感兴趣,那就请各位朋友继续看下去吧~希望能真正帮到你们,谢谢!
首先设计Question、Reply和Like实体类,接着通过LikeService实现点赞去重与计数更新,ReplyService处理回复存储与查询,最后由QuestionController暴露reply和like接口,结合数据库与缓存优化性能,确保点赞功能的正确性与高效性。

在Java中实现在线问答系统的点赞与回复功能,核心在于设计合理的数据模型、服务逻辑以及接口交互。以下是具体实现思路和关键代码示例。
1. 数据模型设计
定义问题、回复、点赞相关的实体类。
Question(问题):
public class Question {
private Long id;
private String title;
private String content;
private Long userId;
private List<Reply> replies = new ArrayList<>();
private int likeCount = 0;
// getter 和 setter
}
Reply(回复):
public class Reply {
private Long id;
private Long questionId;
private Long userId;
private String content;
private int likeCount = 0;
// getter 和 setter
}
Like(点赞记录):用于防止重复点赞
public class Like {
private Long id;
private Long targetId; // 目标ID(问题或回复)
private String targetType; // 类型:"question" 或 "reply"
private Long userId;
// getter 和 setter
}
2. 点赞功能实现
通过判断目标类型(问题或回复),检查用户是否已点赞,未点赞则添加记录并更新计数。
@Service
public class LikeService {
private Map<String, Set<Long>> userLikes = new HashMap<>(); // 实际应使用数据库
public boolean addLike(Long userId, String targetType, Long targetId) {
String key = targetType + ":" + targetId;
Set<Long> likedUsers = userLikes.computeIfAbsent(key, k -> new HashSet<>());
if (likedUsers.contains(userId)) {
return false; // 已点赞
}
likedUsers.add(userId);
// 更新问题或回复的点赞数(实际中调用对应service)
if ("question".equals(targetType)) {
updateQuestionLikeCount(targetId, 1);
} else if ("reply".equals(targetType)) {
updateReplyLikeCount(targetId, 1);
}
return true;
}
private void updateQuestionLikeCount(Long questionId, int delta) {
// 调用 QuestionService 增加点赞数
}
private void updateReplyLikeCount(Long replyId, int delta) {
// 调用 ReplyService 增加点赞数
}
}
3. 回复功能实现
用户提交回复后,将内容保存并与对应问题关联。
@Service
public class ReplyService {
private List<Reply> replies = new ArrayList<>(); // 模拟存储
public Reply createReply(Long questionId, Long userId, String content) {
Reply reply = new Reply();
reply.setId((long) (replies.size() + 1));
reply.setQuestionId(questionId);
reply.setUserId(userId);
reply.setContent(content);
replies.add(reply);
return reply;
}
public List<Reply> getRepliesByQuestionId(Long questionId) {
return replies.stream()
.filter(r -> r.getQuestionId().equals(questionId))
.collect(Collectors.toList());
}
}
4. 控制器层接口
提供HTTP接口供前端调用。
@RestController
@RequestMapping("/api/questions")
public class QuestionController {
@Autowired
private ReplyService replyService;
@Autowired
private LikeService likeService;
@PostMapping("/{id}/reply")
public Reply replyToQuestion(
@PathVariable Long id,
@RequestParam Long userId,
@RequestParam String content) {
return replyService.createReply(id, userId, content);
}
@PostMapping("/{id}/like")
public ResponseEntity<String> likeQuestion(
@PathVariable Long id,
@RequestParam Long userId) {
boolean success = likeService.addLike(userId, "question", id);
if (success) {
return ResponseEntity.ok("点赞成功");
} else {
return ResponseEntity.badRequest().body("已点赞");
}
}
@PostMapping("/replies/{replyId}/like")
public ResponseEntity<String> likeReply(
@PathVariable Long replyId,
@RequestParam Long userId) {
boolean success = likeService.addLike(userId, "reply", replyId);
if (success) {
return ResponseEntity.ok("点赞成功");
} else {
return ResponseEntity.badRequest().body("已点赞");
}
}
}
基本上就这些。使用Spring Boot可快速搭建该结构,实际项目中需结合数据库(如MySQL、MongoDB)和缓存(如Redis)提升性能。点赞去重建议用唯一索引约束,避免并发问题。
以上就是《Java实现问答点赞与回复功能详解》的详细内容,更多关于的资料请关注golang学习网公众号!
相关阅读
更多>
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
最新阅读
更多>
-
164 收藏
-
341 收藏
-
125 收藏
-
427 收藏
-
152 收藏
-
129 收藏
-
334 收藏
-
431 收藏
-
294 收藏
-
292 收藏
-
183 收藏
-
288 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 485次学习