增加用户详情

This commit is contained in:
cyk
2023-07-04 11:21:58 +08:00
parent e6e811ebf8
commit 05756a88e4
6 changed files with 121 additions and 1 deletions

View File

@@ -40,4 +40,19 @@ public class commentDao {
}
return num;
}
//根据用户显示评论
public List<commentEntity> selectByUserId(int id){
List<commentEntity> commentEntityList ;
try {
Connection conn = JdbcUtils.getConnection();
QueryRunner runner = new QueryRunner();
String sql = "select * from comment where userId = ? 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

@@ -223,5 +223,17 @@ public class projectDao {
return num;
}
public List<projectEntity> retProjectByUserId(int id){
List<projectEntity> projectEntityList;
try {
Connection conn = JdbcUtils.getConnection();
QueryRunner runner = new QueryRunner();
String sql = "select * from project where isValid = 1 and userId= ?";
projectEntityList = runner.query(conn, sql, new BeanListHandler<>(projectEntity.class),id);
conn.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
return projectEntityList;
}
}

View File

@@ -66,4 +66,6 @@ public class UserService {
public int addLabel(String username,String str){return userDao.addLabel(username,str);}
public userEntity selectUserByName(String name ){return userDao.selectUserByName(name);}
public int selectIdByName(String name ){return userDao.selectIdByName(name);}
}

View File

@@ -43,4 +43,9 @@ public class commentService {
int userid = userDao.selectIdByName(username);
return commentDao.insertComment(userid,projectId,content,isUsed,commentTime,star);
}
public String selectByUserId(int id){
List<commentEntity> commentEntityList = commentDao.selectByUserId(id);
return JSONObject.toJSONString(commentEntityList);
}
}

View File

@@ -177,4 +177,8 @@ public class projectService {
return projectEntityList;
}
public String retProjectByUserId(int id){
List<projectEntity> projectEntityList = projectDao.retProjectByUserId(id);
return JSONObject.toJSONString(projectEntityList);
}
}

View File

@@ -0,0 +1,82 @@
package com.hellogithub.servlet;
import com.alibaba.fastjson.JSONObject;
import com.hellogithub.entity.userEntity;
import com.hellogithub.service.UserService;
import com.hellogithub.service.commentService;
import com.hellogithub.service.projectService;
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 jakarta.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
@WebServlet("/userDetail")
public class userDetailServlet extends HttpServlet {
private UserService userService=new UserService();
private projectService projectService= new projectService();
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");
PrintWriter writer = resp.getWriter();
HttpSession session = req.getSession();
userEntity userEntity=(userEntity)session.getAttribute("user");
if(userEntity == null){
setResultError("用户暂未登录", writer);
}else{
String name = userEntity.getUserName();
Map<String,String> dataMap= new HashMap<>();
int id = userService.selectIdByName(name);
dataMap.put("comment",commentService.selectByUserId(id));
dataMap.put("project",projectService.retProjectByUserId(id));
String jsonString = JSONObject.toJSONString(dataMap);
writer.println(jsonString);
}
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);
}
}