From ea45ddd7f1c92c063f2d41f2732d267ee26d92a8 Mon Sep 17 00:00:00 2001 From: User_cyk <1020691186@qq.com> Date: Wed, 5 Jul 2023 11:23:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E7=94=A8=E6=88=B7=E4=B8=BB?= =?UTF-8?q?=E9=A1=B5=E6=9F=A5=E8=AF=A2=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/hellogithub/dao/supportDao.java | 59 +++++++++++++++++++ src/com/hellogithub/entity/supportEntity.java | 41 +++++++++++++ .../hellogithub/service/supprotService.java | 49 +++++++++++++++ .../servlet/userDetailServlet.java | 17 +++--- 4 files changed, 158 insertions(+), 8 deletions(-) create mode 100644 src/com/hellogithub/dao/supportDao.java create mode 100644 src/com/hellogithub/entity/supportEntity.java create mode 100644 src/com/hellogithub/service/supprotService.java diff --git a/src/com/hellogithub/dao/supportDao.java b/src/com/hellogithub/dao/supportDao.java new file mode 100644 index 0000000..d311acc --- /dev/null +++ b/src/com/hellogithub/dao/supportDao.java @@ -0,0 +1,59 @@ +package com.hellogithub.dao; + +import com.hellogithub.entity.projectEntity; +import com.hellogithub.entity.supportEntity; +import com.hellogithub.utils.JdbcUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanHandler; + +import java.sql.Connection; +import java.sql.SQLException; + +public class supportDao { + public supportEntity selectSupport(int userId,int projectId) + { + int num; + supportEntity supportEntity =null; + try{ + Connection conn = JdbcUtils.getConnection(); + QueryRunner runner = new QueryRunner(); + String sql = "select * from support where isValid = 1 and userId = ? and projectId = ?"; + supportEntity = runner.query(conn, sql,new BeanHandler<>(supportEntity.class) , userId,projectId); + conn.close(); + }catch (SQLException e){ + throw new RuntimeException(e); + } + + return supportEntity; + } + public int insertSupport(int userId,int projectId) + { + int num; + try{ + Connection conn = JdbcUtils.getConnection(); + QueryRunner runner = new QueryRunner(); + String sql = "insert into support (userId,projectId,isValid) VALUES (?,?,1) "; + num = runner.update(conn, sql, userId,projectId); + conn.close(); + }catch (SQLException e){ + throw new RuntimeException(e); + } + + return num; + } + public int deleteSupport(int userId,int projectId) + { + int num; + try{ + Connection conn = JdbcUtils.getConnection(); + QueryRunner runner = new QueryRunner(); + String sql = "delete from support where isValid = 1 and userId = ? and projectId =?"; + num = runner.update(conn, sql , userId,projectId); + conn.close(); + }catch (SQLException e){ + throw new RuntimeException(e); + } + + return num; + } +} diff --git a/src/com/hellogithub/entity/supportEntity.java b/src/com/hellogithub/entity/supportEntity.java new file mode 100644 index 0000000..e88de74 --- /dev/null +++ b/src/com/hellogithub/entity/supportEntity.java @@ -0,0 +1,41 @@ +package com.hellogithub.entity; + +public class supportEntity { + int userId; + int projectId; + int isValid; + + int id; + + public int getUserId() { + return userId; + } + + public void setUserId(int userId) { + this.userId = userId; + } + + public int getProjectId() { + return projectId; + } + + public void setProjectId(int projectId) { + this.projectId = projectId; + } + + public int getIsValid() { + return isValid; + } + + public void setIsValid(int isValid) { + this.isValid = isValid; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } +} diff --git a/src/com/hellogithub/service/supprotService.java b/src/com/hellogithub/service/supprotService.java new file mode 100644 index 0000000..ecafae2 --- /dev/null +++ b/src/com/hellogithub/service/supprotService.java @@ -0,0 +1,49 @@ +package com.hellogithub.service; + +import com.hellogithub.dao.supportDao; +import com.hellogithub.entity.supportEntity; + +public class supprotService { + private supportDao supportDao = new supportDao(); + + public int retNum(int userId,int projectId){ + int num ; + supportEntity supportEntity = supportDao.selectSupport(userId,projectId); + if(supportEntity == null) + { + num =0; + } + else{ + num = 1; + } + return num ; + } + + public String insertSupport(int userId , int projectId) + { + String tip; + int num = supportDao.insertSupport(userId,projectId); + if(num == 1) + { + tip="点赞成功"; + } + else{ + tip="点赞失败"; + } + return tip; + } + + public String deleteSupport(int userId , int projectId) + { + String tip; + int num = supportDao.deleteSupport(userId,projectId); + if(num == 1) + { + tip="删除成功"; + } + else{ + tip="删除失败"; + } + return tip; + } +} diff --git a/src/com/hellogithub/servlet/userDetailServlet.java b/src/com/hellogithub/servlet/userDetailServlet.java index 10ea0e5..78a4e8e 100644 --- a/src/com/hellogithub/servlet/userDetailServlet.java +++ b/src/com/hellogithub/servlet/userDetailServlet.java @@ -47,20 +47,21 @@ public class userDetailServlet extends HttpServlet { 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); + String jsonString=""; + String username = req.getParameter("username"); + jsonString = JSONObject.toJSONString(userService.selectUserByName(username)); + if(jsonString.equals("")) + { + setResultError("查询失败",writer); }else{ - String name = userEntity.getUserName(); Map dataMap= new HashMap<>(); - int id = userService.selectIdByName(name); + int id = userService.selectIdByName(username); dataMap.put("comment",commentService.selectByUserId(id)); dataMap.put("project",projectService.retProjectByUserId(id)); - String jsonString = JSONObject.toJSONString(dataMap); + jsonString = JSONObject.toJSONString(dataMap); writer.println(jsonString); + setResultOK("查询失败",writer); } - writer.close(); } public void setResult(Integer code, String msg, PrintWriter writer) {