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

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

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

相关推荐
非 白5 分钟前
【Java】代理模式
java·开发语言·代理模式
muxue17814 分钟前
go:运行第一个go语言程序
开发语言·后端·golang
米饭好好吃.15 分钟前
【Go】Go wire 依赖注入
开发语言·后端·golang
闲猫15 分钟前
go 接口interface func (m Market) getName() string {
开发语言·后端·golang
Good Note15 分钟前
Golang的静态强类型、编译型、并发型
java·数据库·redis·后端·mysql·面试·golang
可爱de艺艺15 分钟前
Go入门之struct
开发语言·后端·golang
信徒_18 分钟前
Go 语言中的协程
开发语言·后端·golang
心随_风动28 分钟前
CentOS 下安装和配置 HTTPD 服务的详细指南
linux·运维·centos
m0_7482365831 分钟前
跟据spring boot版本,查看对应的tomcat,并查看可支持的tomcat的版本范围
spring boot·后端·tomcat
信阳农夫32 分钟前
centos 7只能安装到3.6.8
linux·运维·centos