点赞排序

This commit is contained in:
cyk
2023-07-05 13:05:57 +08:00
parent 9bfd4e29e3
commit cfca7113ef
4 changed files with 90 additions and 1 deletions

View File

@@ -32,7 +32,7 @@ public class commentDao {
try {
Connection conn = JdbcUtils.getConnection();
QueryRunner runner = new QueryRunner();
String sql = "INSERT INTO comment (userId,projectId,content,isUsed,commentTime,isValid,star) VALUES (?,?,?,?,?,1,?)";
String sql = "INSERT INTO comment (userId,projectId,content,isUsed,commentTime,isValid,star,likeNum) VALUES (?,?,?,?,?,1,?,0)";
num = runner.update(conn, sql,userid,projectId,content,isUsed,commentTime,star);
conn.close();
} catch (SQLException e) {
@@ -55,4 +55,20 @@ public class commentDao {
}
return commentEntityList;
}
public List<commentEntity> selectByTrait(String id,String trait)
{
List<commentEntity> commentEntityList ;
try {
Connection conn = JdbcUtils.getConnection();
QueryRunner runner = new QueryRunner();
String sql = "select * from comment where projectId = ? and isValid = 1 Order BY ?";
commentEntityList = runner.query(conn, sql,new BeanListHandler<>(commentEntity.class),id,trait);
conn.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
return commentEntityList;
}
}

View File

@@ -75,4 +75,14 @@ public class commentEntity {
public void setStar(int star) {
this.star = star;
}
int likeCount;
public int getLikeCount() {
return likeCount;
}
public void setLikeCount(int likeCount) {
this.likeCount = likeCount;
}
}

View File

@@ -48,4 +48,19 @@ public class commentService {
List<commentEntity> commentEntityList = commentDao.selectByUserId(id);
return JSONObject.toJSONString(commentEntityList);
}
public String selectByTrait(String id,String trait)
{
List<commentEntity> commentEntityList = commentDao.selectByTrait(id,trait);
if(trait.equals("last"))
{
commentEntityList = commentDao.selectByTrait(id,"creatTime");
}
if(trait.equals("hot"))
{
commentEntityList = commentDao.selectByTrait(id,"likeNum");
}
return JSONObject.toJSONString(commentEntityList);
}
}

View File

@@ -0,0 +1,48 @@
package com.hellogithub.servlet;
import com.alibaba.fastjson.JSONObject;
import com.hellogithub.service.commentService;
import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/selectCommentBy")
public class selectCommentByServlet extends HttpServlet {
private commentService commentService = new commentService();
@Override
public void init(ServletConfig config) throws ServletException {
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html; charset=utf-8");
// 设置响应头允许ajax跨域访问
String curOrigin = req.getHeader("Origin");
resp.setHeader("Access-Control-Allow-Origin", curOrigin == null ? "true" : curOrigin);
resp.setHeader("Access-Control-Allow-Credentials", "true");
resp.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, HEAD");
resp.setHeader("Access-Control-Max-Age", "3600");
resp.setHeader("Access-Control-Allow-Headers", "access-control-allow-origin, authority, content-type, version-info, X-Requested-With");
resp.setContentType("application/json;charset=UTF-8");
String id = req.getParameter("id");
String trait = req.getParameter("trait");
PrintWriter writer = resp.getWriter();
writer.println(commentService.selectByTrait(id,trait));
writer.close();
}
}