這篇文章我們來講一下我的這個項目的另外一個功能:私信列表和發送列表功能。
先來設計DAO層。
@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層。
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)中定義的方法來實現對消息數據的訪問和操作。以下是每個方法的功能解析:
findConversations(int userId, int offset, int limit)
: 查詢當前用戶的會話列表,返回一定數量的會話。findConversationCount(int userId)
: 查詢當前用戶的會話數量。findLetters(String conversationId, int offset, int limit)
: 查詢某個會話中的私信列表,返回一定數量的私信。findLetterCount(String conversationId)
: 查詢某個會話中的私信數量。findLetterUnreadCount(int userId, String conversationId)
: 查詢某個會話中未讀私信的數量。addMessage(Message message)
: 添加一條消息,并進行 HTML 轉義和敏感詞過濾。readMessage(List<Integer> ids)
: 將消息標記為已讀。findLatestNotice(int userId, String topic)
: 查詢某個主題下最新的通知。findNoticeCount(int userId, String topic)
: 查詢某個主題下的通知數量。findNoticeUnreadCount(int userId, String topic)
: 查詢某個主題下未讀通知的數量。findNotices(int userId, String topic, int offset, int limit)
: 查詢某個主題下的通知列表,返回一定數量的通知。
該消息服務類通過調用消息映射器中的方法,將數據庫的讀取和寫入操作封裝成更高層次的服務方法,方便其他模塊調用并實現相關的業務邏輯。這些方法可以用于獲取會話列表、私信列表、未讀消息數量、最新通知等信息,并進行消息的添加和讀取操作。
最后設計controller層。
controller層太多了,就不寫了,因為這個功能其實也不是很重要,面試不怎么會問。