携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第15天,点击查看活动详情
3.7 私信列表 和 私信详情
这次我们开发的是私信列表和私信详情两个功能,这里我们一起开发。
首先是私信对应的实体类 Message
public class Message {
private int id; // 主键
private int fromId; // 发信方id
private int toId; // 首先方id
private String conversationId; // 会话id,有fromId和toId组成
private String content; // 绘画内容
private int status; // 状态 0-未读 1-已读 2-删除
private Date createTime; // 日期
// 以免影响阅读体验,get。set。toString方法 没有粘,但开发时这些方法是需要的
}
- 数据访问层(dao)
因为是一个新业务,所以我们创建一个新的dao接口
@Mapper
public interface MessageMapper {
// 开发私信列表页面:查询当前用户的会话列表(支持分页),针对每个会话只返回一条最新的消息
List<Message> selectConversations(int userId, int offset, int limit);
// 开发私信列表页面:查询当前用户的会话数量
int selectConversationCount(int userId);
// 开发私信详情页面:查询某个会话包含的私信列表
List<Message> selectLetters(String conversationId, int offset, int limit);
// 开发私信详情页面:查询某个会话所包含的私信数量
int selectLetterCount(String conversationId);
// 查询未读私信的数量, 这里conversationId作为动态的条件去拼,不是一定会有,传了就拼上去,这样这个方法能够实现两种业务
int selectLetterUnreadCount(int userId, String conversationId);
}
然后是对应的mapper配置文件message-mapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.nowcoder.community.dao.MessageMapper">
<sql id="selectFields">
id, from_id, to_id, conversation_id, content, status, create_time
</sql>
<!--查询所有会话,每个会话查询和某个人的最后一个对话,from_id=1是系统用户是通知不是私信-->
<select id="selectConversations" resultType="com.nowcoder.community.entity.Message">
select <include refid="selectFields"></include>
from message
where id in (
select max(id) from message
where status != 2
and from_id != 1
and (from_id = #{userId} or to_id = #{userId})
group by conversation_id
)
order by id desc
limit #{offset}, #{limit}
</select>
<!--查询私信列表的会话数量-->
<select id="selectConversationCount" resultType="Integer">
select count(m.maxid) from (
select max(id) as maxid from message
where status != 2
and from_id != 1
and (from_id = #{userId} or to_id = #{userId})
group by conversation_id
) as m
</select>
<!--查询某个会话包含的私信列表-->
<select id="selectLetters" resultType="Message">
select <include refid="selectFields"></include>
from message
where status != 2
and from_id != 1
and conversation_id = #{conversationId}
order by id desc
limit #{offset}, #{limit}
</select>
<!--查询某个会话所包含的私信数量-->
<select id="selectLetterCount" resultType="Integer">
select count(id)
from message
where status != 2
and from_id != 1
and conversation_id = #{conversationId}
</select>
<!--查询未读私信的数量-->
<select id="selectLetterUnreadCount" resultType="Integer">
select count(id)
from message
where status = 0
and from_id != 1
and to_id = #{userId}
<if test="conversationId!=null">
and conversation_id = #{conversationId}
</if>
</select>
</mapper>
- 业务层
新建一个Service:MessageService
@Service
public class MessageService {
@Autowired
private MessageMapper messageMapper;
public List<Message> findConversations(int userId, int offset, int limit) {
return messageMapper.selectConversations(userId, offset, limit);
}
public int findConversationCount(int userId) {
return messageMapper.selectConversationCount(userId);
}
public List<Message> findLetters(String conversationId, int offset, int limit) {
return messageMapper.selectLetters(conversationId, offset, limit);
}
public int findLetterCount(String conversationId) {
return messageMapper.selectLetterCount(conversationId);
}
public int findLetterUnreadCount(int userId, String conversationId) {
return messageMapper.selectLetterUnreadCount(userId, conversationId);
}
}
- 表现层
表现层我们需要两个功能,一个是私信列表,一个是私信详情。这两个功能写在一个controller里就可以。
先处理私信列表
新建一个Controller叫 MessageController
@Controller
public class MessageController {
@Autowired
private MessageService messageService;
@Autowired
private HostHolder hostHolder;
@Autowired
private UserService userService;
// 私信列表
@RequestMapping(path = "/letter/list", method = RequestMethod.GET)
public String getLetterList(Model model, Page page){
User user = hostHolder.getUser();
// 分页信息
page.setLimit(5); //设置一页显示5条
page.setPath("/letter/llist"); //设置路径
page.setRows(messageService.findConversationCount(user.getId())); // 设置总条数
// 会话列表
List<Message> conversationList = messageService.findConversations(user.getId(),
page.getOffset(), page.getLimit());
List<Map<String, Object>> conversations = new ArrayList<>();
if(conversationList != null){
for (Message message : conversationList) {
Map<String, Object> map = new HashMap<>();
// 将 message 封装进去
map.put("conversation", message);
// 私信数量
map.put("letterCount", messageService.findLetterCount(message.getConversationId()));
// 将 未读消息数量 封装进去
map.put("unreadCount", messageService.findLetterUnreadCount(user.getId(), message.getConversationId()));
// 找到谁给“自己”传了消息的id,然后传入,因为我们在页面上还要显示这个人的用户名和头像
int targetId = user.getId() == message.getFromId() ? message.getToId() : message.getFromId();
map.put("target", userService.findUserById(targetId));
conversations.add(map);
}
}
model.addAttribute("conversations", conversations);
// 查询未读消息数量
int letterUnreadCount = messageService.findLetterUnreadCount(user.getId(), null);
model.addAttribute("letterUnreadCount", letterUnreadCount);
return "/site/letter";
}
}
首先要将首页 index.html 的 “消息” 链到 controller 上
然后是处理私信列表的模板 letter.html
到这里私信列表功能就已经实现了,到这里可以启动一下项目测试一下
接下来开发 私信详情 功能
在 MessageController 中添加下面方法
// 私信详情
@RequestMapping(path = "/letter/detail/{conversationId}", method = RequestMethod.GET)
public String getLetterDetail(@PathVariable("conversationId") String conversationId, Page page, Model model) {
// 分页信息
page.setLimit(5);
page.setPath("/letter/detail/" + conversationId);
page.setRows(messageService.findLetterCount(conversationId));
// 私信列表
List<Message> letterList = messageService.findLetters(conversationId, page.getOffset(), page.getLimit());
List<Map<String, Object>> letters = new ArrayList<>();
if (letterList != null) {
for (Message message : letterList) {
Map<String, Object> map = new HashMap<>();
map.put("letter", message);
map.put("fromUser", userService.findUserById(message.getFromId()));
letters.add(map);
}
}
model.addAttribute("letters", letters);
// 私信目标
model.addAttribute("target", getLetterTarget(conversationId));
return "/site/letter-detail";
}
// 根据conversationId查询目标用户
private User getLetterTarget(String conversationId) {
String[] ids = conversationId.split("_");
int id0 = Integer.parseInt(ids[0]); // 将字符串转成整数
int id1 = Integer.parseInt(ids[1]); // 将字符串转成整数
if (hostHolder.getUser().getId() == id0) {
return userService.findUserById(id1);
} else {
return userService.findUserById(id0);
}
}
然后我们就可以处理模板了
首先我们在会话列表里 letter.html 做一个处理,把那个链接(可以链到详情页面)做好
然后处理私信详情页面 letter-detail.html
到这里私信详情就开发完了
测试
经测试,功能开发成功!