添加文章类相关代码

This commit is contained in:
sjm
2023-07-02 12:00:16 +08:00
parent 73642f8838
commit 4c9b31b69a
4 changed files with 205 additions and 4 deletions

View File

@@ -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<articleEntity> ArticleSortBylast(){
Connection conn = null;
PreparedStatement preparedStatement = null;
ResultSet rs = null;
String sql = "SELECT * FROM `article` ORDER BY publishTime DESC;";
ArrayList<com.hellogithub.entity.articleEntity> articleEntities = new ArrayList<articleEntity>();
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<articleEntity> ArticleSortByhot(){
Connection conn = null;
PreparedStatement preparedStatement = null;
ResultSet rs = null;
String sql = "SELECT * FROM `article` ORDER BY readCount DESC";
ArrayList<com.hellogithub.entity.articleEntity> articleEntities = new ArrayList<articleEntity>();
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;
}
}

View File

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

View File

@@ -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<articleEntity> ArticleSortBylast(){
articleDao articleDao = new articleDao();
return articleDao.ArticleSortBylast();
}
public List<articleEntity> ArticleSortByhot(){
articleDao articleDao = new articleDao();
return articleDao.ArticleSortByhot();
}
}

View File

@@ -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<articleEntity> articleEntityListLast = articleservice.ArticleSortBylast();
List<articleEntity> 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<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);
}
}