高级项目—评论模块和消息中心模块

08月31日 收藏 0 评论 1 java开发

高级项目—评论模块和消息中心模块

文章申明:转载来源:https://blog.csdn.net/qq_37591656/article/details/87381788

一、评论中心模块开发

无论是问题的评论,还是评论的评论,都是基于评论中心的。

1、数据库

2、javaModel实体

和数据库一一对应的实体

3、DAO层

评论list是通过entity_id(归属的实体ID)和entity_type(归属于何种实体)进行确定的。
1) 添加评论(向表中插入一条数据)

@Insert({"insert into ", TABLE_NAME, "(", INSERT_FIELDS,
") values (#{userId},#{content},#{createdDate},#{entityId},#{entityType},#{status})"})
int addComment(Comment comment);

2) 根据entity_id和entity_type 查询,也就是查询某问题的所有评论

/**
* 通过entityId和entityType可以定位到一条评论(或问题),
* 查找出它的所有评论
*/
@Select({"select ", SELECT_FIELDS, " from ", TABLE_NAME,
" where entity_id=#{entityId} and entity_type=#{entityType} order by created_date desc"})
List<Comment> selectByEntity(@Param("entityId") int entityId, @Param("entityType") int entityType);

3) 查询某问题的评论总数

@Select({"select count(id) from ", TABLE_NAME, " where entity_id=#{entityId} and entity_type=#{entityType} "})
int getCommentCount(@Param("entityId") int entityId, @Param("entityType") int entityType);

4、Service层
业务需求有 1)添加评论(回复问题) 2)展示所有回复

5、controller层
5.1 添加评论的controller
这里获取view中POST过来的数据,封装成comment,接着调用service进行添加。
并且对问题所携带的评论总数进行更新。

@RequestMapping(path = {"/addComment"}, method = {RequestMethod.POST})
public String addComment(@RequestParam("questionId") int questionId,
@RequestParam("content") String content) {

try {
Comment comment = new Comment();
comment.setContent(content);
if (hostHolder.getUser() != null) {
comment.setUserId(hostHolder.getUser().getId());
} else {
//匿名用户
comment.setUserId(WendaUtil.ANONYMOUS_USERID);
// 跳转到登陆界面
//return "redirect:/reglogin";
}
comment.setCreatedDate(new Date());
comment.setEntityType(EntityType.ENTITY_QUESTION);
comment.setEntityId(questionId);

commentService.addComment(comment);
int countComment = commentService.getCommentCount(comment.getEntityId(), comment.getEntityType());

questionService.updateCommentCount(comment.getEntityId(), countComment);
} catch (Exception e) {
logger.error("增加评论失败" + e.getMessage());

}
return "redirect:/question/" + questionId;
}

5.2 展示评论的 question/{qid} controller

再此之前已经把question通过model发送到了页面View,此外还需要把评论也发送过去。

评论是列表形式,仿造之前的viewObject,把每一条评论封装成viewObject,然后添加到list。页面通过调用foreach(list),就能把所有的评论展示

@RequestMapping(path = {"question/{qid}"})
public String questionDetail(Model model,
@PathVariable("qid") int qid) {
Question question = questionService.selectById(qid);
model.addAttribute("question", question);

List<Comment> commentList = commentService.getCommentsByEntity(qid, EntityType.ENTITY_QUESTION);
List<ViewObject> comments = new ArrayList<ViewObject>();
for (Comment comment : commentList) {
// 每个问题的评论,封装成一个viewObject
ViewObject vo = new ViewObject();
vo.set("comment", comment);
vo.set("user", userService.getUser(comment.getUserId()));
comments.add(vo);
}

model.addAttribute("comments", comments);
return "detail";
}

二、消息中心模块开发

A发送消息给B,那么数据库中存储一条数据。其中from_id = A.getId(),to_id = B.getId(),conversation_id = A.getId() + “_” + B.getId()(一律小号在前)。

因此数据库是这样的。


2.1 发送一条私信

数据库里就是插入一条数据而已。

2.2 展示两人来往信息情况

构造出两人的conversationId,然后在表中进行查找。

@RequestMapping(path = {"/msg/detail"}, method = {RequestMethod.GET})
public String getConversationDetail(Model model, @RequestParam("conversationId") String conversationId) {
try {
List<Message> messageList = messageService.getConversationDetail(conversationId, 0, 10);
List<ViewObject> messages = new ArrayList<>();
for (Message message : messageList) {
ViewObject vo = new ViewObject();
vo.set("message", message);
vo.set("user", userService.getUser(message.getFromId()));

messages.add(vo);
}
model.addAttribute("messages", messages);
} catch (Exception e) {
logger.error("获取站内信详情失败" + e.getMessage());
}
return "letterDetail";
}

2.3 展示当前用户的消息总览

每对用户间的对话只保留一个,当用户点击进去,才是2.2完成的具体对话情况。
因此在表中通过conversation_id进行分组查询,并且通过日期进行逆序排序,顺带查询出count(id),这是分组后,每个分组内的count(用于计算信息总数)

sql语句

select *,count(id)as cnt from
(select * from message ORDER BY created_date desc) tt
GROUP BY conversation_id;

最终再进行按时间排序,最新的排在最上面。加上分页

select *,count(id)as cnt from (select * from message ORDER BY created_date desc) tt GROUP BY conversation_id
ORDER BY created_date limit 0 2;

2.4 Controller层
1、查看“我的私信”界面,展示和别人的交流,概括展示。
使用List< ViewObject >用于展示多条数据,每个viewObject添加进去展示需要用到的量。

@RequestMapping(path = {"/msg/list"}, method = {RequestMethod.GET})
public String getConversationList(Model model) {
User user = hostHolder.getUser();
if (user == null) {
return "redirect:/reglogin";
}
int userId = hostHolder.getUser().getId();
List<Message> conversationList = messageService.getConversationList(userId, 0, 10);
List<ViewObject> conversations = new ArrayList<>();
for (Message message : conversationList) {
ViewObject ov = new ViewObject();
ov.set("conversation", message);
int targetId = message.getFromId() == userId ? message.getToId() : message.getFromId();
ov.set("user", userService.getUser(targetId));
ov.set("unread", messageService.getConvesationUnreadCount(userId, message.getConversationId()));
conversations.add(ov);
}
model.addAttribute("conversations", conversations);
return "letter";
}

再1)的界面中,点击进去查看和某个人的详细聊天信息,这里按照conversionId进行查找即可。

发送信息
构造出一个Message,插入数据库即可。

C 1条回复 评论
努力努力再努力

感谢,这种刷题式的学习方式真的很方便!

发表于 2023-02-08 21:00:00
0 0