操作的相关表有:评论表comment,用户表user,点赞表like
如果用户对哪条评论点赞,在like表添加对应记录,取消点赞则删除点赞记录。(两种情况)
1.访问点赞页面,遍历查询用户对哪条评论点过赞
底层采用的查询语句是采用 left jion on
String sql="SELECT `comment`.*, `like`.COMMENT_ID,`like`.USER_ID uid ,`like`.ID likeid,`user`.* from comment LEFT JOIN `like` ON `comment`.DELETE_STATUS=1 and `comment`.ID=`like`.COMMENT_ID AND `like`.USER_ID="+userid+" LEFT JOIN `user` ON `user`.ID=`comment`.USER_ID where `comment`.ITEM_ID="+itemid+" ORDER BY comment.CREATE_TIME DESC LIMIT "+pager.getStart()+ ","+pager.getPageSize();
jsp遍历
${comment1.NICK_NAME}
${comment1.COMMENT_INFO}
2.ajax 实现点赞和取消点赞
//取消赞
//===============================================
function quxiao(index){
var nu=0;
$('#zhan_yes'+index).hide();
$('#zhan_no'+index).show();
$('#zhan_yess'+index).hide();
$('#zhan_noo'+index).show();
//$('#span'+index).html(114);
var pan=$('#span'+index).text();
$.ajax({
type : "GET",
url : "${APP_PATH}/fore/like.cs",
data :{'commentid':index,'num':nu},
dataType: "json",
success:function(data) {
if (data && data.success == "true") {
if(data.status=='0'){
pan--;
$('#span'+index).html(pan);
$('#spann'+index).html(pan);
}
}
},
error:function(d, errorThrown) {// 失败的时候要执行的函数
alert("失败");
}
});
}
//点赞
function dianzan(index){
var nu=1;
$('#zhan_no'+index).hide();
$('#zhan_yes'+index).show();
$('#zhan_noo'+index).hide();
$('#zhan_yess'+index).show();
//$('#span'+index).html(115);
var pan=$('#span'+index).text();
// alert(pan);
$.ajax({
type : "GET",
url : "${APP_PATH}/fore/like.cs",
data :{'commentid':index,'num':nu},
dataType: "json",
success:function(data) {
if (data && data.success == "true") {
if(data.status=='1'){
pan++;
$('#span'+index).html(pan);
$('#spann'+index).html(pan);
}
}
},
error:function(d, errorThrown) {// 失败的时候要执行的函数
alert("失败");
}
});
}
3,controlle层处理请求
//点赞
@ResponseBody
@RequestMapping("/like")
public Map like(int commentid,HttpSession session,int num){
Map map = new HashMap();
if(num==1||num==0){
int userid= (int) session.getAttribute("userId");
commentService.updateLikes(num,commentid,userid);
}
if(num==0){
map.put("status", 0);
}if(num==1){
//加一
map.put("status", 1);
}
map.put("success", "true");
return map;
}