仿牛客网项目---私信列表和发送列表功能的实现

这篇文章我们来讲一下我的这个项目的另外一个功能:私信列表和发送列表功能。

先来设计DAO层。

java 复制代码
@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);

    // 查询未读私信的数量
    int selectLetterUnreadCount(int userId, String conversationId);

    // 新增消息
    int insertMessage(Message message);

    // 修改消息的状态
    int updateStatus(List<Integer> ids, int status);

    // 查询某个主题下最新的通知
    Message selectLatestNotice(int userId, String topic);

    // 查询某个主题所包含的通知数量
    int selectNoticeCount(int userId, String topic);

    // 查询未读的通知的数量
    int selectNoticeUnreadCount(int userId, String topic);

    // 查询某个主题所包含的通知列表
    List<Message> selectNotices(int userId, String topic, int offset, int limit);

}

这些方法提供了对消息数据库的读取和写入操作,包括查询会话、私信和通知的列表和数量,查询未读消息数量,新增消息,修改消息状态等。这些方法可以用于实现用户私信功能、通知功能以及消息管理等相关功能的开发。

再来设计Service层。

java 复制代码
 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);
    }

    public int addMessage(Message message) {
        message.setContent(HtmlUtils.htmlEscape(message.getContent()));
        message.setContent(sensitiveFilter.filter(message.getContent()));
        return messageMapper.insertMessage(message);
    }

    public int readMessage(List<Integer> ids) {
        return messageMapper.updateStatus(ids, 1);
    }

    public Message findLatestNotice(int userId, String topic) {
        return messageMapper.selectLatestNotice(userId, topic);
    }

    public int findNoticeCount(int userId, String topic) {
        return messageMapper.selectNoticeCount(userId, topic);
    }

    public int findNoticeUnreadCount(int userId, String topic) {
        return messageMapper.selectNoticeUnreadCount(userId, topic);
    }

    public List<Message> findNotices(int userId, String topic, int offset, int limit) {
        return messageMapper.selectNotices(userId, topic, offset, limit);
    }

这段代码是一个消息服务类,它调用了消息映射器(MessageMapper)中定义的方法来实现对消息数据的访问和操作。以下是每个方法的功能解析:

  1. findConversations(int userId, int offset, int limit): 查询当前用户的会话列表,返回一定数量的会话。
  2. findConversationCount(int userId): 查询当前用户的会话数量。
  3. findLetters(String conversationId, int offset, int limit): 查询某个会话中的私信列表,返回一定数量的私信。
  4. findLetterCount(String conversationId): 查询某个会话中的私信数量。
  5. findLetterUnreadCount(int userId, String conversationId): 查询某个会话中未读私信的数量。
  6. addMessage(Message message): 添加一条消息,并进行 HTML 转义和敏感词过滤。
  7. readMessage(List<Integer> ids): 将消息标记为已读。
  8. findLatestNotice(int userId, String topic): 查询某个主题下最新的通知。
  9. findNoticeCount(int userId, String topic): 查询某个主题下的通知数量。
  10. findNoticeUnreadCount(int userId, String topic): 查询某个主题下未读通知的数量。
  11. findNotices(int userId, String topic, int offset, int limit): 查询某个主题下的通知列表,返回一定数量的通知。

该消息服务类通过调用消息映射器中的方法,将数据库的读取和写入操作封装成更高层次的服务方法,方便其他模块调用并实现相关的业务逻辑。这些方法可以用于获取会话列表、私信列表、未读消息数量、最新通知等信息,并进行消息的添加和读取操作。

最后设计controller层。

controller层太多了,就不写了,因为这个功能其实也不是很重要,面试不怎么会问。

相关推荐
岁忧16 分钟前
(LeetCode 面试经典 150 题 ) 11. 盛最多水的容器 (贪心+双指针)
java·c++·算法·leetcode·面试·go
乌托邦的逃亡者19 分钟前
Docker的/var/lib/docker/目录占用100%的处理方法
运维·docker·容器
CJi0NG20 分钟前
【自用】JavaSE--算法、正则表达式、异常
java
ldj202023 分钟前
Jenkins 流水线配置
运维·jenkins
Nejosi_念旧44 分钟前
解读 Go 中的 constraints包
后端·golang·go
风无雨1 小时前
GO 启动 简单服务
开发语言·后端·golang
Hellyc1 小时前
用户查询优惠券之缓存击穿
java·redis·缓存
小明的小名叫小明1 小时前
Go从入门到精通(19)-协程(goroutine)与通道(channel)
后端·golang
斯普信专业组1 小时前
Go语言包管理完全指南:从基础到最佳实践
开发语言·后端·golang
今天又在摸鱼1 小时前
Maven
java·maven