点赞排序
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -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;
|
||||
}
|
||||
}
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
||||
|
48
src/com/hellogithub/servlet/selectCommentByServlet.java
Normal file
48
src/com/hellogithub/servlet/selectCommentByServlet.java
Normal 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();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user