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

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

先来设计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层太多了,就不写了,因为这个功能其实也不是很重要,面试不怎么会问。

相关推荐
掘金者阿豪几秒前
金仓数据库与Java整合Activiti工作流实战:从兼容性到SpringBoot配置
后端
郭京京10 分钟前
go框架 Gin(上)
后端
ikun10 分钟前
geotools28.6异常 java.lang.NoSuchFieldError: JAVA_9
后端
道路与代码之旅10 分钟前
Delphi - IndyHttpServer接收上传文件
运维·服务器
烦躁的大鼻嘎43 分钟前
【Linux】深入Linux多线程架构与高性能编程
linux·运维·服务器·开发语言·c++·ubuntu
緈諨の約錠1 小时前
JVM基础篇以及JVM内存泄漏诊断与分析
java·jvm
Slaughter信仰1 小时前
深入理解Java虚拟机:JVM高级特性与最佳实践(第3版)第十三章知识点问答(15题)
java·开发语言·jvm
羚羊角uou1 小时前
【Linux】system V共享内存
linux·运维·服务器
winfield8211 小时前
Redis 线上问题排查简版手册
redis
绝无仅有1 小时前
大厂Redis高级面试题与答案
后端·面试·github