java网页版加好友功能实现思路
2017-12-10 15:13
295 查看
刚毕业出来工作,感觉像条咸鱼。。。,闲来无事就写了个基于SSM的在线影院的项目,想加个类似网页版QQ的功能,包括加好友及聊天。一直不知道存放好友的数据表(t_friend)该怎么设计?按传统的设计思路把数据表设计成一列表示用户名(userName),一列表示好友名(friendName),一一对应?虽然这样设计的好处是数据之间的关系一目了然,但是明显不适合用来存放好友数据,那A是B的好友,B必然是A的好友,一个好友关系要2条数据来表示,明显这样设计不合适。给某用户发出加好友请求后,等待该用户响应好友请求这段时间的数据状态该怎么设计?想了几天,决定将上述问题设计成两张数据表,一张是存放好友的数据表(t_friend),一张是存放加好友状态的数据表(t_addfriend).下面来看这两种表的设计: 好友表(t_friend)只有两个字段:friend_1,friend_2,我在这里就没设置主键了。在好友请求通过的情况下,friend_1表示主动发起好友请求的那一方,friend_2自然就是另一方。 加好友状态表(t_addfriend)有四个字段:friend_1,friend_2,f1_allow,f2_allow,同样没有主键。friend_1表示主动发起好友请求的那一方,friend_2自然就是另一方。f1_allow表示friend_1是否同意好友请求,friend_1表示主动发起好友请求,当然同意了。。。。(这个完全是为了数据的完整性),f2_allow表示friend_2是否同意好友请求。 两个实体类的设计是这样的: 好友类(Friend):currentUser表示当前用户,friendName表示当前用户的好友,这个是为了传数据的时候方便,否则怎么知道哪个是friend_1?哪个是friend_2?
public class Friend { private String friend_1; private String friend_2; private String currentUser; private String friendName; ...... }
加好友类(AddFriend):这里加了两个状态常量:ALLOW表示同意好友请求,DISALLOW表示不同意好友请求。
public class AddFriend { public static final String ALLOW="Y"; public static final String DISALLOW="N"; private String friend_1; private String friend_2; private String f1_allow; private String f2_allow; private String currentUser; private String friendName; ...... }
只有t_addfriend表中的f1_allow和f2_allow都为"Y"时,才将数据存入t_friend表。t_addfriend表主要的作用是临时存放好友请求状态,这个表后续有用处(主要是为了通知用户的好友请求是被同意还是被拒绝)。 下面来看程序:
1.我的好友
(1).mapper文件
<select id="findFriendByName" parameterType="java.lang.String" resultType="cn.tomato.domain.Friend"> select * from t_friend where friend_1=#{userName} or friend_2=#{userName} </select>
(2)Controller层逻辑
User curUser = (User) session.getAttribute("session_user"); String curUserName = curUser.getLoginName(); List<Friend> friendList = friendService.findFriendByName(curUserName); List<User> userList = new ArrayList<>(); for(Friend friend:friendList){ User user = null; if(friend.getFriend_1().equals(curUserName)){ user = userService.findUserByName(friend.getFriend_2()); }else{ user = userService.findUserByName(friend.getFriend_1()); } userList.add(user); }
2.加好友
(1).mapper文件
<sql id="key"> <trim suffixOverrides=","> <if test="friend_1!=null and friend_1!=''">friend_1,</if> <if test="friend_2!=null and friend_2!=''">friend_2,</if> <if test="f1_allow!=null and f1_allow!=''">f1_allow,</if> <if test="f2_allow!=null and f2_allow!=''">f2_allow,</if> </trim> </sql> <sql id="value"> <trim suffixOverrides=","> <if test="friend_1!=null and friend_1!=''">#{friend_1},</if> <if test="friend_2!=null and friend_2!=''">#{friend_2},</if> <if test="f1_allow!=null and f1_allow!=''">#{f1_allow},</if> <if test="f2_allow!=null and f2_allow!=''">#{f2_allow},</if> </trim> </sql> <insert id="addFriend" parameterType="cn.tomato.domain.AddFriend"> insert into t_addfriend(<include refid="key"/>) values(<include refid="value"/>) </insert>
(2)Controller层逻辑(数据从前端传过来,这里就不细说了)
@RequestMapping("/addFriend") public void addFriend(@RequestBody AddFriend friend,HttpServletResponse response) throws Exception{ friendService.addFriend(friend); String hintMessage="请求已发出!等待其接受!!"; JSONObject message = new JSONObject(); message.put("hintMessage", hintMessage); System.out.println(message.toJSONString()); response.setContentType("text/html;charset=UTF-8"); response.getWriter().println(message.toJSONString()); response.getWriter().close(); }
3.查看好友请求及请求响应(是同意还是拒绝)
(1).mapper文件
<select id="findFriendRequest" parameterType="java.lang.String" resultType="cn.tomato.domain.AddFriend"> select * from t_addfriend where friend_2=#{userName} and f2_allow is null </select> <select id="responseMessage" parameterType="java.lang.String" resultType="cn.tomato.domain.AddFriend"> select * from t_addfriend t where t.friend_1=#{userName} and t.f2_allow is not null </select> <delete id="delFriendRequest" parameterType="java.lang.String"> delete from t_addfriend where friend_1=#{userName} and f2_allow is not null </delete>
(2)Controller层逻辑(点击上图白色部分的div“我知道了”,删除临时表t_addfriend中的数据)
//侧边栏:发现 @RequestMapping("/found") public ModelAndView found(HttpSession session){ User curUser = (User) session.getAttribute("session_user"); String curUserName = curUser.getLoginName(); List<AddFriend> friendRequestList = friendService.findFriendRequest(curUserName); List<User> userList = new ArrayList<>(); for(AddFriend friend:friendRequestList){ User user = userService.findUserByName(friend.getFriend_1()); userList.add(user); } List<AddFriend> respMessageList = friendService.responseMessage(curUserName); ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("userList", userList); modelAndView.addObject("respMessageList", respMessageList); modelAndView.setViewName("found"); return modelAndView; }
4.同意或拒绝好友请求
(1).mapper文件
<update id="friendReqResp" parameterType="cn.tomato.domain.AddFriend"> update t_addfriend set f2_allow=#{f2_allow} where friend_2=#{friend_2} </update>
(2)Controller层逻辑
//同意“好友请求” @RequestMapping("/agreeReq") public void agreeReq(@RequestBody AddFriend addFriend,HttpServletResponse response) throws Exception{ friendService.friendReqResp(addFriend); Friend friend = new Friend(); friend.setFriend_1(addFriend.getFriend_1()); friend.setFriend_2(addFriend.getFriend_2()); friendService.allowFriendReq(friend); String hintMessage="相见恨晚,快去聊天吧!!"; JSONObject message = new JSONObject(); message.put("hintMessage", hintMessage); System.out.println(message.toJSONString()); response.setContentType("text/html;charset=UTF-8"); response.getWriter().println(message.toJSONString()); response.getWriter().close(); } //拒绝“好友请求” @RequestMapping("/rejectReq") public void rejectReq(@RequestBody AddFriend addFriend,HttpServletResponse response) throws Exception{ friendService.friendReqResp(addFriend); String hintMessage="铁石心肠,成大事者也!!"; JSONObject message = new JSONObject(); message.put("hintMessage", hintMessage); System.out.println(message.toJSONString()); response.setContentType("text/html;charset=UTF-8"); response.getWriter().println(message.toJSONString()); response.getWriter().close(); }
基本思路就是这样的,数据大多数都是通过ajax从前端传过来的。第一次写博客,水平有限,有不足之处请谅解!!!最近在学Activiti,苦于找不着Activiti与SSM框架整合的资料或视频,有资料的朋友求分享!!!
相关文章推荐
- PageRank_网页排名_MapReduceJava代码实现思路
- java 实现某个功能的思路
- js网页中的(运行代码)功能实现思路
- PHP如何实现嵌入网页功能思路
- java实现网页验证码功能
- Java实现网页截屏功能(图片下载功能)的几种方式(整理)
- java 关于项目导出功能实现过程中遇到的问题及思路历程
- 基于java实现的简单网页日历功能,有兴趣得可以把它转换到前端实现
- 添加好友功能实现思路
- Java实现网易163邮箱好友通讯录的解析功能(带源码)
- java在线聊天项目1.2版 ——开启多个客户端,分别实现数据库注册和登录功能后,成功登陆则登录框消失,好友列表窗出现
- Java实现的简单网页截屏功能示例
- java实现一个简单的HTTP服务器,带打开网页和计算功能
- java实现图片或文件的上传功能具体思路及代码
- 毕设1--利用Java实现网页的模板功能技术---简要了解
- js网页中的(运行代码)功能实现思路
- 用java实现一个简单的单用户登陆功能的思路
- 添加好友功能实现思路
- java servlet 几种页面跳转的方法,需要的朋友可以参考一下 在访问网页的过程中,页面自动刷新、跳转和重定向是经常用到的,这里就说说在Java中是如何实现这些功能的! 一、页面自动刷新
- 用asp.net实现把此文推荐给好友的功能