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

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

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

相关推荐
陈随易2 分钟前
VSCode v1.102发布,AI体验大幅提升
前端·后端·程序员
宇钶宇夕4 分钟前
SIMATIC S7-1200的以太网通信能力:协议与资源详细解析
运维·服务器·数据库·程序人生·自动化
该用户已不存在6 分钟前
关于我把Mac Mini托管到机房,后续来了,还有更多玩法
服务器·前端·mac
杰夫贾维斯12 分钟前
CentOS Linux 8 的系统部署 Qwen2.5-7B -Instruct-AWQ
linux·运维·人工智能·机器学习·centos
生无谓16 分钟前
什么是跨域,如何处理跨域
后端
咖啡啡不加糖17 分钟前
RabbitMQ 消息队列:从入门到Spring Boot实战
java·spring boot·rabbitmq
Smilejudy18 分钟前
极具特色的位置运算
后端
码出极致18 分钟前
支付线上问题复盘的“5W”框架
后端
玩代码24 分钟前
Java线程池原理概述
java·开发语言·线程池
ezl1fe27 分钟前
RAG 每日一技(三):不止文本,代码和Markdown如何优雅地分块?
后端