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

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

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

相关推荐
积水成江1 小时前
Vite+Vue3+SpringBoot项目如何打包部署
java·前端·vue.js·windows·spring boot·后端·nginx
爱吃龙利鱼2 小时前
网络基础知识笔记(五)接口管理
运维·网络·笔记·云原生·智能路由器
陈小肚2 小时前
DNS 反向解析导致 ssh 连接缓慢
运维·ssh
zbdx不知名菜鸡2 小时前
linux基础 超级笔记
linux·运维·服务器
wellnw2 小时前
【IPv6】IPv6地址格式及地址分类(组播、单播、任播)整理
运维·服务器·网络
CocoaAndYy2 小时前
ThreadLocal、InheritableThreadLocal、TransmittableThreadLocal原理及Demo
java·jvm·算法
CoderJia程序员甲3 小时前
重学SpringBoot3-集成Redis(六)之消息队列
spring boot·redis·中间件
wanhengwangluo3 小时前
高防服务器的优劣势有哪些?
运维·服务器
原机小子3 小时前
SpringBoot在线教育系统:从零到一的构建过程
数据库·spring boot·后端
2401_857439693 小时前
SpringBoot在线教育平台:设计与实现的深度解析
java·spring boot·后端