From 73642f883862fdb59547cb38660520dd8b293ab9 Mon Sep 17 00:00:00 2001 From: User_cyk <1020691186@qq.com> Date: Sat, 1 Jul 2023 13:58:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/hellogithub/dao/UserDao.java | 18 ++-- src/com/hellogithub/dao/projectDao.java | 69 +++++++++------ src/com/hellogithub/service/UserService.java | 7 +- .../hellogithub/service/projectService.java | 85 ++++++++++++++++--- .../servlet/selectByInputServlet.java | 73 ++++++++++++++++ .../servlet/selectLastByCateServlet.java | 2 +- .../servlet/selectMostStarServlet.java | 9 +- .../hellogithub/servlet/userNumServlet.java | 43 +++++++--- web/index.jsp | 3 +- 9 files changed, 233 insertions(+), 76 deletions(-) create mode 100644 src/com/hellogithub/servlet/selectByInputServlet.java diff --git a/src/com/hellogithub/dao/UserDao.java b/src/com/hellogithub/dao/UserDao.java index b23fdba..fde0b4d 100644 --- a/src/com/hellogithub/dao/UserDao.java +++ b/src/com/hellogithub/dao/UserDao.java @@ -4,7 +4,6 @@ import com.hellogithub.entity.userEntity; import com.hellogithub.utils.JdbcUtils; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; -import org.apache.commons.dbutils.handlers.ScalarHandler; import java.sql.*; @@ -172,18 +171,21 @@ public class UserDao { return num; } - public Long retUserNum(){ - Long count; + /* + * + * 返回用户总数 + * */ + public int queryUserCount(){ + int num; try { Connection conn = JdbcUtils.getConnection(); - ScalarHandler handler=new ScalarHandler(); QueryRunner runner = new QueryRunner(); - String sql = "select count(*) from user"; - count =(Long) runner.query( conn,sql,new ScalarHandler()); - conn.close(); + String sql="select count(*) from user"; + num = runner.query(conn,sql,new BeanHandler<>(Integer.class)); } catch (SQLException e) { throw new RuntimeException(e); } - return count; + + return num; } } diff --git a/src/com/hellogithub/dao/projectDao.java b/src/com/hellogithub/dao/projectDao.java index 65aa7eb..880decd 100644 --- a/src/com/hellogithub/dao/projectDao.java +++ b/src/com/hellogithub/dao/projectDao.java @@ -5,11 +5,8 @@ import com.hellogithub.utils.JdbcUtils; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import org.apache.commons.dbutils.handlers.BeanListHandler; -import org.apache.commons.dbutils.handlers.ScalarHandler; -import java.lang.ref.PhantomReference; import java.sql.Connection; -import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; @@ -53,17 +50,19 @@ public class projectDao { return projectEntity.getPeriodicals(); } /** - * 返回最所有项目总数 + * 返回所有项目总数 * * */ public int countAll() { int count; + projectEntity p; try { Connection conn = JdbcUtils.getConnection(); QueryRunner runner = new QueryRunner(); - String sql = "select count(1) from project"; - count = runner.query(conn, sql, new BeanHandler<>(Integer.class)); + String sql = "select count(*) as projectId from project"; + p = runner.query(conn, sql, new BeanHandler<>(projectEntity.class)); + count = p.getProjectId(); conn.close(); } catch (SQLException e) { throw new RuntimeException(e); @@ -96,16 +95,12 @@ public class projectDao { QueryRunner runner = new QueryRunner(); String sql = "select * from project;"; projectEntityList = runner.query(conn, sql, new BeanListHandler<>(projectEntity.class)); + conn.close(); } catch (SQLException e) { throw new RuntimeException(e); } return projectEntityList; } - /** - * 按照分类检索项目 - * - * - */ public List retSelcetByCate(int cateid){ List projectEntityList; try { @@ -113,47 +108,65 @@ public class projectDao { QueryRunner runner = new QueryRunner(); String sql = "select * from project where projectId in (select projectId from project_category where categoryId=?)"; projectEntityList = runner.query(conn, sql, new BeanListHandler<>(projectEntity.class),cateid); + conn.close(); } catch (SQLException e) { throw new RuntimeException(e); } return projectEntityList; } - /** - * 按照分类检索最热门项目 - * - * - */ + public List retSelcetByStartNum(int cateid){ List projectEntityList; try { Connection conn = JdbcUtils.getConnection(); QueryRunner runner = new QueryRunner(); - String sql = "select * from project where projectId in (select projectId from project_category where categoryId=?) order by startNum"; + String sql = "select * from project where projectId in (select projectId from project_category where categoryId=?) order by lookCount desc"; projectEntityList = runner.query(conn, sql, new BeanListHandler<>(projectEntity.class),cateid); + conn.close(); } catch (SQLException e) { throw new RuntimeException(e); } return projectEntityList; } - /** - * 检索所有最热门的项目 - * - * - */ - public List retMostStar(){ + public List retMostStar( ){ List projectEntityList; try { Connection conn = JdbcUtils.getConnection(); QueryRunner runner = new QueryRunner(); - String sql = "select * from project order by startNum desc"; + String sql = "select * from project order by lookCount desc"; projectEntityList = runner.query(conn, sql, new BeanListHandler<>(projectEntity.class)); + conn.close(); } catch (SQLException e) { throw new RuntimeException(e); } return projectEntityList; } - - - - + //搜索框 + public List selectByInput(String str) { + List projectEntityList; + try { + Connection conn = JdbcUtils.getConnection(); + QueryRunner runner = new QueryRunner(); + String sql = "SELECT * FROM project WHERE projectName LIKE %?%"; + projectEntityList = runner.query(conn, sql, new BeanListHandler<>(projectEntity.class), str); + conn.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + return projectEntityList; + } + //返回标签 +// public List<> retLabelBy(String str) { +// List projectEntityList; +// try { +// Connection conn = JdbcUtils.getConnection(); +// QueryRunner runner = new QueryRunner(); +// String sql = "SELECT * FROM project WHERE projectName LIKE %?%"; +// projectEntityList = runner.query(conn, sql, new BeanListHandler<>(projectEntity.class), str); +// conn.close(); +// } catch (SQLException e) { +// throw new RuntimeException(e); +// } +// return ; +// } } diff --git a/src/com/hellogithub/service/UserService.java b/src/com/hellogithub/service/UserService.java index 43e89f5..80ce2dc 100644 --- a/src/com/hellogithub/service/UserService.java +++ b/src/com/hellogithub/service/UserService.java @@ -47,10 +47,5 @@ public class UserService { * @param name * */ public int dedleteAllLabel(String name){return userDao.dedleteAllLabel(name);} - /** - * 查询用户表中的用户总数 - * */ - public Long retUserNums(){return userDao.retUserNum();} - - + public int queryUserCount(){return userDao.queryUserCount();} } diff --git a/src/com/hellogithub/service/projectService.java b/src/com/hellogithub/service/projectService.java index dd7afe8..844455c 100644 --- a/src/com/hellogithub/service/projectService.java +++ b/src/com/hellogithub/service/projectService.java @@ -4,8 +4,10 @@ import com.hellogithub.dao.UserDao; import com.hellogithub.dao.projectDao; import com.hellogithub.entity.projectEntity; -import java.sql.Array; -import java.util.*; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; public class projectService { @@ -39,7 +41,7 @@ public class projectService { for(int i =0;i retSelcetByStar(int cate){ - if (cate==0){ - return projectDao.retMostStar(); + Map dataMap=new HashMap<>(); + dataMap.put(1,"C项目"); + dataMap.put(2,"C#项目"); + dataMap.put(3,"C++项目"); + dataMap.put(4,"CSS项目"); + dataMap.put(5,"Go项目"); + dataMap.put(6,"Java项目"); + dataMap.put(7,"Javascript项目"); + dataMap.put(8,"Kotlin项目"); + dataMap.put(9,"Obejctive-C项目"); + dataMap.put(10,"PHP项目"); + dataMap.put(11,"Python项目"); + dataMap.put(12,"Ruby项目"); + dataMap.put(13,"Rust项目"); + dataMap.put(14,"Swift项目"); + dataMap.put(15,"其他"); + dataMap.put(16,"开源书籍"); + dataMap.put(17,"机器学习"); + List projectEntityList; + + if (cate == 0){ + projectEntityList =projectDao.retMostStar(); + for(int i =0;i selectByInput(String str){ + Map dataMap=new HashMap<>(); + dataMap.put(1,"C项目"); + dataMap.put(2,"C#项目"); + dataMap.put(3,"C++项目"); + dataMap.put(4,"CSS项目"); + dataMap.put(5,"Go项目"); + dataMap.put(6,"Java项目"); + dataMap.put(7,"Javascript项目"); + dataMap.put(8,"Kotlin项目"); + dataMap.put(9,"Obejctive-C项目"); + dataMap.put(10,"PHP项目"); + dataMap.put(11,"Python项目"); + dataMap.put(12,"Ruby项目"); + dataMap.put(13,"Rust项目"); + dataMap.put(14,"Swift项目"); + dataMap.put(15,"其他"); + dataMap.put(16,"开源书籍"); + dataMap.put(17,"机器学习"); + List projectEntityList = projectDao.selectByInput(str); + for(int i =0;i projectEntityList = projectService.selectByInput(str); + 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) { + 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/selectLastByCateServlet.java b/src/com/hellogithub/servlet/selectLastByCateServlet.java index d9894e0..49a2f35 100644 --- a/src/com/hellogithub/servlet/selectLastByCateServlet.java +++ b/src/com/hellogithub/servlet/selectLastByCateServlet.java @@ -42,7 +42,7 @@ public class selectLastByCateServlet extends HttpServlet { 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 cate = req.getParameter("cate"); + String cate = req.getParameter("num"); PrintWriter writer = resp.getWriter(); String jsonString = JSONObject.toJSONString(projectService.retLatestProject(Integer.valueOf(cate))); writer.println(jsonString); diff --git a/src/com/hellogithub/servlet/selectMostStarServlet.java b/src/com/hellogithub/servlet/selectMostStarServlet.java index fe076a3..eb4c9ab 100644 --- a/src/com/hellogithub/servlet/selectMostStarServlet.java +++ b/src/com/hellogithub/servlet/selectMostStarServlet.java @@ -11,11 +11,7 @@ import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; -/** - * 根据标签查询最热门的项目 - * - * - */ + @WebServlet("/selectMostStar") public class selectMostStarServlet extends HttpServlet { private projectService projectService=new projectService(); @@ -42,7 +38,8 @@ public class selectMostStarServlet extends HttpServlet { 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 cate = req.getParameter("cate"); + String cate = req.getParameter("num"); + PrintWriter writer = resp.getWriter(); String jsonString = JSONObject.toJSONString(projectService.retSelcetByStar(Integer.valueOf(cate))); writer.println(jsonString); diff --git a/src/com/hellogithub/servlet/userNumServlet.java b/src/com/hellogithub/servlet/userNumServlet.java index a837b82..7d4342f 100644 --- a/src/com/hellogithub/servlet/userNumServlet.java +++ b/src/com/hellogithub/servlet/userNumServlet.java @@ -1,31 +1,27 @@ package com.hellogithub.servlet; import com.alibaba.fastjson.JSONObject; -import com.hellogithub.dao.UserDao; +import com.hellogithub.service.UserService; 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; -/** - * 返回数据库中用户的总人数 - * */ -@WebServlet("/userNum") +import java.util.HashMap; + public class userNumServlet extends HttpServlet { - UserDao userDao = new UserDao(); + private UserService userService = new UserService(); @Override public void init(ServletConfig config) throws ServletException { - super.init(config); + } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - - doPost(req,resp); + doPost(req, resp); } @Override @@ -42,14 +38,33 @@ public class userNumServlet extends HttpServlet { resp.setContentType("application/json;charset=UTF-8"); PrintWriter writer = resp.getWriter(); - String str= "num:"+ userDao.retUserNum(); + int num = userService.queryUserCount(); + String str= "num:"+num ; String jsonString = JSONObject.toJSONString(str); writer.println(jsonString); + if(num == 0) + { + setResultError("用户总数为空",writer); + }else{ + setResultOK("success",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(); } - @Override - protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - super.service(req, resp); + 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/web/index.jsp b/web/index.jsp index 136da64..8da7aaf 100644 --- a/web/index.jsp +++ b/web/index.jsp @@ -11,6 +11,7 @@ $Title$ - $END$ + 你说的对,但是hellogithub是一个.... +