smbms项目心得day2

用户管理实现

思路:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5szkeaFI-1586516515323)(C:\Users\you\AppData\Roaming\Typora\typora-user-images\image-20200410092258571.png)]

1.导入分页的工具类

2.用户列表页面导入

useradd.jsp
rollpage.jsp

1.获取用户数量

(select count(1) as count from smbms_user u,smbms_role r where u.userRole=r.id 连表查询)

1.USerDao

 //根据用户名或者角色查询用户总数
    public int getUserCount(Connection connection,String username,int userRole) throws SQLException;
}

2.USerDaoImpl

//根据用户名或者角色查询用户总数
    public int getUserCount(Connection connection, String username, int userRole) throws SQLException {
        PreparedStatement preparedStatement=null;
        ResultSet  resultSet= null;
        int count = 0;
        if(connection!=null){
            StringBuffer sql = new StringBuffer();
            //这里使用StringBuffer是因为这个sql是可变的 要做到随时添加条件语句
            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>();//用arrayList传递参数 (万能)
            if (username!=null) {
            //如果用户名不为空 就添加用户名查找的条件
            sql.append(" and u.userName like ?");
            list.add("'"+"%"+username+"%"+"'");//模糊查询条件(LIKE '%三%')
            }
            if (userRole>0){
                sql.append(" and u.userRole = ?");
                list.add(userRole);
            }
            //将list转换为数组
            Object[] params = list.toArray();
            //输出完整的sql语句
            System.out.println("sql:"+sql);//方便调试
            //执行sql
            ResultSet rs = BaseDao.executeQuery(connection, preparedStatement, resultSet, sql.toString(), params);
            if (rs.next()){
                count = rs.getInt("count");//sql返回的是count 拿count即可
            }
            BaseDao.closeResource(null,preparedStatement,rs);
        }return  count;
    }
}

3.UserService

  //通过用户名或者用户角色查询全部总数
    public int Usercount( String username, int userRole) throws SQLException;
}

4.UserServiceImpl

    public int Usercount(String username, int userRole) {
        //调用base获取连接
        Connection connection = BaseDao.getconnection();
        int userCount =0;
        //调用UserDao完成数据库的操作
        try {
            userCount = userDao.getUserCount(connection,username,userRole);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //关闭资源
            BaseDao.closeResource(connection,null,null);
        }return  userCount;
    }
//    //测试一下功能
    @Test
    public  void test(){
        UserServiceImpl userService = new UserServiceImpl();
        int usercount = userService.Usercount(null, 2);
        System.out.println(usercount);
    }(测试成功)
}

2.获取用户列表

UserDao

//通过条件查询用户列表
    public List<User> getUserList(Connection connection, String userName, int userRole, int currentPageNo, int pageSize)throws Exception;
}

UserDaoImpl

 public List<User> getUserList(Connection connection, String userName, int userRole, int currentPageNo, int pageSize) throws Exception {
        PreparedStatement 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")
            ArrayList<Object> list = new ArrayList<Object>();
            if(userName!=null){
            //通过userName查询
            sql.append(" and u.userName like ?");//前面要带一个空格
            list.add("'"+"%"+username+"%"+"'");//模糊查询条件(LIKE '%三%')
        }
        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 List<User> getUserList(String queryUserName, int queryUserRole, int currentPageNo, int pageSize);

UserServiceImpl

public List<User> getUserList(String queryUserName,int queryUserRole,int currentPageNo, int pageSize) {
        // TODO Auto-generated method stub
        Connection 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 block
            e.printStackTrace();
        }finally{
            BaseDao.closeResource(connection, null, null);
        }
        return userList;
    }

3.获取角色操作

RoleDao

public interface RoleDao {
    public List<Role> getRoleList(Connection connection) throws SQLException;
}

RoleDaoImpl

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[] = {};
            ResultSet resultSet = BaseDao.executeQuery(connection, pstm, rs, sql, params);
            while (resultSet.next()) {
                Role role = new Role();
                role.setRoleName(resultSet.getString("roleName"));
                role.setId(resultSet.getInt("id"));
                role.setRoleCode(rs.getString("roleCode"));
                roleList.add(role);
            }
            BaseDao.closeResource(null, pstm, resultSet);
        }
        return roleList;
    }
}

Roleservice

public interface RoleService {
    public List<Role> getRoleList();
}

RoleServiceImpl

public class RoleSerivceImpl implements RoleService {
    //引入dao
    private RoleDao roleDao;

    public RoleSerivceImpl() {
         roleDao = new RoleDaoImpl();
    }

    public List<Role> getRoleList() {
        List<Role> roleList =null;
        Connection connection = null;
        connection = BaseDao.getconnection();
        try {
            roleList = roleDao.getRoleList(connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
    return roleList;
    }
}

4.用户显示的servlet

1、获取用户前端的数据
2、判断用户请求是否执行
3、为了实现分页需要计算出当前页面,总页面、页面大小
4、用户列表展示
5、返回前端
 //通过用户名或者角色名查找用户(重难点)
    public void query(HttpServletRequest req, HttpServletResponse resp){
        //获取前端传过来的数据
        String queryName = req.getParameter("queryname");
        String Temp = req.getParameter("queryUserRole");
        String pageIndex = req.getParameter("pageIndex");
        int queryUserRole = 0;
        //获取用户列表
        UserServiceImpl userService = new UserServiceImpl();
        //第一次走这个请求一定是第一页,并且页面大小固定
        int pageSize= 5 ;//可以写进配置文件 方便后期修改;
        int currentPageNo = 1;

        if(queryName == null){
            queryName=null;
        }
        if (Temp!=null && !Temp.equals("")){
            queryUserRole = Integer.parseInt(Temp);//给查询赋值 0,1,2,3
        }
        if(pageIndex!=null){
            currentPageNo = Integer.parseInt(pageIndex);//设置当前页码
        }
        //获取用户端额总数(分页:上一页 下一页)
        int totalCount = userService.Usercount(queryName, queryUserRole);
        System.out.println(totalCount);
        //总页数支持
        PageSupport pageSupport= new PageSupport();
        pageSupport.setCurrentPageNo(currentPageNo);//设置当前页码的数量
        pageSupport.setPageSize(pageSize);          //获取页面大小
        pageSupport.setTotalPageCount(totalCount);  //获取总页数
        int totalpageCount = (int)(totalCount/pageSize)+1;//总页数
        //控制首页和尾页
        if(currentPageNo<1){//如果用户想设置当前页小于1
            currentPageNo=1;//强制将页面转换成1
        }
//        if (currentPageNo>totalpageCount){//如果用户设置当前页大于了最大页
//            currentPageNo=totalpageCount;//将当前页直接转换成最大页
//        }
//        展示用户列表
        List<User> userList = userService.getUserList(queryName, queryUserRole, currentPageNo, pageSize);
        req.setAttribute("userList",userList);

        //展示角色列表
        RoleSerivceImpl roleSerivce = new RoleSerivceImpl();
        List<Role> roleList = roleSerivce.getRoleList();
        req.setAttribute("roleList",roleList);
        //把分页信息传回去
        req.setAttribute("totalCount",totalCount);
        req.setAttribute("currentPageNo",currentPageNo);
        req.setAttribute("totalPageCount",totalpageCount);
        //返回前端
        try {
            req.getRequestDispatcher("userlist.jsp").forward(req,resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

5.添加用户

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值