最近实现了评论和回复、点赞、@的功能。在这里分享一下我的设计思路(先分享评论和回复功能)。希望各位读者给出一些不一样的建议后期改进。

总共是两层回复 (回复评论、回复评论下的回复)
评论表(TFW_Comments)和回复内容表(TFW_UserResponse)以及评论回复关系表(TFW_MsgRelation)

注:各位读者自动忽略评论表的服务机构ID字段,这个字段相当于这条评论是在哪个帖子(文章下面)
1、根据文章ID或者是帖子ID查询评论表获取评论(本文的服务机构ID)。第一层(评论)
2、根据评论ID并且回复类型等于1的去关系表获取第二层的回复(commentsId)。第二层(评论下的回复)
3、根据评论ID、回复类型等于2、回复ID去关系表获取第三层回复。第三层(评论下回复中的回复)注:回复ID是它的上级
@Override
public Map<String, Object> findComments(JSONObject jsonObject) {
data.clear();
String userId = jsonObject.getString("userId");
String role = this.role(jsonObject);
if (role.equals("-1")){
//没有权限
data.put("error","-1");
data.put("msg","当前用户没有权限");
return data;
}
List<Map<String, Object>> info = commentsDao.findComment(jsonObject.getString("fWJLID"),null);
//查询点赞次数
int countTag = 0;
MsgRelationTag msgRelationTag = new MsgRelationTag();
for (Map item : info){
item.put("inputShow",false);
int commentsId = (int) item.get("commentsId");
//查询点赞次数
countTag = msgRelationDao.findCountTagByTagId(commentsId,1);
item.put("countTag",countTag);
//设置点赞状态
msgRelationTag.setTagId(commentsId);
msgRelationTag.setTagType(1);
msgRelationTag.setTagUserId(Integer.parseInt(userId));
MsgRelationTag msgTag = msgRelationDao.findMsgTag(msgRelationTag);
if (msgTag != null) {
item.put("tagStatus",msgTag.getStatus());
}else {
item.put("tagStatus","");
}
//如果有@id
if (item.get("atId") != null){
String content = item.get("content").toString();
StringBuffer tmrAtId = findUserName(item.get("atId").toString());
item.put("content",content+'@'+tmrAtId);
}
//二级回复数据
List<Map<String, Object>> twoReply = new ArrayList<>();
//所有数据
List<Map<String, Object>> userResponse = userResponseDao.findUserResponse(commentsId, null, "","",null);
for (Map userResponseInfo :userResponse){
int userResponseIds = Integer.parseInt(userResponseInfo.get("userResponseId").toString());
//查询点赞次数
countTag = msgRelationDao.findCountTagByTagId(userResponseIds,2);
//设置点赞状态
msgRelationTag.setTagId(userResponseIds);
msgRelationTag.setTagType(2);
msgTag = msgRelationDao.findMsgTag(msgRelationTag);
if (msgTag != null) {userResponseInfo.put("tagStatus",msgTag.getStatus());}else {userResponseInfo.put("tagStatus","");}
userResponseInfo.put("countTag",countTag);
userResponseInfo.put("inputShow",false);
Integer responseType = (Integer) userResponseInfo.get("responseType");
for (Map stairReplyInfo : userResponse){
Integer userResponseId = (Integer) stairReplyInfo.get("userResponseId");
int msgRelationId = Integer.parseInt(stairReplyInfo.get("msgRelationId").toString());
//接受者id*/
twoReply = userResponseDao.findUserResponse(msgRelationId, userResponseId,"1","",null); //二级回复数据
for (Map twoReplyItem : twoReply){
int twoReplyId = Integer.parseInt(twoReplyItem.get("userResponseId").toString());
twoReplyItem.put("inputShow",false);
//查询点赞次数
countTag = msgRelationDao.findCountTagByTagId(twoReplyId,2);
twoReplyItem.put("countTag",countTag);
//设置点赞状态
msgRelationTag.setTagId(twoReplyId);
msgTag = msgRelationDao.findMsgTag(msgRelationTag);
if (msgTag != null) {twoReplyItem.put("tagStatus",msgTag.getStatus());}else {twoReplyItem.put("tagStatus","");}
String userRepContent = twoReplyItem.get("userRepContent").toString();
if (twoReplyItem.get("tmrAtId") != null){
StringBuffer tmrAtId = findUserName(twoReplyItem.get("tmrAtId").toString());
twoReplyItem.put("userRepContent",userRepContent+'@'+tmrAtId);
}
}
stairReplyInfo.put("twoReply",twoReply);
}
}
item.put("stairReply",userResponse);
}
data.put("data",info);
data.put("error",0);
data.put("msg","查询成功");
return data;
}
其它的代码可以忽略。主要语句有:
List<Map<String, Object>> info = commentsDao.findComment(jsonObject.getString("fWJLID"),null);
上图根据FWJLID获取评论。(此处可以当成帖子的ID,获取帖子下的评论)一级展示
对应SQL语句(OPT是我的用户表)
select tc.content ,tc.commentsId,convert(varchar(19),tc.startTime,120) as startTime,tc.recipientId ,tc.operatorId,zo.NAME as operatorName,tc.atId,zo.HeadImgUrl as operatorHeadImgUrl
from TFW_Comments tc
left join zd_opt zo on zo.AID = tc.operatorId where tc.FWJLID = 5101
查询结果:

List<Map<String, Object>> userResponse = userResponseDao.findUserResponse(commentsId, null, "","",null);
上图根据commentsid获取评论下的回复。(根据评论ID获取回复)二级展示
对应sql语句
select
tur.userResponseId,tur.operatorId,tur.recipientId,convert(varchar(19),tur.startTime,120) as startTime,tur.userRepContent,tmr.atId as tmrAtId,
tmr.msgRelationId ,tmr.responseType,tmr.replyId,
zo.NAME as operatorName,
zo1.NAME as recipientName,
zo.HeadImgUrl as operatorHeadImgUrl,
zo1.HeadImgUrl as recipientHeadImgUrl
from TFW_MsgRelation tmr
left join TFW_UserResponse tur on tur.userResponseId = tmr.userResponseId
left join zd_opt zo on zo.AID = tur.operatorId
left join zd_opt zo1 on zo1.AID = tur.recipientId where tmr.commentsId = 47
查询结果

twoReply = userResponseDao.findUserResponse(msgRelationId, userResponseId,"1","",null); //二级回复数据
上图是根据评论ID(msgRelationId)和回复ID(userResponseId)去获取二级回复。回复ID也就是父类。就是回复那一条回复的ID。 第三层展示
对应sql
select
tur.userResponseId,tur.operatorId,tur.recipientId,convert(varchar(19),tur.startTime,120) as startTime,tur.userRepContent,tmr.atId as tmrAtId,
tmr.msgRelationId ,tmr.responseType,tmr.replyId,
zo.NAME as operatorName,
zo1.NAME as recipientName,
zo.HeadImgUrl as operatorHeadImgUrl,
zo1.HeadImgUrl as recipientHeadImgUrl
from TFW_MsgRelation tmr
left join TFW_UserResponse tur on tur.userResponseId = tmr.userResponseId
left join zd_opt zo on zo.AID = tur.operatorId
left join zd_opt zo1 on zo1.AID = tur.recipientId where tmr.commentsId = 136 and tmr.replyId = 155
查询结果

返回页面展示和返回体展示


我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
我将应用程序升级到Rails4,一切正常。我可以登录并转到我的编辑页面。也更新了观点。使用标准View时,用户会更新。但是当我添加例如字段:name时,它不会在表单中更新。使用devise3.1.1和gem'protected_attributes'我需要在设备或数据库上运行某种更新命令吗?我也搜索过这个地方,找到了许多不同的解决方案,但没有一个会更新我的用户字段。我没有添加任何自定义字段。 最佳答案 如果您想允许额外的参数,您可以在ApplicationController中使用beforefilter,因为Rails4将参数
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
只是想确保我理解了事情。据我目前收集到的信息,Cucumber只是一个“包装器”,或者是一种通过将事物分类为功能和步骤来组织测试的好方法,其中实际的单元测试处于步骤阶段。它允许您根据事物的工作方式组织您的测试。对吗? 最佳答案 有点。它是一种组织测试的方式,但不仅如此。它的行为就像最初的Rails集成测试一样,但更易于使用。这里最大的好处是您的session在整个Scenario中保持透明。关于Cucumber的另一件事是您(应该)从使用您的代码的浏览器或客户端的角度进行测试。如果您愿意,您可以使用步骤来构建对象和设置状态,但通常您
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我意识到这可能是一个非常基本的问题,但我现在已经花了几天时间回过头来解决这个问题,但出于某种原因,Google就是没有帮助我。(我认为部分问题在于我是一个初学者,我不知道该问什么......)我也看过O'Reilly的RubyCookbook和RailsAPI,但我仍然停留在这个问题上.我找到了一些关于多态关系的信息,但它似乎不是我需要的(尽管如果我错了请告诉我)。我正在尝试调整MichaelHartl'stutorial创建一个包含用户、文章和评论的博客应用程序(不使用脚手架)。我希望评论既属于用户又属于文章。我的主要问题是:我不知道如何将当前文章的ID放入评论Controller。
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht
这篇文章是继上一篇文章“Observability:从零开始创建Java微服务并监控它(一)”的续篇。在上一篇文章中,我们讲述了如何创建一个Javaweb应用,并使用Filebeat来收集应用所生成的日志。在今天的文章中,我来详述如何收集应用的指标,使用APM来监控应用并监督web服务的在线情况。源码可以在地址 https://github.com/liu-xiao-guo/java_observability 进行下载。摄入指标指标被视为可以随时更改的时间点值。当前请求的数量可以改变任何毫秒。你可能有1000个请求的峰值,然后一切都回到一个请求。这也意味着这些指标可能不准确,你还想提取最小/
HashMap中为什么引入红黑树,而不是AVL树呢1.概述开始学习这个知识点之前我们需要知道,在JDK1.8以及之前,针对HashMap有什么不同。JDK1.7的时候,HashMap的底层实现是数组+链表JDK1.8的时候,HashMap的底层实现是数组+链表+红黑树我们要思考一个问题,为什么要从链表转为红黑树呢。首先先让我们了解下链表有什么不好???2.链表上述的截图其实就是链表的结构,我们来看下链表的增删改查的时间复杂度增:因为链表不是线性结构,所以每次添加的时候,只需要移动一个节点,所以可以理解为复杂度是N(1)删:算法时间复杂度跟增保持一致查:既然是非线性结构,所以查询某一个节点的时候