From 4c9b31b69a6a38f397c040102de11766302677d9 Mon Sep 17 00:00:00 2001 From: sjm <2431685932@qq.com> Date: Sun, 2 Jul 2023 12:00:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=96=87=E7=AB=A0=E7=B1=BB?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/hellogithub/dao/articleDao.java | 94 +++++++++++++++++++ src/com/hellogithub/entity/articleEntity.java | 23 ++++- .../hellogithub/service/articleService.java | 23 +++++ .../servlet/article/articleSortServlet.java | 69 ++++++++++++++ 4 files changed, 205 insertions(+), 4 deletions(-) create mode 100644 src/com/hellogithub/dao/articleDao.java create mode 100644 src/com/hellogithub/service/articleService.java create mode 100644 src/com/hellogithub/servlet/article/articleSortServlet.java diff --git a/src/com/hellogithub/dao/articleDao.java b/src/com/hellogithub/dao/articleDao.java new file mode 100644 index 0000000..3629a4c --- /dev/null +++ b/src/com/hellogithub/dao/articleDao.java @@ -0,0 +1,94 @@ +package com.hellogithub.dao; + +import com.hellogithub.entity.articleEntity; +import com.hellogithub.entity.projectEntity; +import com.hellogithub.entity.userEntity; +import com.hellogithub.utils.JdbcUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.ResultSetHandler; +import org.apache.commons.dbutils.handlers.BeanListHandler; + +import java.sql.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class articleDao { + private articleEntity articleEntity = new articleEntity(); + + /** + 按照最新排序文章 + */ + public List ArticleSortBylast(){ + Connection conn = null; + PreparedStatement preparedStatement = null; + ResultSet rs = null; + String sql = "SELECT * FROM `article` ORDER BY publishTime DESC;"; + ArrayList articleEntities = new ArrayList(); + try { + conn = JdbcUtils.getConnection(); + preparedStatement = conn.prepareStatement(sql); + rs = preparedStatement.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); + articleEntity entity = new articleEntity(articleid, userid, articlecontent, datetime, isvalid, readcount); + articleEntities.add(entity); + } + return articleEntities; + }catch(Exception e){ + e.printStackTrace(); + }finally{ + try{ + conn.close(); + preparedStatement.close(); + rs.close(); + }catch(Exception e) { + e.printStackTrace(); + } + }return articleEntities; + } + + /** + * 按浏览量最多排序文章 + * @return + */ + public List ArticleSortByhot(){ + Connection conn = null; + PreparedStatement preparedStatement = null; + ResultSet rs = null; + String sql = "SELECT * FROM `article` ORDER BY readCount DESC"; + ArrayList articleEntities = new ArrayList(); + try { + conn = JdbcUtils.getConnection(); + preparedStatement = conn.prepareStatement(sql); + rs = preparedStatement.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); + articleEntity entity = new articleEntity(articleid, userid, articlecontent, datetime, isvalid, readcount); + articleEntities.add(entity); + } + return articleEntities; + }catch(Exception e){ + e.printStackTrace(); + }finally{ + try{ + conn.close(); + preparedStatement.close(); + rs.close(); + }catch(Exception e) { + e.printStackTrace(); + } + }return articleEntities; + } +} \ No newline at end of file diff --git a/src/com/hellogithub/entity/articleEntity.java b/src/com/hellogithub/entity/articleEntity.java index fcc7965..1dd1682 100644 --- a/src/com/hellogithub/entity/articleEntity.java +++ b/src/com/hellogithub/entity/articleEntity.java @@ -1,11 +1,28 @@ package com.hellogithub.entity; public class articleEntity { + public articleEntity(){} //无参构造函数 + public articleEntity(int articleId,int userId,String articleContent,String publishTime,int isValid,int readCount){ + this.articleId = articleId; + this.userId = userId; + this.articleContent = articleContent; + this.publishTime = publishTime; + this.isValid = isValid; + this.readCount = readCount; + } int articleId; int userId; + String articleContent; + + String publishTime; + int isValid; + + int readCount; + + public int getArticleId() { return articleId; } @@ -46,9 +63,7 @@ public class articleEntity { this.isValid = isValid; } - String articleContent; - - String publishTime; - int isValid; + public int getReadCount(){return readCount;} + public void setReadCount(int readCount){this.readCount = readCount;} } diff --git a/src/com/hellogithub/service/articleService.java b/src/com/hellogithub/service/articleService.java new file mode 100644 index 0000000..289d880 --- /dev/null +++ b/src/com/hellogithub/service/articleService.java @@ -0,0 +1,23 @@ +package com.hellogithub.service; + +import com.hellogithub.dao.articleDao; +import com.hellogithub.entity.articleEntity; + + +import java.util.List; + +public class articleService { + public articleService(){ + + } + + public List ArticleSortBylast(){ + articleDao articleDao = new articleDao(); + return articleDao.ArticleSortBylast(); + } + + public List ArticleSortByhot(){ + articleDao articleDao = new articleDao(); + return articleDao.ArticleSortByhot(); + } +} \ No newline at end of file diff --git a/src/com/hellogithub/servlet/article/articleSortServlet.java b/src/com/hellogithub/servlet/article/articleSortServlet.java new file mode 100644 index 0000000..4dc16c8 --- /dev/null +++ b/src/com/hellogithub/servlet/article/articleSortServlet.java @@ -0,0 +1,69 @@ +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.lang.reflect.Array; +import java.util.HashMap; +import java.util.List; + + +/** + * 最新时间排序文章 + */ +@WebServlet("/articleSort") +public class articleSortServlet 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"); + +// 传入参数?sort_by = last 或者?sort_by = hot 默认是last界面 + PrintWriter writer = resp.getWriter(); + List articleEntityListLast = articleservice.ArticleSortBylast(); + List articleEntityListHot = articleservice.ArticleSortByhot(); + String sortby = req.getParameter("sort_by"); + String jsonString1 = JSONObject.toJSONString(articleEntityListLast); + String jsonString2 = JSONObject.toJSONString(articleEntityListHot); + if ("last".equals(sortby)) { + writer.println(jsonString1); + }else if("hot".equals(sortby)){ + writer.println(jsonString2); + }else{ + setResultError("false", writer); + } + 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); + } +} \ No newline at end of file