超市订单管理系统---用户管理实现2

3、获取角色列表

为了我们职责统一,可以把角色的操作单独放在一个包中,和pojo类对应

  1. RoleDao

    package com.tr.dao.role;
    
    import com.tr.pojo.Role;
    
    import java.sql.Connection;
    import java.sql.SQLException;
    import java.util.List;
    
    /**
     * @author TRasdzz
     * @version 1.0
     * @since create in 2021/5/12 20:14
     */
    
    public interface RoleDao {
        //获取角色列表
        public List<Role> getRoleList(Connection connection) throws SQLException;
    }
    
    
  2. RoleDaoImpl

    package com.tr.dao.role;
    
    import com.tr.dao.BaseDao;
    import com.tr.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;
    
    /**
     * @author TRasdzz
     * @version 1.0
     * @since create in 2021/5/12 20:53
     */
    
    
    public class RoleDaoImpl implements RoleDao{
        //获取角色列表
        @Override
        public List<Role> getRoleList(Connection connection) throws SQLException {
            PreparedStatement st = null;
            String sql = "select * from smbms_role";
            ResultSet rs = null;
            List<Role> roleList = new ArrayList<Role>();
            
            if (connection != null){
                Object[] params = {};
                rs = BaseDao.execute(connection, st, 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.release(connection,st,rs);
            }
            return roleList;
        }
    }
    
  3. RoleService

    package com.tr.service.role;
    
    import com.tr.pojo.Role;
    
    import java.util.List;
    
    /**
     * @author TRasdzz
     * @version 1.0
     * @since create in 2021/5/13 8:42
     */
    
    public interface RoleService {
        
        //获取角色列表
        public List<Role> getRoleList();
        
    }
    
  4. RoleServiceImpl

    package com.tr.service.role;
    
    import com.tr.dao.BaseDao;
    import com.tr.dao.role.RoleDao;
    import com.tr.dao.role.RoleDaoImpl;
    import com.tr.pojo.Role;
    
    import java.sql.Connection;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * @author TRasdzz
     * @version 1.0
     * @since create in 2021/5/13 8:44
     */
    
    public class RoleServiceImpl implements RoleService {
        
        private RoleDao roleDao;
        
        public RoleServiceImpl() {
            roleDao = new RoleDaoImpl();
        }
        
        @Override
        public List<Role> getRoleList() {
            Connection connection = null;
            List<Role> roleList = new ArrayList<Role>();
            try {
               connection = BaseDao.getConnection();
               roleList = roleDao.getRoleList(connection);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                BaseDao.release(connection,null,null);
            }
            
            return roleList;
        }
        
    }
    

4、用户显示的servlet

  1. 获取用户前端的数据(查询)
  2. 判断请求是否需要执行,看参数的值判断
  3. 为了实现分页,需要计算出当前页和总页数,页面大小等
  4. 用户列表展示
  5. 返回前端
//查询用户列表以及角色列表
//重点  难点
public void query(HttpServletRequest req, HttpServletResponse resp) {

    //获取数据
    String queryUserName = req.getParameter("queryname");
    String queryUserRoleTemp = req.getParameter("queryUserRole");
    String pageIndexTemp = req.getParameter("pageIndex");
    int totalPageCount = 0;
    int queryUserRole = 0;

    UserServiceImpl userService = new UserServiceImpl();
    List<User> userList = new ArrayList<User>();

    //第一次走这个请求,一定是第一页,页面大小是固定的;
    int pageSize = 5;//可以把这个信息写到配置文件中,方便后器做修改;
    int currentPageNo = 1;

    if (queryUserName == null) {
        queryUserName = "";
    }

    if (!StringUtils.isNullOrEmpty(queryUserRoleTemp)) {
        queryUserRole = Integer.parseInt(queryUserRoleTemp); //给查询赋值!
    }

    //现在的页码
    if (pageIndexTemp != null){
        currentPageNo = Integer.parseInt(pageIndexTemp);
    }

    //获取用户的总数(分页: 上一页 下一页)
    int totalCount = userService.getUserCount(queryUserName, queryUserRole);

    //总页数支持
    PageSupport pageSupport = new PageSupport();
    pageSupport.setPageSize(pageSize);//页面显示数量
    pageSupport.setCurrentPageNo(currentPageNo);//现在的页码
    pageSupport.setTotalCount(totalCount);//总数量

    //控制首页和尾页


    totalPageCount = pageSupport.getTotalPageCount();//获取总页码

    if (currentPageNo < 1 ){
        currentPageNo = 1;
    }

    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("totalPageCount",totalPageCount);
    req.setAttribute("totalCount",totalCount);
    req.setAttribute("currentPageNo",currentPageNo);
    req.setAttribute("totalPageCount", totalPageCount);
    req.setAttribute("queryUserName",queryUserName);
    req.setAttribute("queryUserRole",queryUserRole);


    try {
        req.getRequestDispatcher("/jsp/userlist.jsp").forward(req,resp);
    } catch (ServletException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

最后 附上项目源码地址:gitee
项目后续的功能我没有继续实现基本上就是增删改查的重复书写

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值