ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

狂神说Java【SMBMS】——SMBMS超市订单管理系统(七) ——获取角色列表

2022-03-28 09:33:10  阅读:246  来源: 互联网

标签:Java currentPageNo req queryname SMBMS role import 狂神 queryUserRole


 

1、RoleDao接口

package com.thhh.dao.role;

import com.thhh.pojo.Role;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public interface RoleDao {
    /**
     * 1、获取角色列表
     * @param conn:数据库连接对象
     * @return:返回的结构集
     * @throws SQLException
     */
    public List<Role> getRoleList(Connection conn) throws SQLException;
}

2、RoleDaoImpl接口实现

package com.thhh.dao.role;

import com.thhh.dao.BaseDao;
import com.thhh.pojo.Role;
import org.junit.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RoleDaoImpl implements RoleDao{
    //1、获取角色列表

    @Override
    public List<Role> getRoleList(Connection conn) throws SQLException {
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<Role> list = null;//存储角色对象的集合

        if (conn!=null){
            list = new ArrayList<Role>();
            String sql = "SELECT * FROM smbms_role";//直接写死,不用参数
            Object[] params = {};
            rs = BaseDao.executeQuery(sql,params,conn,pstmt,rs);
            while (rs.next()){
                Role role = new Role();
                role.setId(rs.getInt("id"));
                role.setRoleCode(rs.getString("roleCode"));
                role.setRoleName(rs.getString("roleName"));
                list.add(role);
            }
            BaseDao.close(null,pstmt,rs);
        }
        return list;
    }
}

3、RoleService接口

package com.thhh.service.role;

import com.thhh.pojo.Role;

import java.util.List;

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

4、RoleService接口实现

package com.thhh.service.role;

import com.thhh.dao.BaseDao;
import com.thhh.dao.role.RoleDao;
import com.thhh.dao.role.RoleDaoImpl;
import com.thhh.pojo.Role;
import org.junit.Test;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class RoleServiceImpl implements RoleService{
    private RoleDao roleDao = null;

    public RoleServiceImpl() {
        this.roleDao = new RoleDaoImpl();   //servlet中一旦调用这个service,就会实例化该Dao对象
    }

    @Override
    public List<Role> getRoleList() {
        Connection conn = null;//获取连接
        List<Role> roleList = null;
        try {
            conn = BaseDao.getConnection();
            roleList = roleDao.getRoleList(conn);//服务层调用Dao层方法
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            BaseDao.close(conn,null,null);//关闭连接
        }
        return roleList;
    }
}

5、测试

@Test
public void test(){
    RoleService roleService = new RoleServiceImpl();
    List<Role> list = roleService.getRoleList();
    for (Role role:list) {
        System.out.println(role.getId()+"\t"+role.getRoleName()+"\t"+role.getRoleCode());
    }
}

6、编写servlet

href="${pageContext.request.contextPath }/jsp/user.do?method=query"

/jsp/user.do
//这个URL映射的servlet我们在前面已经注册过了,所以区别就是它后面跟的参数
    
method=query
这个参数就是在传递指定servlet应该调用的方法

​ 这一次编写的servlet比较的复杂,我们把它单独拎出来看

    //3、按照用户名/职位名称查询用户列表或整表查询
    //【重点&难点】
    public void quary(HttpServletRequest req, HttpServletResponse resp){
        //1、从前端获取数据
        String queryname = req.getParameter("queryname");
        String queryUserRole = req.getParameter("queryUserRole");
        String pageIndex = req.getParameter("pageIndex");//通过隐藏域进行的提交,默认 = 1

        int UserRole = 0;//我们先让UserRole = 0,因为从前端接收到的queryUserRole可能就是一个NULL,此时我们就需要将其指定为0
        int pageSize = 5;//这个数字最好是写在配置文件中,这样以后想要修改一页上面显示的行数,我们就不用再从新编译代码和测试了
        int currentPageNo = 1;//先给当前页设置一个默认的值

        //2、通过判断参数决定哪些请求需要处理
        if (queryname == null){
            queryname = "";//如果前端没有按照用户名查询,我们就将用户名设置""
        }
        if (queryUserRole!=null && queryUserRole!=""){
            UserRole = Integer.parseInt(queryUserRole);//当前端传过来的queryUserRole有数据的时候我们才对其进行解析
        }
        if (pageIndex!=null){
            currentPageNo = Integer.parseInt(pageIndex);
        }

        //3、为了实现分页,需要使用工具类PageSupport并传入总用户数,计算出totalPageCount
        //参数totalCount由getUserCount得出;pageSize是固定死了的;currentPageNo默认设为1
        UserService userService = new UserServiceImpl();
        int totalCount = userService.getUserCount(queryname,UserRole);
        //使用最开始导入的工具类
        PageSupport pageSupport = new PageSupport();
        pageSupport.setPageSize(pageSize);//设置一页多少行数据
        pageSupport.setTotalCount(totalCount);//设置总页数
        pageSupport.setCurrentPageNo(currentPageNo);//设置当前页数


        int totalPageCount = 0;
        totalPageCount = pageSupport.getTotalPageCount();

        //4、控制翻页
        if (currentPageNo<1){//在第一页的时候还想点击上一页
            currentPageNo = 1;
        }else if(currentPageNo>pageSupport.getTotalPageCount()) {//在第最后一页的时候还想点击下一页
            currentPageNo = totalPageCount;
        }

        //5、用户列表展示
        List<User> userList = userService.getUserList(queryname,UserRole,currentPageNo,pageSize);
        //将集合返回给前端进行解析显示
        req.setAttribute("userList",userList);

        //6、角色列表展示
        List<Role> roleList = new RoleServiceImpl().getRoleList();
        req.setAttribute("roleList",roleList);

        //7、将参数返回前端
        req.setAttribute("queryUserName",queryname);
        req.setAttribute("queryUserRole",queryUserRole);
        req.setAttribute("totalPageCount",totalPageCount);
        req.setAttribute("totalCount",totalCount);
        req.setAttribute("currentPageNo",currentPageNo);

        //8、重定向刷新页面
        try {
            System.out.println("=======================进入到servlet,且i调用method = query");
            req.getRequestDispatcher("userlist.jsp").forward(req,resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

​ 首先,这个servlet直接和前端的用户管理页面进行交互,且用户管理页面所有展示的数据都是由这个servlet的query()返回的,所以我们应该按照前端素材来编写servlet的query()




​ 通过上面的分析我们可以发现:我们需要从前端页面获取3个参数,并给前端返回7个参数

  • 获取前端参数
    • queryname:按照姓名查询的姓名
    • queryUserRole:按照职位查询的职位名称
    • pageIndex:当前的页面index
  • 传递给前端的参数
    • userList:存储前端展示的用户(user)对象集合
    • roleList:存储前端展示的角色(role)对象集合
    • queryname:再将这个参数传递回去是为了前端搜索之后搜索框中还保留着用户搜索的值
    • queryUserRole:作用同queryname
    • totalCount:总共多少条用户记录
    • currentPageNo:当前所在页数
    • totalPageCount:总页数参数,注意:这个参数来自于工具类pageSupport
//1、从前端获取数据
String queryname = req.getParameter("queryname");
String queryUserRole = req.getParameter("queryUserRole");
String pageIndex = req.getParameter("pageIndex");//通过隐藏域进行的提交,默认 = 1

int UserRole = 0;//我们先让UserRole = 0,因为从前端接收到的queryUserRole可能就是一个NULL,此时我们就需要将其指定为0
int pageSize = 5;//这个数字最好是写在配置文件中,这样以后想要修改一页上面显示的行数,我们就不用再从新编译代码和测试了
int currentPageNo = 1;//先给当前页设置一个默认的值
//2、通过判断参数决定哪些请求需要处理
if (queryname == null){
    queryname = "";//如果前端没有按照用户名查询,我们就将用户名设置""
}
if (queryUserRole!=null && queryUserRole!=""){
    UserRole = Integer.parseInt(queryUserRole);//当前端传过来的queryUserRole有数据的时候我们才对其进行解析
}
if (pageIndex!=null){
    currentPageNo = Integer.parseInt(pageIndex);
}
//3、为了实现分页,需要使用工具类PageSupport并传入总用户数,计算出totalPageCount
//参数totalCount由getUserCount得出;pageSize是固定死了的;currentPageNo默认设为1
UserService userService = new UserServiceImpl();
int totalCount = userService.getUserCount(queryname,UserRole);
//使用最开始导入的工具类
PageSupport pageSupport = new PageSupport();
pageSupport.setPageSize(pageSize);//设置一页多少行数据
pageSupport.setTotalCount(totalCount);//设置总页数
pageSupport.setCurrentPageNo(currentPageNo);//设置当前页数


int totalPageCount = 0;
totalPageCount = pageSupport.getTotalPageCount();
//4、控制翻页
if (currentPageNo<1){//在第一页的时候还想点击上一页
    currentPageNo = 1;
}else if(currentPageNo>pageSupport.getTotalPageCount()) {//在第最后一页的时候还想点击下一页
    currentPageNo = totalPageCount;
}
//5、用户列表展示
List<User> userList = userService.getUserList(queryname,UserRole,currentPageNo,pageSize);
//将集合返回给前端进行解析显示
req.setAttribute("userList",userList)
//6、角色列表展示
List<Role> roleList = new RoleServiceImpl().getRoleList();
req.setAttribute("roleList",roleList);
//7、将参数返回前端
req.setAttribute("queryUserName",queryname);
req.setAttribute("queryUserRole",queryUserRole);
req.setAttribute("totalPageCount",totalPageCount);
req.setAttribute("totalCount",totalCount);
req.setAttribute("currentPageNo",currentPageNo);
//8、重定向刷新页面
try {
    System.out.println("=======================进入到servlet,且i调用method = query");
    req.getRequestDispatcher("userlist.jsp").forward(req,resp);
} catch (ServletException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

7、测试




测试完成!

标签:Java,currentPageNo,req,queryname,SMBMS,role,import,狂神,queryUserRole
来源: https://www.cnblogs.com/yayuya/p/16065599.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有