diff --git a/sql/hellogithub.sql b/sql/hellogithub.sql index eefde35..8fc0e55 100644 --- a/sql/hellogithub.sql +++ b/sql/hellogithub.sql @@ -11,7 +11,7 @@ Target Server Version : 80031 (8.0.31) File Encoding : 65001 - Date: 02/07/2023 22:50:04 + Date: 03/07/2023 11:28:18 */ SET NAMES utf8mb4; @@ -99,16 +99,18 @@ CREATE TABLE `comment` ( `isUsed` int NULL DEFAULT NULL COMMENT '是否使用过', `commentTime` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '评论时间', `isValid` int NULL DEFAULT NULL COMMENT '是否有效', + `star` int NULL DEFAULT NULL COMMENT '评分', PRIMARY KEY (`commentId`) USING BTREE, INDEX `userId_comment`(`userId` ASC) USING BTREE, INDEX `projectId_comment`(`projectId` ASC) USING BTREE, CONSTRAINT `projectId_comment` FOREIGN KEY (`projectId`) REFERENCES `project` (`projectId`) ON DELETE RESTRICT ON UPDATE RESTRICT, CONSTRAINT `userId_comment` FOREIGN KEY (`userId`) REFERENCES `project` (`userId`) ON DELETE RESTRICT ON UPDATE RESTRICT -) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '评论表' ROW_FORMAT = DYNAMIC; +) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '评论表' ROW_FORMAT = DYNAMIC; -- ---------------------------- -- Records of comment -- ---------------------------- +INSERT INTO `comment` VALUES (1, 1, 1, 'hao好好', 1, '2023', 1, NULL); -- ---------------------------- -- Table structure for project @@ -134,14 +136,19 @@ CREATE TABLE `project` ( INDEX `categoryId_project`(`categoryId` ASC) USING BTREE, CONSTRAINT `categoryId_project` FOREIGN KEY (`categoryId`) REFERENCES `category` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT, CONSTRAINT `userId` FOREIGN KEY (`userId`) REFERENCES `user` (`userId`) ON DELETE RESTRICT ON UPDATE RESTRICT -) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '项目表' ROW_FORMAT = DYNAMIC; +) ENGINE = InnoDB AUTO_INCREMENT = 9 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '项目表' ROW_FORMAT = DYNAMIC; -- ---------------------------- -- Records of project -- ---------------------------- -INSERT INTO `project` VALUES (1, 1, 'primihub', '11111', 'https://pic.imgdb.cn/item/6495df131ddac507cc80689f.png', '由密码学专家团队打造的开源隐私计算平台\r\nHelloGitHub 评分\r\n', '随着《数据安全法》和《个人信息保护法》的相继颁布,隐私计算技术在近两年迎来了前所未有的热度。该项目是由密码学专家团队打造的隐私计算平台,它开箱即用、安全可靠,支持隐匿查询、隐私求交、联合统计、数据资源管理等功能,实现了“数据可用不可见”,为数据安全流通保驾护航。', 1, '2023-06-30 18:58:01', 1, 1, '1111', 1, 111); -INSERT INTO `project` VALUES (2, 1, 'primihub', '11111', 'https://pic.imgdb.cn/item/6495df131ddac507cc80689f.png', '由密码学专家团队打造的开源隐私计算平台\r\nHelloGitHub 评分\r\n', '随着《数据安全法》和《个人信息保护法》的相继颁布,隐私计算技术在近两年迎来了前所未有的热度。该项目是由密码学专家团队打造的隐私计算平台,它开箱即用、安全可靠,支持隐匿查询、隐私求交、联合统计、数据资源管理等功能,实现了“数据可用不可见”,为数据安全流通保驾护航。', 1, '2023-06-30 18:58:01', 1, 1, '1111', 1, 222); -INSERT INTO `project` VALUES (3, 1, 'primihub', '11111', 'https://pic.imgdb.cn/item/6495df131ddac507cc80689f.png', '由密码学专家团队打造的开源隐私计算平台\r\nHelloGitHub 评分\r\n', '随着《数据安全法》和《个人信息保护法》的相继颁布,隐私计算技术在近两年迎来了前所未有的热度。该项目是由密码学专家团队打造的隐私计算平台,它开箱即用、安全可靠,支持隐匿查询、隐私求交、联合统计、数据资源管理等功能,实现了“数据可用不可见”,为数据安全流通保驾护航。', 1, '2023-06-30 18:58:01', 2, 1, '1111', 1, 3333); +INSERT INTO `project` VALUES (1, 1, 'primihub', '11111', 'https://pic.imgdb.cn/item/6495df131ddac507cc80689f.png', '由密码学专家团队打造的开源隐私计算平台\r\nHelloGitHub 评分\r\n', '随着《数据安全法》和《个人信息保护法》的相继颁布,隐私计算技术在近两年迎来了前所未有的热度。该项目是由密码学专家团队打造的隐私计算平台,它开箱即用、安全可靠,支持隐匿查询、隐私求交、联合统计、数据资源管理等功能,实现了“数据可用不可见”,为数据安全流通保驾护航。', 1, '2023-06-30 18:58:01', 1, 1, '1111', 1, 15); +INSERT INTO `project` VALUES (2, 2, 'primihub', '11111', 'https://pic.imgdb.cn/item/6495df131ddac507cc80689f.png', '由密码学专家团队打造的开源隐私计算平台\r\nHelloGitHub 评分\r\n', '随着《数据安全法》和《个人信息保护法》的相继颁布,隐私计算技术在近两年迎来了前所未有的热度。该项目是由密码学专家团队打造的隐私计算平台,它开箱即用、安全可靠,支持隐匿查询、隐私求交、联合统计、数据资源管理等功能,实现了“数据可用不可见”,为数据安全流通保驾护航。', 1, '2023-06-30 18:58:01', 2, 1, '1111', 1, 23); +INSERT INTO `project` VALUES (3, 1, 'primihub', '11111', 'https://pic.imgdb.cn/item/6495df131ddac507cc80689f.png', '由密码学专家团队打造的开源隐私计算平台\r\nHelloGitHub 评分\r\n', '随着《数据安全法》和《个人信息保护法》的相继颁布,隐私计算技术在近两年迎来了前所未有的热度。该项目是由密码学专家团队打造的隐私计算平台,它开箱即用、安全可靠,支持隐匿查询、隐私求交、联合统计、数据资源管理等功能,实现了“数据可用不可见”,为数据安全流通保驾护航。', 1, '2023-06-30 18:58:01', 3, 1, '1111', 1, 333); +INSERT INTO `project` VALUES (4, 1, 'primihub', '11111', 'https://pic.imgdb.cn/item/6495df131ddac507cc80689f.png', '由密码学专家团队打造的开源隐私计算平台\r\nHelloGitHub 评分\r\n', '随着《数据安全法》和《个人信息保护法》的相继颁布,隐私计算技术在近两年迎来了前所未有的热度。该项目是由密码学专家团队打造的隐私计算平台,它开箱即用、安全可靠,支持隐匿查询、隐私求交、联合统计、数据资源管理等功能,实现了“数据可用不可见”,为数据安全流通保驾护航。', 1, '2023-06-30 18:58:01', 4, 1, '1111', 1, 4444); +INSERT INTO `project` VALUES (5, 1, 'primihub', '11111', 'https://pic.imgdb.cn/item/6495df131ddac507cc80689f.png', '由密码学专家团队打造的开源隐私计算平台\r\nHelloGitHub 评分\r\n', '随着《数据安全法》和《个人信息保护法》的相继颁布,隐私计算技术在近两年迎来了前所未有的热度。该项目是由密码学专家团队打造的隐私计算平台,它开箱即用、安全可靠,支持隐匿查询、隐私求交、联合统计、数据资源管理等功能,实现了“数据可用不可见”,为数据安全流通保驾护航。', 1, '2023-06-30 18:58:01', 5, 1, '1111', 1, 55555); +INSERT INTO `project` VALUES (6, 2, 'primihub', '11111', 'https://pic.imgdb.cn/item/6495df131ddac507cc80689f.png', '由密码学专家团队打造的开源隐私计算平台\r\nHelloGitHub 评分\r\n', '随着《数据安全法》和《个人信息保护法》的相继颁布,隐私计算技术在近两年迎来了前所未有的热度。该项目是由密码学专家团队打造的隐私计算平台,它开箱即用、安全可靠,支持隐匿查询、隐私求交、联合统计、数据资源管理等功能,实现了“数据可用不可见”,为数据安全流通保驾护航。', 1, '2023-06-30 18:58:01', 6, 1, '1111', 1, 666666); +INSERT INTO `project` VALUES (7, 1, 'primihub', '11111', 'https://pic.imgdb.cn/item/6495df131ddac507cc80689f.png', '由密码学专家团队打造的开源隐私计算平台\r\nHelloGitHub 评分\r\n', '随着《数据安全法》和《个人信息保护法》的相继颁布,隐私计算技术在近两年迎来了前所未有的热度。该项目是由密码学专家团队打造的隐私计算平台,它开箱即用、安全可靠,支持隐匿查询、隐私求交、联合统计、数据资源管理等功能,实现了“数据可用不可见”,为数据安全流通保驾护航。', 1, '2023-06-30 18:58:01', 7, 1, '1111', 1, 7777777); +INSERT INTO `project` VALUES (8, 2, 'primihub', '11111', 'https://pic.imgdb.cn/item/6495df131ddac507cc80689f.png', '由密码学专家团队打造的开源隐私计算平台\r\nHelloGitHub 评分\r\n', '随着《数据安全法》和《个人信息保护法》的相继颁布,隐私计算技术在近两年迎来了前所未有的热度。该项目是由密码学专家团队打造的隐私计算平台,它开箱即用、安全可靠,支持隐匿查询、隐私求交、联合统计、数据资源管理等功能,实现了“数据可用不可见”,为数据安全流通保驾护航。', 1, '2023-06-30 18:58:01', 8, 1, '1111', 1, 88888888); -- ---------------------------- -- Table structure for project_category @@ -156,15 +163,20 @@ CREATE TABLE `project_category` ( INDEX `projectId`(`projectId` ASC) USING BTREE, CONSTRAINT `categoryId` FOREIGN KEY (`categoryId`) REFERENCES `category` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT, CONSTRAINT `projectId` FOREIGN KEY (`projectId`) REFERENCES `project` (`projectId`) ON DELETE RESTRICT ON UPDATE RESTRICT -) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '项目标签中间表' ROW_FORMAT = DYNAMIC; +) ENGINE = InnoDB AUTO_INCREMENT = 10 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '项目标签中间表' ROW_FORMAT = DYNAMIC; -- ---------------------------- -- Records of project_category -- ---------------------------- INSERT INTO `project_category` VALUES (1, 1, 1); -INSERT INTO `project_category` VALUES (2, 2, 1); +INSERT INTO `project_category` VALUES (2, 2, 2); INSERT INTO `project_category` VALUES (3, 3, 3); -INSERT INTO `project_category` VALUES (4, 1, 2); +INSERT INTO `project_category` VALUES (4, 4, 4); +INSERT INTO `project_category` VALUES (5, 5, 5); +INSERT INTO `project_category` VALUES (6, 6, 6); +INSERT INTO `project_category` VALUES (7, 7, 7); +INSERT INTO `project_category` VALUES (8, 8, 8); +INSERT INTO `project_category` VALUES (9, 2, 1); -- ---------------------------- -- Table structure for user @@ -185,6 +197,7 @@ CREATE TABLE `user` ( -- ---------------------------- -- Records of user -- ---------------------------- -INSERT INTO `user` VALUES (1, '666', '666', '2023-06-29 22:43:22', 1, 1, '666;777;33;测试;hhh;rrr;12e;', NULL); +INSERT INTO `user` VALUES (1, '666', '666', '2023-06-29 22:43:22', 1, 1, '11111;C++;C项目;github;', 1); +INSERT INTO `user` VALUES (2, '111', '111', '2023-06-29 22:43:22', 1, 1, '', 2); SET FOREIGN_KEY_CHECKS = 1; diff --git a/src/com/hellogithub/dao/UserDao.java b/src/com/hellogithub/dao/UserDao.java index 3ca3d81..59e1414 100644 --- a/src/com/hellogithub/dao/UserDao.java +++ b/src/com/hellogithub/dao/UserDao.java @@ -201,7 +201,7 @@ public class UserDao { Connection conn = null; PreparedStatement preparedStatement = null; ResultSet rs = null; - String sql = "SELECT * FROM `user` ,`article` WHERE `user`.userId = `article`.userId AND `user`.userName = ?"; + String sql = "SELECT * FROM `user` ,`article` WHERE `user`.userId = `article`.userId AND `user`.userName = ? and isValid = 1"; int Value = 0; int projectNum = 0; try{ diff --git a/src/com/hellogithub/dao/articleDao.java b/src/com/hellogithub/dao/articleDao.java index 3629a4c..401ad1e 100644 --- a/src/com/hellogithub/dao/articleDao.java +++ b/src/com/hellogithub/dao/articleDao.java @@ -24,7 +24,7 @@ public class articleDao { Connection conn = null; PreparedStatement preparedStatement = null; ResultSet rs = null; - String sql = "SELECT * FROM `article` ORDER BY publishTime DESC;"; + String sql = "SELECT * FROM `article` where isValid = 1 ORDER BY publishTime DESC;"; ArrayList articleEntities = new ArrayList(); try { conn = JdbcUtils.getConnection(); @@ -62,7 +62,7 @@ public class articleDao { Connection conn = null; PreparedStatement preparedStatement = null; ResultSet rs = null; - String sql = "SELECT * FROM `article` ORDER BY readCount DESC"; + String sql = "SELECT * FROM `article` where isValid = 1 ORDER BY readCount DESC"; ArrayList articleEntities = new ArrayList(); try { conn = JdbcUtils.getConnection(); diff --git a/src/com/hellogithub/dao/categoryDao.java b/src/com/hellogithub/dao/categoryDao.java index 192960b..27b8ef6 100644 --- a/src/com/hellogithub/dao/categoryDao.java +++ b/src/com/hellogithub/dao/categoryDao.java @@ -19,7 +19,7 @@ public class categoryDao { try { Connection conn = JdbcUtils.getConnection(); QueryRunner runner = new QueryRunner(); - String sql = "select * from category "; + String sql = "select * from category where isValid = 1"; categoryEntity = runner.query(conn, sql, new BeanListHandler<>(categoryEntity.class)); conn.close(); } catch (SQLException e) { @@ -33,7 +33,7 @@ public class categoryDao { try { Connection conn = JdbcUtils.getConnection(); QueryRunner runner = new QueryRunner(); - String sql = "select * from category where categoryId=?"; + String sql = "select * from category where categoryId=? and isValid = 1"; categoryEntity = runner.query(conn, sql, new BeanHandler<>(categoryEntity.class),id); conn.close(); } catch (SQLException e) { diff --git a/src/com/hellogithub/dao/commentDao.java b/src/com/hellogithub/dao/commentDao.java new file mode 100644 index 0000000..a0750f3 --- /dev/null +++ b/src/com/hellogithub/dao/commentDao.java @@ -0,0 +1,29 @@ +package com.hellogithub.dao; + +import com.hellogithub.entity.categoryEntity; +import com.hellogithub.entity.commentEntity; +import com.hellogithub.utils.JdbcUtils; +import org.apache.commons.dbutils.QueryRunner; +import org.apache.commons.dbutils.handlers.BeanListHandler; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.List; + +public class commentDao { + + //显示评论 + public List selectByProjectId(String id){ + List commentEntityList ; + try { + Connection conn = JdbcUtils.getConnection(); + QueryRunner runner = new QueryRunner(); + String sql = "select * from comment where projectId = ? and isValid = 1"; + commentEntityList = runner.query(conn, sql,new BeanListHandler<>(commentEntity.class),id); + conn.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + return commentEntityList; + } +} diff --git a/src/com/hellogithub/dao/projectDao.java b/src/com/hellogithub/dao/projectDao.java index a237490..74bde94 100644 --- a/src/com/hellogithub/dao/projectDao.java +++ b/src/com/hellogithub/dao/projectDao.java @@ -1,5 +1,6 @@ package com.hellogithub.dao; +import com.hellogithub.entity.categoryEntity; import com.hellogithub.entity.projectEntity; import com.hellogithub.utils.JdbcUtils; import org.apache.commons.dbutils.QueryRunner; @@ -187,4 +188,37 @@ public class projectDao { } return projectEntity; } + + /** + * 根据项目ID增加观看量 + */ + public int addProjectLookcount(String id ) { + int num; + try { + Connection conn = JdbcUtils.getConnection(); + QueryRunner runner = new QueryRunner(); + String sql = "UPDATE project SET lookcount = lookcount+1 WHERE projectId= ?"; + num = runner.update(conn, sql,id); + conn.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + return num; + } + /** + * 返回标签 + */ + public List retProjectLabel(String id ) { + List categoryEntityList ; + try { + Connection conn = JdbcUtils.getConnection(); + QueryRunner runner = new QueryRunner(); + String sql = "select categoryName from category where isValid = 1 and categoryId in( select categoryId from project_category where projectId = ? and isValid = 1)"; + categoryEntityList = runner.query(conn, sql,new BeanListHandler<>(categoryEntity.class),id); + conn.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + return categoryEntityList; + } } diff --git a/src/com/hellogithub/entity/commentEntity.java b/src/com/hellogithub/entity/commentEntity.java index d247946..6e08384 100644 --- a/src/com/hellogithub/entity/commentEntity.java +++ b/src/com/hellogithub/entity/commentEntity.java @@ -1,6 +1,5 @@ package com.hellogithub.entity; -import java.util.Date; public class commentEntity { int commentId; @@ -8,7 +7,7 @@ public class commentEntity { int projectId; String content; int isUsed; - Date commentTime; + String commentTime; int isValid; public int getCommentId() { @@ -51,11 +50,11 @@ public class commentEntity { this.isUsed = isUsed; } - public Date getCommentTime() { + public String getCommentTime() { return commentTime; } - public void setCommentTime(Date commentTime) { + public void setCommentTime(String commentTime) { this.commentTime = commentTime; } @@ -66,4 +65,14 @@ public class commentEntity { public void setIsValid(int isValid) { this.isValid = isValid; } + + int star; + + public int getStar() { + return star; + } + + public void setStar(int star) { + this.star = star; + } } diff --git a/src/com/hellogithub/entity/userEntity.java b/src/com/hellogithub/entity/userEntity.java index 823af4a..2d2c9e3 100644 --- a/src/com/hellogithub/entity/userEntity.java +++ b/src/com/hellogithub/entity/userEntity.java @@ -22,7 +22,7 @@ public class userEntity { private String userName; private String userPassword; - private Date creatTime; + private String creatTime; private Integer isValid; @@ -41,7 +41,7 @@ public class userEntity { this.userPassword = userPassword; } - public userEntity(Integer userId, String userName, String userPassword, Date creatTime, Integer isValid, Integer projectNum, String searchHistory, Integer contributionValue) { + public userEntity(Integer userId, String userName, String userPassword, String creatTime, Integer isValid, Integer projectNum, String searchHistory, Integer contributionValue) { this.userId = userId; this.userName = userName; this.userPassword = userPassword; @@ -76,11 +76,11 @@ public class userEntity { this.userPassword = userPassword; } - public Date getCreatTime() { + public String getCreatTime() { return creatTime; } - public void setCreatTime(Date creatTime) { + public void setCreatTime(String creatTime) { this.creatTime = creatTime; } diff --git a/src/com/hellogithub/service/UserService.java b/src/com/hellogithub/service/UserService.java index 8101c2f..2573775 100644 --- a/src/com/hellogithub/service/UserService.java +++ b/src/com/hellogithub/service/UserService.java @@ -62,4 +62,6 @@ public class UserService { * @return */ public int contributionValueReturn(String name){return userDao.contributionValueReturn(name);} + + public int addLabel(String username,String str){return userDao.addLabel(username,str);} } diff --git a/src/com/hellogithub/service/commentService.java b/src/com/hellogithub/service/commentService.java new file mode 100644 index 0000000..b7a439a --- /dev/null +++ b/src/com/hellogithub/service/commentService.java @@ -0,0 +1,39 @@ +package com.hellogithub.service; + +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import com.hellogithub.dao.UserDao; +import com.hellogithub.dao.commentDao; +import com.hellogithub.entity.commentEntity; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class commentService { + private commentDao commentDao =new commentDao(); + private UserDao userDao = new UserDao(); + //显示所有评论 + public String selectByProjectId(String id){ + List commentEntityList; + Map dataMap = new HashMap<>(); + Map> dataMap2 = new HashMap<>(); + commentEntityList = commentDao.selectByProjectId(id); + for(int i=0;i(); + int userid = commentEntity.getUserId(); + String username = userDao.selectNameById(userid); + String time = commentEntity.getCommentTime(); + String content = commentEntity.getContent(); + int star = commentEntity.getStar(); + dataMap.put("username",username); + dataMap.put("time",time); + dataMap.put("content",content); + dataMap.put("star",String.valueOf(star)); + dataMap2.put(String.valueOf(i),dataMap); + } + return JSONArray.toJSONString(dataMap2); + } +} diff --git a/src/com/hellogithub/service/projectService.java b/src/com/hellogithub/service/projectService.java index 135222b..64599b3 100644 --- a/src/com/hellogithub/service/projectService.java +++ b/src/com/hellogithub/service/projectService.java @@ -1,11 +1,17 @@ package com.hellogithub.service; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; import com.hellogithub.dao.categoryDao; import com.hellogithub.dao.projectDao; import com.hellogithub.entity.categoryEntity; import com.hellogithub.entity.projectEntity; + +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; public class projectService { @@ -123,4 +129,20 @@ public class projectService { projectEntity.setCategoryName(categoryEntity.getCategoryName()); return projectEntity; } + public int addProjectLookcount(String id ){ + return projectDao.addProjectLookcount(id); + } + public String retProjectLabel(String id ){ + Map> dataMap = new HashMap<>(); + List strs=new ArrayList<>(); + List categoryEntityList = projectDao.retProjectLabel(id); + for(int i=0;i projectEntityList = projectService.selectByInput(str); @@ -51,24 +53,35 @@ public class selectByInputServlet extends HttpServlet { userEntity=(userEntity)session.getAttribute("user"); if(userEntity == null) { - + if(projectEntityList.size() == 0 ){ + setResultError("查询结果为空",writer); + } + else{ + String jsonString = JSONObject.toJSONString(projectEntityList); + setResultOK("success",writer); + writer.println(jsonString); + writer.close(); + setResultOK("success",writer); + } } else{ + String username = userEntity.getUserName(); + int num = userService.addLabel(username,str); - + if(projectEntityList.size() == 0 ){ + setResultError("查询结果为空",writer); + } + else{ + String jsonString = JSONObject.toJSONString(projectEntityList); + setResultOK("success",writer); + writer.println(jsonString); + writer.close(); + setResultOK("success",writer); + } } - if(projectEntityList.size() == 0 ){ - setResultError("查询结果为空",writer); - } - else{ - String jsonString = JSONObject.toJSONString(projectEntityList); - setResultOK("success",writer); - writer.println(jsonString); - writer.close(); - setResultOK("success",writer); - } + } public void setResult(Integer code, String msg, PrintWriter writer) { diff --git a/src/com/hellogithub/servlet/selectCommentByProServlet.java b/src/com/hellogithub/servlet/selectCommentByProServlet.java new file mode 100644 index 0000000..0d9f1a2 --- /dev/null +++ b/src/com/hellogithub/servlet/selectCommentByProServlet.java @@ -0,0 +1,63 @@ +package com.hellogithub.servlet; + +import com.alibaba.fastjson.JSONObject; +import com.hellogithub.service.commentService; +import jakarta.servlet.ServletConfig; +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("/selectCommentByPro") +public class selectCommentByProServlet extends HttpServlet { + private commentService commentService = new commentService(); + @Override + public void init(ServletConfig config) throws ServletException { + + } + + @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"); + resp.setContentType("text/html; charset=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"); + //按照分类检索 + String id = req.getParameter("id"); + PrintWriter writer = resp.getWriter(); + writer.println(commentService.selectByProjectId(id)); + 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); + } + + public void setResultOK(String msg, PrintWriter writer) { + setResult(200, msg, writer); + } +} diff --git a/src/com/hellogithub/servlet/selectProByIdServlet.java b/src/com/hellogithub/servlet/selectProByIdServlet.java index 0401666..8458dd3 100644 --- a/src/com/hellogithub/servlet/selectProByIdServlet.java +++ b/src/com/hellogithub/servlet/selectProByIdServlet.java @@ -1,5 +1,6 @@ package com.hellogithub.servlet; +import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.hellogithub.service.projectService; import jakarta.servlet.ServletConfig; @@ -37,11 +38,17 @@ public class selectProByIdServlet extends HttpServlet { 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"); - //按照分类返回期刊 - String id = req.getParameter("xxxxxxx"); + + String id = req.getParameter("id"); + int num = projectService.addProjectLookcount(id); PrintWriter writer = resp.getWriter(); - String jsonString = JSONObject.toJSONString(projectService.selectProById(id)); - writer.println(jsonString); + String dataMap = projectService.retProjectLabel(id); + JSONObject jsonObject = JSON.parseObject(dataMap); + jsonObject.put("project",projectService.selectProById(id)); + writer.println(JSONObject.toJSONString(jsonObject)); + if(num == 0 ){ + setResult(200, "观看量增加失败", writer); + } writer.close(); } public void setResult(Integer code, String msg, PrintWriter writer) {