SMBMS订单管理系统(手把手教细致讲解实现全过程) (七)

article/2025/7/13 17:18:50

实现用户管功能
在这里插入图片描述
刚刚访问密码直接走前端

现在要发起请求走Servlet,Servlet处理后返回前端页面
Servlet

  • 处理请求
  • 调用业务
  • 返回页面

业务要查询用户列表,查询角色列表,为了实现分页,需查询pageSize总数。查询从Service层到Dao层,Dao层从数据库里面查

1、导入分页的工具类

2、用户列表页面导入
为了我们职责统一,可以把角色的操作单独放在一个包中,和Pojo类一一对应

1、获取用户数量


UserDao

 //查询用户总数public int getUserCount(Connection connection,String username,int userRole) throws SQLException;
//通过条件查询-userlistpublic List<User> getUserList(Connection connection,String userName,int userRole,int currentPageNo,int pageSize)throws Exception;
  • UserDaoImpl
//根据用户名或者角色查询用户总数public int getUserCount(Connection connection, String username, int userRole) throws SQLException {PreparedStatement pstm=null;ResultSet rs=null;int count=0;if(connection!=null){StringBuffer sql=new StringBuffer();sql.append("select count(1) as count from smbms_user u,smbms_role r where u.userRole=r.id");ArrayList<Object> list=new ArrayList<Object>();if(!StringUtils.isNullOrEmpty(username)){sql.append(" and u.userName like ?");list.add("%"+username+"%");}if(userRole>0){sql.append(" and u.userRole= ?");list.add(userRole);}//list转化为数组Object[] params=list.toArray();System.out.println("UserDaoImpl->getUserCount:"+sql.toString());rs = BaseDao.execute(connection, pstm, rs, sql.toString(), params);if(rs.next()){//从结果集中获取最终的数量count = rs.getInt("count");}BaseDao.closeResource(null,pstm,rs);}return count;}public List<User> getUserList(Connection connection, String userName, int userRole, int currentPageNo, int pageSize) throws Exception {// TODO Auto-generated method stubPreparedStatement pstm = null;ResultSet rs = null;List<User> userList = new ArrayList<User>();if(connection != null){StringBuffer sql = new StringBuffer();sql.append("select u.*,r.roleName as userRoleName from smbms_user u,smbms_role r where u.userRole = r.id");List<Object> list = new ArrayList<Object>();if(!StringUtils.isNullOrEmpty(userName)){sql.append(" and u.userName like ?");list.add("%"+userName+"%");}if(userRole > 0){sql.append(" and u.userRole = ?");list.add(userRole);}sql.append(" order by creationDate DESC limit ?,?");currentPageNo = (currentPageNo-1)*pageSize;list.add(currentPageNo);list.add(pageSize);Object[] params = list.toArray();System.out.println("sql ----> " + sql.toString());rs = BaseDao.execute(connection, pstm, rs, sql.toString(), params);while(rs.next()){User _user = new User();_user.setId(rs.getInt("id"));_user.setUserCode(rs.getString("userCode"));_user.setUserName(rs.getString("userName"));_user.setGender(rs.getInt("gender"));_user.setBirthday(rs.getDate("birthday"));_user.setPhone(rs.getString("phone"));_user.setUserRole(rs.getInt("userRole"));_user.setUserRoleName(rs.getString("userRoleName"));userList.add(_user);}BaseDao.closeResource(null, pstm, rs);}return userList;}
  • UserService
    //查询记录数public int getUserCount(String userName,int userRole);public List<User> getUserList(String queryUserName, int queryUserRole, int currentPageNo, int pageSize);
  • UserServiceImpl
//查询记录数public int getUserCount(String userName, int userRole) {Connection connection = null;int count=0;try {connection=BaseDao.getConnection();count=userDao.getUserCount(connection,userName,userRole);} catch (SQLException e) {e.printStackTrace();}finally {BaseDao.closeResource(connection,null,null);}return count;}public List<User> getUserList(String queryUserName, int queryUserRole, int currentPageNo, int pageSize) {// TODO Auto-generated method stubConnection connection = null;List<User> userList = null;System.out.println("queryUserName ---- > " + queryUserName);System.out.println("queryUserRole ---- > " + queryUserRole);System.out.println("currentPageNo ---- > " + currentPageNo);System.out.println("pageSize ---- > " + pageSize);try {connection = BaseDao.getConnection();userList = userDao.getUserList(connection, queryUserName,queryUserRole,currentPageNo,pageSize);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{BaseDao.closeResource(connection, null, null);}return userList;}@Testpublic void test1(){UserServiceImpl userService = new UserServiceImpl();int userCount=userService.getUserCount(null,0);System.out.println(userCount);}

2、获取角色数量

在Dao层和Service层都新增role部分
在这里插入图片描述

RoleDao

public interface RoleDao {//获取角色列表public List<Role> getRoleList(Connection connection)throws SQLException;
}

RoleDaoImpl

package com.lding.dao.role;import com.lding.dao.BaseDao;
import com.lding.pojo.Role;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;/*** @program: SMBMS* @description:* @author: 王丁* @date: 2021-11-09 15:39**/
public class RoleDaoImpl implements RoleDao{public List<Role> getRoleList(Connection connection) throws SQLException {PreparedStatement pstm = null;ResultSet rs = null;ArrayList<Role> roleList = new ArrayList<Role>();if (connection != null) {String sql = "select * from smbms_role";Object[] params = {};rs = BaseDao.execute(connection, pstm, rs, sql, params);while (rs.next()) {Role _role = new Role();_role.setId(rs.getInt("id"));_role.setRoleCode(rs.getString("roleCode"));_role.setRoleName(rs.getString("roleName"));roleList.add(_role);}BaseDao.closeResource(null, pstm, rs);}return roleList;}
}

RoleService

public interface RoleService {//获取角色列表public List<Role> getRoleList();
}

RoleServiceImpl

public class RoleServiceImpl implements RoleService {//引入Daoprivate RoleDao roleDao;public RoleServiceImpl(){roleDao=new RoleDaoImpl();}public List<Role> getRoleList() {Connection connection = null;List<Role> roleList=null;try {connection=BaseDao.getConnection();roleList = roleDao.getRoleList(connection);} catch (SQLException e) {}finally {BaseDao.closeResource(connection,null,null);}return roleList;}@Testpublic void test(){RoleServiceImpl roleService=new RoleServiceImpl();List<Role> roleList=roleService.getRoleList();for(Role role:roleList){System.out.println(role.getRoleName());}}
}

和之前的实现思路一样,我们先在Dao层实现对数据库中信息的提取,然后在Service层直接调用Dao层中的方法

以角色查询为例,在DaoImpl实现方法中创建PrepareStatement预编译对象,rs结果集对象,
roleList存储角色列表,通过传过来的连接,编写查询role的sql语句,然后执行Dao的execute方法
在这里插入图片描述
得到rs结果集后遍历rs结果集,取出每个role对象,存储到roleList,然后返回roleList

RoleServiceImpl直接调用Dao层中的getRoleList方法
在service层创建与数据库的连接,然后调用getRoleList方法得到查询结果 最后关闭资源
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
拿到前端列表后,将所有数据通过req.setAttribute的方式存入属性中,前端也可以通过这个属性获取。
最后通过请求转发req.getRequestDispatcher(“userlist.jsp”).forward(req,resp);返回前端页面。
UserServlet完整代码

package com.lding.servlet.user;import com.alibaba.fastjson.JSONArray;
import com.lding.pojo.Role;
import com.lding.pojo.User;
import com.lding.service.role.RoleService;
import com.lding.service.role.RoleServiceImpl;
import com.lding.service.user.UserServiceImpl;
import com.lding.util.Constants;
import com.lding.util.PageSupport;
import com.mysql.jdbc.StringUtils;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;/*** @program: SMBMS* @description:* @author: 王丁* @date: 2021-11-06 20:49**/
public class UserServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {String method = req.getParameter("method");if(method!=null&&method.equals("savepwd")){updatePwd(req,resp);}else if(method!=null&&method.equals("pwdmodify")){pwdModify(req,resp);}else if(method.equals("query")&&method!=null){this.query(req,resp);}}//重点难点public void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//查询用户列表//从前端获取数据String queryUserName=req.getParameter("queryname");String temp=req.getParameter("queryUserRole");String pageIndex=req.getParameter("pageIndex");List<User> userList=null;int queryUserRole=0;//获取用户列表UserServiceImpl userService=new UserServiceImpl();//第一次走这个请求,一定是第一页,页面大小是固定的int pageSize=5; //可以把这个写在配置文件中,方便后期修改int currentPageNo=1;if(queryUserName==null){queryUserName="";}if(temp!=null&&!temp.equals("")){queryUserRole=Integer.parseInt(temp);//给查询赋值0,1,2,3}if(pageIndex!=null){currentPageNo = Integer.parseInt(pageIndex);}//获取用户的总数(分页:上一页,下一页)int totalCount= userService.getUserCount(queryUserName, queryUserRole);//总页数支持PageSupport pageSupport=new PageSupport();pageSupport.setCurrentPageNo(currentPageNo);pageSupport.setPageSize(pageSize);pageSupport.setTotalCount(totalCount);int totalPageCount=pageSupport.getTotalPageCount();//控制首页和尾页//如果页面要小于1了,就显示第一页的东西if(currentPageNo<1){currentPageNo=1;}else if(currentPageNo>totalPageCount){currentPageNo=totalPageCount;}//获取用户列表展示userList = userService.getUserList(queryUserName, queryUserRole, currentPageNo, pageSize);req.setAttribute("userList",userList);//拿到角色列表RoleServiceImpl roleService = new RoleServiceImpl();List<Role> roleList = roleService.getRoleList();req.setAttribute("roleList",roleList);req.setAttribute("totalCount",totalCount);req.setAttribute("currentPageNo",currentPageNo);req.setAttribute("totalPageCount",totalPageCount);req.setAttribute("queryUserName",queryUserName);req.setAttribute("queryUserRole",queryUserRole);//返回前端req.getRequestDispatcher("userlist.jsp").forward(req,resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req,resp);}public void updatePwd(HttpServletRequest req, HttpServletResponse resp){//获取参数往上提交//从Session中拿用户IDObject user = req.getSession().getAttribute(Constants.USER_SESSION);String newpassword = req.getParameter("newpassword");boolean flag=false;if(user!=null&&newpassword!=null){UserServiceImpl userService = new UserServiceImpl();flag= userService.updatePwd(((User) user).getId(), newpassword);if(flag){req.setAttribute("message","修改密码成功,请退出重新登陆");//密码修改成功 移除当前Sessionreq.getSession().removeAttribute(Constants.USER_SESSION);//过滤器自动判断Session为null 重新回登陆页面}else{req.setAttribute("message","密码修改失败");}}else{req.setAttribute("message","新密码有问题");}try {req.getRequestDispatcher("pwdmodify.jsp").forward(req,resp);} catch (ServletException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}public void pwdModify(HttpServletRequest req, HttpServletResponse resp){//从Session里面拿IDObject o=req.getSession().getAttribute(Constants.USER_SESSION);String oldpassword=req.getParameter("oldpassword");//万能的Map :结果集Map<String,String> resultMap=new HashMap<String, String>();if(o==null){//Session失效了,session过期了resultMap.put("result","sessionerror");}else if(StringUtils.isNullOrEmpty(oldpassword)){resultMap.put("result","error");}else{String userPassword=((User)o).getUserPassword();//Session中的用户的密码if(oldpassword.equals(userPassword)){resultMap.put("result","true");}else{resultMap.put("result","false");}}resp.setContentType("application/json");try {PrintWriter writer=resp.getWriter();//JSONArray 阿里巴巴的JSON工具类,转换格式/*resultMap=["result","sessionerror"]转化为Json格式={key:value}*/writer.write(JSONArray.toJSONString(resultMap));writer.flush();writer.close();} catch (IOException e) {e.printStackTrace();}}} 

后面其他功能都和用户管理的类似,我就不完全粘代码了,有需要完整工程的在评论区评论发给您~

如果对您有帮助,免费的三连点一个,感谢🙏~


http://chatgpt.dhexx.cn/article/ppQHljcB.shtml

相关文章

【电商开发手册】订单-下单

下单需求 所谓下单&#xff0c;本质上就是买卖双方通过确认一系列信息并且签订电子合同的过程 在电商平台的下单过程中&#xff0c;也需要确定买卖双方的一系列信息&#xff1a; 买方&#xff1a;用户确认收货地址、支付方式、配送方式等等 卖方&#xff1a;卖方需要进行供…

如何规范业务管理过程?低代码平台助力订单管理系统建设

编者按&#xff1a;本文介绍了订单管理系统的概念以及作用&#xff0c;并进一步展现了低代码平台是如何为企业实现订单管理科学化&#xff0c;规范业务管理过程的。 关键词&#xff1a;老厂商&#xff0c;流程管理&#xff0c;订单管理 什么是订单管理系统 订单管理系统(OMS)…

【网课平台】Day13.订单支付模式:生成支付二维码与查询支付

文章目录 一、需求&#xff1a;生成支付二维码1、需求分析2、表设计3、接口定义4、接口实现5、完善controller 二、需求&#xff1a;查询支付结果1、需求分析2、表设计与模型类3、接口定义4、接口实现步骤一&#xff1a;查询支付结果步骤二&#xff1a;保存支付结果&#xff08…

简单的订单系统

目录 一、数据库方面 二、jdbc配置文件 三、JDBC工具类 三、Users类 四、功能实现类 五、运行结果 一、数据库方面 USE foodmenu; DROP TABLE IF EXISTS menu7; CREATE TABLE menu7(num INT PRIMARY KEY,TypeDishes VARCHAR(255))CHARSETutf8; INSERT INTO menu7(num,Type…

天翎低代码平台实现的订单管理系统

编者按&#xff1a; 本文 主要介绍了基于天翎低代码平台实现的 订单管理系统以及 优势 &#xff0c;并进一步展现了低代码平台是如何为企业实现订单管理科学化 和 规范业务管理过程。 关键词&#xff1a;低代码平台、订单管理系统 1.订单管理系统是什么&#xff1f; 订单管理系…

初学订单-支付流程(思路)

主要说的是 生成订单的一系列操作 生成订单号---确认支付---生成支付链接--支付流程 支付流程 ---1.获取支付链接 1.1 三方接口&#xff0c;发送数据 ----1.2 返回数据解析&#xff08;包含支付订单id&#xff09;将链接也返回前端 ----2.进行支付 2.1 扫码支付 2.2 支付成…

订单系统的代码实现

面向接口的编程&#xff1a; 面向接口编程(Interface Oriented Programming:OIP)是一种编程思想&#xff0c;接口作为实体抽象出来的一种表现形式&#xff0c;用于抽离内部实现进行外部沟通&#xff0c;最终实现内部变动而不影响外部与其他实现交互&#xff0c;可以理解成按照这…

【码学堂】教师如何在码学堂上组织教学活动?

码学堂简介 码学堂是由贵州师范学院数学与大数据学院研发的智慧教学平台&#xff0c;学生可以自主练习&#xff0c;教师可以组织练习、考试、竞赛、共享题库、共享教学资源&#xff0c;支持判断题、单项选择题、多项选择题、填空题、程序函数题、程序填空题、编程题、主观题8种…

如何在码学堂组织练习、考试、竞赛?

组织练习、考试、竞赛时就是将多个题目组成题目集&#xff0c;然后加入学生组完成。题目集是由多个题目构成的集合&#xff0c;可以理解为组卷、出卷&#xff0c;码学堂上“练习/作业”、“考试”或“竞赛”操作方式一致&#xff0c;故下面以考试为例来说明操作方法。 1 设置题…

如何开发出一款直播APP项目实践篇 -【原理篇】

【 主要模块】 主播端&#xff1a; 把主播实时录制的视频&#xff0c;经过&#xff08;采集、美颜处理、编码&#xff09;推送到服务器服务器&#xff1a; 处理&#xff08;转码、录制、截图、鉴黄&#xff09;后分发给用户播放端播放器&#xff1a; 获取服务器地址&#xff0…

短视频小视频直播app开发定制解决方案

一、直播APP的市场前景 随着智能移动手机端的普及,人们对于线上的娱乐的要求越发感兴趣,很多互联网电商平台也将直播APP作为销售的主战场之一。将线上与线下的方式相结合才能更好的促进企业的发展。当然对于直播APP的开发也是我们需要了解的。相关数据表明,目前直播APP对于…

直播APP开发过程

直播是2016年火爆的产业&#xff0c;看起来很炫&#xff0c;玩起来很方便、很贴近生活&#xff0c;开发一款直播App不仅耗时还非常昂贵&#xff0c;那么&#xff0c;开发一款直播App到底分几步走&#xff1f; 第一步&#xff1a;分解直播App的功能&#xff0c;我们以X客为例 1…

直播app开发必备五步流程

直播app开发搭建是最近几年比较火的技术&#xff0c;本文从技术角度分析一套直播app开发必备的几个流程。 从主播录制视频开始到最后直播间播放&#xff0c;涉及到的流程包括&#xff1a; 音视频采集—>编码和封装—>推流到流媒体服务器—>流媒体服务器播流分发—&g…

金融直播APP方案开发

分享一下英唐众创开发的金融直播APP解决方案。随着视频直播风靡全球&#xff0c;视频直播已成为众多传统行业和互联网行业争夺的“香饽饽”。金融行业当然也不例外&#xff0c;在当今“互联网”的大时代下&#xff0c;金融行业作为走在前沿的产业&#xff0c;不但开辟出互联网金…

如何开发出一款仿映客直播APP项目实践篇 -【原理篇】

前言&#xff1a;每个成功者多是站在巨人的肩膀上&#xff01;在做直播开发时 碰到了很多问题&#xff0c;在收集了许多人博客的基础上做出来了成功的直播项目并做了整理&#xff0c;并在最后奉上我的全部代码。 其中采用博客的博主开篇在此感谢&#xff0c;本着开源分享的精神…

cmd的炫酷玩法教程

在我们看电影的时候&#xff0c;经常看到黑客在电脑是一顿猛如虎的操作。然后电脑上就出现一系列花里胡哨的画面&#xff0c;其实那种画面我们用cmd的一行代码就能搞定。 第一步 按WinR&#xff0c;输入cmd&#xff0c;打开小黑框。 第二部 如果什么属性都不设置&#xff…

一行代码让你伪装成黑客惊艳世人

今天给大家带来一行代码让你伪装成黑客惊艳世人&#xff0c;保证让你成为学校机房最亮的崽 新建一个文本文档&#xff0c;输入tree c: CtrlS保存 重命名修改后缀名为.bat 这就OK了&#xff0c;不知道这个代码你有没有学废了&#xff01;

小bat大装逼(▼へ▼メ)

直接上代码 echo off cls color echo come!!! color 1a color 2b color 3c color 4d color 5e color 6f color 70 tree d: dir /s %0把代码粘贴到一个【文件名.bat】文件中&#xff0c;例如 复制粘贴完成&#xff0c;别忘记【Ctrls】进行保存操作啊。 然后打开就行了。很疯狂…

使用cmd命令行装逼,让命令行滚动起来

使用cmd命令行装逼&#xff0c;让命令行滚动起来 一、滚动cmd二、清理垃圾总结 一、滚动cmd color a扫描当前所有目录 dir /s二、清理垃圾 创建txt文件 echo offdel/f/s/q %systemdrive%\*.tmp del/f/s/q %systemdrive%\*._mp del/f/s/q %systemdrive%\*.logdel/f/s/q %sys…

(六)C语言入门,代码编程,三子棋游戏【300行】【原创】

文章目录 十二篇文章汇总&#xff0c;独家吐大血整理 编译环境 游戏界面 test6.c game.c game.h​​​​​​​ ​​​​​​​ 编译环境 VS2019 游戏界面 test6.c #include <stdio.h>//std standard input output #include <string.h> #include <game…