From c5dab5c31620fad6ee1588579dd9425880a1eb2f Mon Sep 17 00:00:00 2001 From: User_cyk <1020691186@qq.com> Date: Mon, 3 Jul 2023 12:12:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=98=BE=E7=A4=BA=E6=89=80=E6=9C=89=E8=AF=84?= =?UTF-8?q?=E8=AE=BA=EF=BC=8C=E4=BF=AE=E6=94=B9userEntity=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/hellogithub/dao/UserDao.java | 17 +++++ src/com/hellogithub/dao/commentDao.java | 29 +++++++++ src/com/hellogithub/entity/commentEntity.java | 17 +++-- src/com/hellogithub/entity/userEntity.java | 8 +-- .../hellogithub/service/commentService.java | 39 ++++++++++++ .../servlet/selectCommentByProServlet.java | 63 +++++++++++++++++++ 6 files changed, 165 insertions(+), 8 deletions(-) create mode 100644 src/com/hellogithub/dao/commentDao.java create mode 100644 src/com/hellogithub/service/commentService.java create mode 100644 src/com/hellogithub/servlet/selectCommentByProServlet.java diff --git a/src/com/hellogithub/dao/UserDao.java b/src/com/hellogithub/dao/UserDao.java index 4267e3c..20f05cc 100644 --- a/src/com/hellogithub/dao/UserDao.java +++ b/src/com/hellogithub/dao/UserDao.java @@ -250,4 +250,21 @@ public class UserDao { return num; } + + public String selectNameById(int id) + { + String label; + userEntity userEntity; + try { + Connection conn = JdbcUtils.getConnection(); + QueryRunner runner = new QueryRunner(); + String sql = "select * from user where userId=? and isValid = 1"; + userEntity = runner.query(conn, sql, new BeanHandler<>(userEntity.class),id); + label = userEntity.getUserName(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + + return label; + } } diff --git a/src/com/hellogithub/dao/commentDao.java b/src/com/hellogithub/dao/commentDao.java new file mode 100644 index 0000000..a0750f3 --- /dev/null +++ b/src/com/hellogithub/dao/commentDao.java @@ -0,0 +1,29 @@ +package com.hellogithub.dao; + +import com.hellogithub.entity.categoryEntity; +import com.hellogithub.entity.commentEntity; +import com.hellogithub.utils.JdbcUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +public class commentDao { + + //显示评论 + public List selectByProjectId(String id){ + List commentEntityList ; + try { + Connection conn = JdbcUtils.getConnection(); + QueryRunner runner = new QueryRunner(); + String sql = "select * from comment where projectId = ? and isValid = 1"; + commentEntityList = runner.query(conn, sql,new BeanListHandler<>(commentEntity.class),id); + conn.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + return commentEntityList; + } +} diff --git a/src/com/hellogithub/entity/commentEntity.java b/src/com/hellogithub/entity/commentEntity.java index d247946..6e08384 100644 --- a/src/com/hellogithub/entity/commentEntity.java +++ b/src/com/hellogithub/entity/commentEntity.java @@ -1,6 +1,5 @@ package com.hellogithub.entity; -import java.util.Date; public class commentEntity { int commentId; @@ -8,7 +7,7 @@ public class commentEntity { int projectId; String content; int isUsed; - Date commentTime; + String commentTime; int isValid; public int getCommentId() { @@ -51,11 +50,11 @@ public class commentEntity { this.isUsed = isUsed; } - public Date getCommentTime() { + public String getCommentTime() { return commentTime; } - public void setCommentTime(Date commentTime) { + public void setCommentTime(String commentTime) { this.commentTime = commentTime; } @@ -66,4 +65,14 @@ public class commentEntity { public void setIsValid(int isValid) { this.isValid = isValid; } + + int star; + + public int getStar() { + return star; + } + + public void setStar(int star) { + this.star = star; + } } diff --git a/src/com/hellogithub/entity/userEntity.java b/src/com/hellogithub/entity/userEntity.java index 823af4a..2d2c9e3 100644 --- a/src/com/hellogithub/entity/userEntity.java +++ b/src/com/hellogithub/entity/userEntity.java @@ -22,7 +22,7 @@ public class userEntity { private String userName; private String userPassword; - private Date creatTime; + private String creatTime; private Integer isValid; @@ -41,7 +41,7 @@ public class userEntity { this.userPassword = userPassword; } - public userEntity(Integer userId, String userName, String userPassword, Date creatTime, Integer isValid, Integer projectNum, String searchHistory, Integer contributionValue) { + public userEntity(Integer userId, String userName, String userPassword, String creatTime, Integer isValid, Integer projectNum, String searchHistory, Integer contributionValue) { this.userId = userId; this.userName = userName; this.userPassword = userPassword; @@ -76,11 +76,11 @@ public class userEntity { this.userPassword = userPassword; } - public Date getCreatTime() { + public String getCreatTime() { return creatTime; } - public void setCreatTime(Date creatTime) { + public void setCreatTime(String creatTime) { this.creatTime = creatTime; } diff --git a/src/com/hellogithub/service/commentService.java b/src/com/hellogithub/service/commentService.java new file mode 100644 index 0000000..b7a439a --- /dev/null +++ b/src/com/hellogithub/service/commentService.java @@ -0,0 +1,39 @@ +package com.hellogithub.service; + +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import com.hellogithub.dao.UserDao; +import com.hellogithub.dao.commentDao; +import com.hellogithub.entity.commentEntity; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class commentService { + private commentDao commentDao =new commentDao(); + private UserDao userDao = new UserDao(); + //显示所有评论 + public String selectByProjectId(String id){ + List commentEntityList; + Map dataMap = new HashMap<>(); + Map> dataMap2 = new HashMap<>(); + commentEntityList = commentDao.selectByProjectId(id); + for(int i=0;i(); + int userid = commentEntity.getUserId(); + String username = userDao.selectNameById(userid); + String time = commentEntity.getCommentTime(); + String content = commentEntity.getContent(); + int star = commentEntity.getStar(); + dataMap.put("username",username); + dataMap.put("time",time); + dataMap.put("content",content); + dataMap.put("star",String.valueOf(star)); + dataMap2.put(String.valueOf(i),dataMap); + } + return JSONArray.toJSONString(dataMap2); + } +} diff --git a/src/com/hellogithub/servlet/selectCommentByProServlet.java b/src/com/hellogithub/servlet/selectCommentByProServlet.java new file mode 100644 index 0000000..0d9f1a2 --- /dev/null +++ b/src/com/hellogithub/servlet/selectCommentByProServlet.java @@ -0,0 +1,63 @@ +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; +import java.util.HashMap; + +@WebServlet("/selectCommentByPro") +public class selectCommentByProServlet 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"); + PrintWriter writer = resp.getWriter(); + writer.println(commentService.selectByProjectId(id)); + writer.close(); + } + public void setResult(Integer code, String msg, PrintWriter writer) { + HashMap result = new HashMap<>(); + result.put("code", code); + result.put("msg", msg); + String jsonString = JSONObject.toJSONString(result); + writer.println(jsonString); + writer.close(); + } + + public void setResultError(String msg, PrintWriter writer) { + setResult(500, msg, writer); + } + + public void setResultOK(String msg, PrintWriter writer) { + setResult(200, msg, writer); + } +}