添加文章访问量+1和根据id返回文

This commit is contained in:
sjm
2023-07-04 11:09:51 +08:00
parent c390ea29d1
commit e6e811ebf8
4 changed files with 193 additions and 3 deletions

View File

@@ -91,4 +91,71 @@ public class articleDao {
}
}return articleEntities;
}
public articleEntity selectByArticleId(int id){
Connection conn = null;
PreparedStatement preparedStatement = null;
ResultSet rs = null;
String sql = "SELECT * FROM `article` WHERE articleId = ? AND isValid = 1;";
articleEntity entity = null;
try{
conn = JdbcUtils.getConnection();
preparedStatement = conn.prepareStatement(sql);
rs = preparedStatement.executeQuery();
preparedStatement.setInt(1, id);
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);
}
}
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);
}
}
}

View File

@@ -10,14 +10,20 @@ public class articleService {
public articleService(){
}
private articleDao articleDao = new articleDao();
public List<articleEntity> ArticleSortBylast(){
articleDao articleDao = new articleDao();
return articleDao.ArticleSortBylast();
}
public List<articleEntity> ArticleSortByhot(){
articleDao articleDao = new articleDao();
return articleDao.ArticleSortByhot();
}
public articleEntity selectByArticleId(int id){
return articleDao.selectByArticleId(id);
}
public articleEntity updateReadCount(int id){
return articleDao.updateReadCount(id);
}
}

View File

@@ -0,0 +1,59 @@
package com.hellogithub.servlet.article;
import com.alibaba.fastjson.JSONObject;
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 com.hellogithub.entity.articleEntity;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
@WebServlet("selectByArticleId")
public class selectByArticleId 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<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);
}
}

View File

@@ -0,0 +1,58 @@
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<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);
}
}