显示所有评论,修改userEntity字段类型

This commit is contained in:
cyk
2023-07-03 12:12:42 +08:00
parent 87abd49bc4
commit c5dab5c316
6 changed files with 165 additions and 8 deletions

View File

@@ -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;
}
}

View File

@@ -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<commentEntity> selectByProjectId(String id){
List<commentEntity> 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;
}
}

View File

@@ -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;
}
}

View File

@@ -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;
}

View File

@@ -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<commentEntity> commentEntityList;
Map<String,String> dataMap = new HashMap<>();
Map<String,Map<String,String>> dataMap2 = new HashMap<>();
commentEntityList = commentDao.selectByProjectId(id);
for(int i=0;i<commentEntityList.size();i++)
{
commentEntity commentEntity = commentEntityList.get(i);
dataMap = new HashMap<>();
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);
}
}

View File

@@ -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<String, Object> 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);
}
}