文章目录

  • 显示私信
  • 1.entity包
  • 2.dao层
  • 3.service层
  • 4.controller层
  • 5.View层
  • 5.1 index.html
  • 5.2 letter.html
  • 5.3 letter-detail.html
  • 6.功能测试


显示私信

先实现显示私信功能,再完成添加私信功能。

JAVA 个人账号给好友发消息 java实现私信功能_java

1.entity包

添加Message实体类。

package com.gerrard.community.entity;

import java.util.Date;

public class Message {

    private int id;
    private int fromId;
    private int toId;
    private String conversationId;
    private String content;
    private int status;
    private Date createTime;
    
}

JAVA 个人账号给好友发消息 java实现私信功能_数据库_02

status:0-未读;1-已读;2-删除;

先探究一下conversation_id字段的必要性:

SELECT MAX(id),conversation_id FROM message
            WHERE STATUS != 2
            AND from_id != 1
            GROUP BY conversation_id

SELECT COUNT(*)FROM message
            WHERE STATUS != 2
            AND from_id != 1
            GROUP BY conversation_id     
            
SELECT  COUNT(*) FROM 
(SELECT COUNT(*) FROM message
            WHERE STATUS != 2
            AND from_id != 1
            GROUP BY conversation_id 
 )aa

查询结果:

JAVA 个人账号给好友发消息 java实现私信功能_spring_03

JAVA 个人账号给好友发消息 java实现私信功能_spring_04

JAVA 个人账号给好友发消息 java实现私信功能_sql_05

不加conversation_id:

SELECT MAX(id),from_id,to_id FROM message
            WHERE STATUS != 2
            AND from_id != 1
            GROUP BY from_id,to_id
            
SELECT COUNT(*)FROM message
            WHERE STATUS != 2
            AND from_id != 1
            GROUP BY from_id,to_id
            
SELECT  COUNT(*) FROM 
(SELECT COUNT(*) FROM message
            WHERE STATUS != 2
            AND from_id != 1
            GROUP BY from_id,to_id 
 )aa

JAVA 个人账号给好友发消息 java实现私信功能_sql_06

JAVA 个人账号给好友发消息 java实现私信功能_java_07

JAVA 个人账号给好友发消息 java实现私信功能_sql_08

可以发现上述查询结果不同,事实上,对于message中的记录A和记录B,不管from_id和to_id的先后顺序,只要二者关于from_id和to_id构成的集合(集合满足无序性){from_id,to_id}一致,则记录A和记录B就在同一个会话中,因此有必要构造conversation_id字段,方便sql的查询,将相关逻辑上沉至Java中编写,否则就需在sql端中编写判断在同一个会话的逻辑,较为繁琐。

2.dao层

添加MessageMapper类。

package com.gerrard.community.dao;

import com.gerrard.community.entity.Message;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@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);

}

message-mapper.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.gerrard.community.dao.MessageMapper">

    <sql id="selectFields">
        id, from_id, to_id, conversation_id, content, status, create_time
    </sql>

    <select id="selectConversations" resultType="Message">
        select <include refid="selectFields"></include>
        from message
        where id in (
            select max(id) from message
            where status != 2
            and from_id != 1
            and (from_id = #{userId} or to_id = #{userId})
            group by conversation_id
        )
        order by id desc
        limit #{offset}, #{limit}
    </select>

    <select id="selectConversationCount" resultType="int">
        select count(m.maxid) from (
            select max(id) as maxid from message
            where status != 2
            and from_id != 1
            and (from_id = #{userId} or to_id = #{userId})
            group by conversation_id
        ) as m
    </select>

    <select id="selectLetters" resultType="Message">
        select <include refid="selectFields"></include>
        from message
        where status != 2
        and from_id != 1
        and conversation_id = #{conversationId}
        order by id desc
        limit #{offset}, #{limit}
    </select>

    <select id="selectLetterCount" resultType="int">
        select count(id)
        from message
        where status != 2
        and from_id != 1
        and conversation_id = #{conversationId}
    </select>

    <select id="selectLetterUnreadCount" resultType="int">
        select count(id)
        from message
        where status = 0
        and from_id != 1
        and to_id = #{userId}
        <if test="conversationId!=null">
            and conversation_id = #{conversationId}
        </if>
    </select>

</mapper>

3.service层

添加MessageService类。

package com.gerrard.community.service;

import com.gerrard.community.dao.MessageMapper;
import com.gerrard.community.entity.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MessageService {

    @Autowired
    private MessageMapper messageMapper;

    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);
    }

}

4.controller层

添加MessageController类。

package com.gerrard.community.controller;

import com.gerrard.community.entity.Message;
import com.gerrard.community.entity.Page;
import com.gerrard.community.entity.User;
import com.gerrard.community.service.MessageService;
import com.gerrard.community.service.UserService;
import com.gerrard.community.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class MessageController {

    @Autowired
    private MessageService messageService;

    @Autowired
    private HostHolder hostHolder;

    @Autowired
    private UserService userService;

    // 私信列表
    @RequestMapping(path = "/letter/list", method = RequestMethod.GET)
    public String getLetterList(Model model, Page page) {
        User user = hostHolder.getUser();
        // 分页信息
        page.setLimit(5);
        page.setPath("/letter/list");
        page.setRows(messageService.findConversationCount(user.getId()));

        // 会话列表
        List<Message> conversationList = messageService.findConversations(
                user.getId(), page.getOffset(), page.getLimit());
        List<Map<String, Object>> conversations = new ArrayList<>();
        if (conversationList != null) {
            for (Message message : conversationList) {
                Map<String, Object> map = new HashMap<>();
                map.put("conversation", message);
                map.put("letterCount", messageService.findLetterCount(message.getConversationId()));
                map.put("unreadCount", messageService.findLetterUnreadCount(user.getId(), message.getConversationId()));
                int targetId = user.getId() == message.getFromId() ? message.getToId() : message.getFromId();
                map.put("target", userService.findUserById(targetId));

                conversations.add(map);
            }
        }
        model.addAttribute("conversations", conversations);

        // 查询未读消息数量
        int letterUnreadCount = messageService.findLetterUnreadCount(user.getId(), null);
        model.addAttribute("letterUnreadCount", letterUnreadCount);

        return "/site/letter";
    }

    @RequestMapping(path = "/letter/detail/{conversationId}", method = RequestMethod.GET)
    public String getLetterDetail(@PathVariable("conversationId") String conversationId, Page page, Model model) {
        // 分页信息
        page.setLimit(5);
        page.setPath("/letter/detail/" + conversationId);
        page.setRows(messageService.findLetterCount(conversationId));

        // 私信列表
        List<Message> letterList = messageService.findLetters(conversationId, page.getOffset(), page.getLimit());
        List<Map<String, Object>> letters = new ArrayList<>();
        if (letterList != null) {
            for (Message message : letterList) {
                Map<String, Object> map = new HashMap<>();
                map.put("letter", message);
                map.put("fromUser", userService.findUserById(message.getFromId()));
                letters.add(map);
            }
        }
        model.addAttribute("letters", letters);

        // 私信目标
        model.addAttribute("target", getLetterTarget(conversationId));

        return "/site/letter-detail";
    }

    private User getLetterTarget(String conversationId) {
        String[] ids = conversationId.split("_");
        int id0 = Integer.parseInt(ids[0]);
        int id1 = Integer.parseInt(ids[1]);

        if (hostHolder.getUser().getId() == id0) {
            return userService.findUserById(id1);
        } else {
            return userService.findUserById(id0);
        }
    }

}

getLetterList:

用户登录后在index主页访问私信列表:

1.先通过hostHolder得到登录用户实体,再设置分页信息;

2.查message数据库获得会话列表【每个会话最新的消息】,再对其做数据增强,针对每一个会话,添加”letterCount“,”unreadCount“,”targetId“【对话双方中的非登录用户】信息,封装在conversations列表中;

3.查询总的未读消息数量;

4.最后传给letter.html页面。

getLetterDetail:

用户点击会话消息的内容,进入会话详情页面:

1.先设置分页信息;

2.再根据conversationId查到消息列表,对其进行数据增强,针对每条消息,添加”fromUser“信息,封装在letters列表中;

3.查询”target“【对话双方中的非登录用户】信息【这里编写了getLetterTarget函数,其实按照getLetterList设置targetId的逻辑也可以做,取letterList中的Message实体,根据其fromId和targetId进行判断。一个是利用conversationId信息,一个是利用Message类的fromId和targetId信息】;

4.最后传给letter-detail.html页面。

5.View层

5.1 index.html

链到/letter/list:


JAVA 个人账号给好友发消息 java实现私信功能_sql_09

5.2 letter.html

JAVA 个人账号给好友发消息 java实现私信功能_JAVA 个人账号给好友发消息_10

JAVA 个人账号给好友发消息 java实现私信功能_数据库_11

JAVA 个人账号给好友发消息 java实现私信功能_spring_12

关键修改:

JAVA 个人账号给好友发消息 java实现私信功能_JAVA 个人账号给好友发消息_13

JAVA 个人账号给好友发消息 java实现私信功能_java_14

5.3 letter-detail.html

JAVA 个人账号给好友发消息 java实现私信功能_java_15

JAVA 个人账号给好友发消息 java实现私信功能_JAVA 个人账号给好友发消息_16

关键修改:

JAVA 个人账号给好友发消息 java实现私信功能_JAVA 个人账号给好友发消息_17

JAVA 个人账号给好友发消息 java实现私信功能_java_18

JAVA 个人账号给好友发消息 java实现私信功能_数据库_19

6.功能测试

JAVA 个人账号给好友发消息 java实现私信功能_sql_20

JAVA 个人账号给好友发消息 java实现私信功能_sql_21

JAVA 个人账号给好友发消息 java实现私信功能_数据库_22