From b58a629d48096d842c5042109d6185169992529e Mon Sep 17 00:00:00 2001 From: sjm <2431685932@qq.com> Date: Tue, 4 Jul 2023 20:38:01 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E6=A0=B9=E6=8D=AE=E6=96=87?= =?UTF-8?q?=E7=AB=A0id=E6=9F=A5=E6=89=BEusername=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/hellogithub/dao/articleDao.java | 45 +++----------- .../hellogithub/service/articleService.java | 3 - .../servlet/article/updateReadCount.java | 58 ------------------- 3 files changed, 9 insertions(+), 97 deletions(-) delete mode 100644 src/com/hellogithub/servlet/article/updateReadCount.java diff --git a/src/com/hellogithub/dao/articleDao.java b/src/com/hellogithub/dao/articleDao.java index cc9ea63..ab1dd27 100644 --- a/src/com/hellogithub/dao/articleDao.java +++ b/src/com/hellogithub/dao/articleDao.java @@ -99,14 +99,19 @@ public class articleDao { public articleEntity selectByArticleId(int id){ Connection conn = null; PreparedStatement preparedStatement = null; + PreparedStatement preparedStatement1 = null; ResultSet rs = null; String sql = "SELECT * FROM `article` WHERE articleId = ? AND isValid = 1;"; + String sql1 = "UPDATE article SET readCount = readCount+1 where articleId = ?"; articleEntity entity = null; try{ conn = JdbcUtils.getConnection(); preparedStatement.setInt(1, id); preparedStatement = conn.prepareStatement(sql); + preparedStatement1.setInt(1, id); + preparedStatement1 = conn.prepareStatement(sql); rs = preparedStatement.executeQuery(); + preparedStatement1.executeQuery(); while(rs.next()){ int articleid = rs.getInt(1); @@ -115,12 +120,16 @@ public class articleDao { String datetime = rs.getString(4); int isvalid = rs.getInt(5); int readcount = rs.getInt(6); + String articleTitle = rs.getString(7); + String articleIco = rs.getString(8); entity.setArticleId(articleid); entity.setUserId(userid); entity.setArticleContent(articlecontent); entity.setPublishTime(datetime); entity.setIsValid(isvalid); entity.setReadCount(readcount); + entity.setArticleTitle(articleTitle); + entity.setArticleIco(articleIco); } return entity; } catch (Exception e) { @@ -128,40 +137,4 @@ public class articleDao { return entity; } } - - public articleEntity updateReadCount(int id){ - Connection conn = null; - PreparedStatement preparedStatement = null; - PreparedStatement preparedStatement1 = null; - ResultSet rs = null; - String sql = "UPDATE article SET readCount = readCount+1 where articleId = ?"; - String sql2 = "SELECT * FROM `article` WHERE articleId = ? AND isValid = 1;"; - articleEntity entity = null; - try{ - conn = JdbcUtils.getConnection(); - preparedStatement = conn.prepareStatement(sql); - preparedStatement1 = conn.prepareStatement(sql2); - preparedStatement.setInt(1, id); - preparedStatement.executeQuery(); - preparedStatement1.setInt(1, id); - rs = preparedStatement1.executeQuery(); - while(rs.next()){ - int articleid = rs.getInt(1); - int userid = rs.getInt(2); - String articlecontent = rs.getString(3); - String datetime = rs.getString(4); - int isvalid = rs.getInt(5); - int readcount = rs.getInt(6); - entity.setArticleId(articleid); - entity.setUserId(userid); - entity.setArticleContent(articlecontent); - entity.setPublishTime(datetime); - entity.setIsValid(isvalid); - entity.setReadCount(readcount); - } - return entity; - } catch (SQLException e) { - throw new RuntimeException(e); - } - } } \ No newline at end of file diff --git a/src/com/hellogithub/service/articleService.java b/src/com/hellogithub/service/articleService.java index 8ab3a27..c00ab5a 100644 --- a/src/com/hellogithub/service/articleService.java +++ b/src/com/hellogithub/service/articleService.java @@ -80,7 +80,4 @@ public class articleService { return JSONObject.toJSONString(dataMap); } - public articleEntity updateReadCount(int id){ - return articleDao.updateReadCount(id); - } } \ No newline at end of file diff --git a/src/com/hellogithub/servlet/article/updateReadCount.java b/src/com/hellogithub/servlet/article/updateReadCount.java deleted file mode 100644 index 4dea9db..0000000 --- a/src/com/hellogithub/servlet/article/updateReadCount.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.hellogithub.servlet.article; - -import com.alibaba.fastjson.JSONObject; -import com.hellogithub.entity.articleEntity; -import com.hellogithub.service.articleService; -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("/updateReadCount") -public class updateReadCount extends HttpServlet { - protected articleService articleservice = new articleService(); - @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"); - // 设置响应头允许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(); - String articleid = req.getParameter("articleId"); - int id = Integer.parseInt(articleid); - articleEntity entity = articleservice.selectByArticleId(id); - if(entity == null){ - setResultError("该文章id不存在或已失效", writer); - }else{ - String jsonString = JSONObject.toJSONString(entity); - writer.println(jsonString); - } - } - 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); - } -}