From e6bb6526a9c82cf0ca2d9811e3e61e696c5b03e2 Mon Sep 17 00:00:00 2001 From: Zhang Liguo <482370576@qq.com> Date: Sun, 2 Jul 2023 12:19:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E7=94=A8=E6=88=B7=E6=80=BB?= =?UTF-8?q?=E6=95=B0=E8=BF=9B=E8=A1=8C=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/hellogithub/dao/UserDao.java | 10 ++++++---- src/com/hellogithub/entity/userEntity.java | 9 +++++++++ src/com/hellogithub/service/UserService.java | 2 +- src/com/hellogithub/servlet/userNumServlet.java | 6 ++++-- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/com/hellogithub/dao/UserDao.java b/src/com/hellogithub/dao/UserDao.java index fde0b4d..9e778f2 100644 --- a/src/com/hellogithub/dao/UserDao.java +++ b/src/com/hellogithub/dao/UserDao.java @@ -175,13 +175,15 @@ public class UserDao { * * 返回用户总数 * */ - public int queryUserCount(){ - int num; + public long queryUserCount(){ + userEntity userEntity; + long num; try { Connection conn = JdbcUtils.getConnection(); QueryRunner runner = new QueryRunner(); - String sql="select count(*) from user"; - num = runner.query(conn,sql,new BeanHandler<>(Integer.class)); + String sql="select count(*) as num from user"; + userEntity = runner.query(conn,sql,new BeanHandler<>(userEntity.class)); + num = userEntity.getNum(); } catch (SQLException e) { throw new RuntimeException(e); } diff --git a/src/com/hellogithub/entity/userEntity.java b/src/com/hellogithub/entity/userEntity.java index a9939b5..540a181 100644 --- a/src/com/hellogithub/entity/userEntity.java +++ b/src/com/hellogithub/entity/userEntity.java @@ -117,4 +117,13 @@ public class userEntity { ", searchHistory='" + searchHistory + '\'' + '}'; } + long num; + + public long getNum() { + return num; + } + + public void setNum(long num) { + this.num = num; + } } diff --git a/src/com/hellogithub/service/UserService.java b/src/com/hellogithub/service/UserService.java index 80ce2dc..f2c6a63 100644 --- a/src/com/hellogithub/service/UserService.java +++ b/src/com/hellogithub/service/UserService.java @@ -47,5 +47,5 @@ public class UserService { * @param name * */ public int dedleteAllLabel(String name){return userDao.dedleteAllLabel(name);} - public int queryUserCount(){return userDao.queryUserCount();} + public long queryUserCount(){return userDao.queryUserCount();} } diff --git a/src/com/hellogithub/servlet/userNumServlet.java b/src/com/hellogithub/servlet/userNumServlet.java index 7d4342f..1030075 100644 --- a/src/com/hellogithub/servlet/userNumServlet.java +++ b/src/com/hellogithub/servlet/userNumServlet.java @@ -1,9 +1,11 @@ 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; @@ -11,7 +13,7 @@ import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.util.HashMap; - +@WebServlet("/userNum") public class userNumServlet extends HttpServlet { private UserService userService = new UserService(); @Override @@ -38,7 +40,7 @@ public class userNumServlet extends HttpServlet { resp.setContentType("application/json;charset=UTF-8"); PrintWriter writer = resp.getWriter(); - int num = userService.queryUserCount(); + long num = userService.queryUserCount(); String str= "num:"+num ; String jsonString = JSONObject.toJSONString(str); writer.println(jsonString);