JavaWeb:用户信息管理系统总结

JavaWeb:用户信息管理系统总结,第1张

当当当当~~~~欢迎大家阅读,今天我们总结用户信息管理系统的流程

该系统主要分为两部分,一部分是简单功能(增删改查)的基础实现;

一部分是复杂功能删除选中,分页查询,复杂条件查询的实现;

简单功能

首先我们来看一下登录界面

一:登录 (一):登录界面 1:html登录界面

2:html代码 
  
  	
  		管理员登录
        
		
		
	  	
		  
		   登录失败!
		
  	
  
3:改进后的jsp登录界面

4:页面代码的改进

在html基础上添加了servlet与后端交互,并且在登录时插入了验证码的校验



(二):后端代码实现 1:添加功能的思路分析 

2:AddUserServlet
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.设置编码
        request.setCharacterEncoding("utf-8");
        //2.获取参数
        Map map = request.getParameterMap();
        //3.封装对象
        User user = new User();
        try {
            BeanUtils.populate(user,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        //4.调用Service保存
        UserService service = new UserServiceImpl();
        service.addUser(user);

        //5.跳转到userListServlet
            response.sendRedirect(request.getContextPath()+"/findUserByPageServlet");
    }
3:UserServiceImpl(addUser)
    @Override
    public void addUser(User user) {
        dao.add(user);
    }
4:UserDaoImpl(add)
 @Override
    public void add(User user) {
        //1.定义sql
        String sql = "insert into user values(null,?,?,?,?,?,?,null,null)";
        //2.执行sql
        template.update(sql, user.getName(), user.getGender(), user.getAge(), user.getAddress(), user.getQq(), user.getEmail());
    }
四:删除单个用户信息 (一):添加删除按钮 
 删除
(二):后端代码实现 1:删除功能的思路分析

2:DelUserServlet(先获取id,通过id,删除用户信息)
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取id
        String id = request.getParameter("id");
        //2.调用service删除
        UserService service = new UserServiceImpl();
        service.deleteUser(id);

        //3.跳转到查询所有Servlet
        response.sendRedirect(request.getContextPath()+"/findUserByPageServlet");
}

3:UserServiceImpl(deleteUser)
    @Override
    public void deleteUser(String id) {
        dao.delete(Integer.parseInt(id));
    }
4:UserDaoImpl(delete) 
 @Override
    public void delete(int id) {
        //1.定义sql
        String sql = "delete from user where id = ?";
        //2.执行sql
        template.update(sql, id);
}
 五:修改用户信息 (一):修改用户信息界面  1:html界面

2:html代码

        
        修改联系人
        
        
    
3:改进后的jsp修改界面

 4:页面改进后的代码

添加可以自由选择性别和籍贯的判断


        
        修改联系人
        
        
    
(二):后端代码实现 1:修改功能的思路分析

2:FindUserServlet
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取id
        String id = request.getParameter("id");
        //2.调用Service查询
        UserService service = new UserServiceImpl();
        User user = service.findUserById(id);

        //3.将user存入request
        request.setAttribute("user",user);
        //4.转发到update.jsp
        request.getRequestDispatcher("/update.jsp").forward(request,response);
    }
3:UpdateUserServlet
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.设置编码
        request.setCharacterEncoding("utf-8");
        //2.获取map
        Map map = request.getParameterMap();
        //3.封装对象
        User user = new User();
        try {
            BeanUtils.populate(user,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        //4.调用Service修改
        UserService service = new UserServiceImpl();
        service.updateUser(user);

        //5.跳转到查询所有Servlet
        response.sendRedirect(request.getContextPath()+"/findUserByPageServlet");
    }
4:UserServiceImpl(updateUser)
 @Override
    public void updateUser(User user) {
        dao.update(user);
    }
5:UserDaoImpl(update)
@Override
    public void update(User user) {
        String sql = "update user set name = ?,gender = ? ,age = ? , address = ? , qq = ?, email = ? where id = ?";
        template.update(sql, user.getName(), user.getGender(), user.getAge(), user.getAddress(), user.getQq(), user.getEmail(), user.getId());
    }
复杂功能 一:删除选中  (一):删除选中界面 1:在list页面添加删除选中的按钮,并且给删除选中添加单击事件
    
        删除选中 
        

     
            window.onload = function(){
            //给删除选中按钮添加单击事件
            document.getElementById("delSelected").onclick = function(){
                if(confirm("您确定要删除选中条目吗?")){
                   var flag = false;
                    //判断是否有选中条目
                    var cbs = document.getElementsByName("uid");
                    for (var i = 0; i < cbs.length; i++) {
                        if(cbs[i].checked){
                            //有一个条目选中了
                            flag = true;
                            break;
                        }
                    }
                    if(flag){//有条目被选中
                        //表单提交
                        document.getElementById("form").submit();
                    }
                }
            }
          }
2:删除选中时可以添加全部选中

 //1.获取第一个cb
            document.getElementById("firstCb").onclick = function(){
                //2.获取下边列表中所有的cb
                var cbs = document.getElementsByName("uid");
                //3.遍历
                for (var i = 0; i < cbs.length; i++) {
                    //4.设置这些cbs[i]的checked状态 = firstCb.checked
                    cbs[i].checked = this.checked;
                }
            }
(二):后端代码实现 1:删除选中的思路分析

2:DelSelectedServlet
public class DelSelectedServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取所有id
        String[] ids = request.getParameterValues("uid");
        //2.调用service删除
        UserService service = new UserServiceImpl();
        service.delSelectedUser(ids);

        //3.跳转查询所有Servlet
        response.sendRedirect(request.getContextPath()+"/findUserByPageServlet");
    }
3:UserService(delSelectedUser)
  //删除选中用户
    @Override
    public void delSelectedUser(String[] ids) {
        if(ids != null && ids.length > 0){
            //1.遍历数组
            for (String id : ids) {
                //2.调用dao删除
                dao.delete(Integer.parseInt(id));
            }
        }
    }
二:分页查询 (一):分页查询页面  1:在list.jsp中添加组件分页栏

        
(二):后端代码实现 1:分页查询实体类的思路分析

1:添加一个分页查询相关的类

public class PageBean {
    private int totalCount; // 总记录数
    private int totalPage ; // 总页码
    private List list ; // 每页的数据
    private int currentPage ; //当前页码
    private int rows;//每页显示的记录数
2:分页查询的逻辑思路分析 3:FindUserByPageServlet
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        //1.获取参数
        String currentPage = request.getParameter("currentPage");//当前页码
        String rows = request.getParameter("rows");//每页显示条数

        if(currentPage == null || "".equals(currentPage)){

            currentPage = "1";
        }
        if(rows == null || "".equals(rows)){
            rows = "5";
        }
        //获取条件查询参数
        Map condition = request.getParameterMap();
        //2.调用service查询
        UserService service = new UserServiceImpl();
        PageBean pb = service.findUserByPage(currentPage,rows,condition);
        System.out.println(pb);
        //3.将PageBean存入request
        request.setAttribute("pb",pb);
        request.setAttribute("condition",condition);//将查询条件存入request
        //4.转发到list.jsp
        request.getRequestDispatcher("/list.jsp").forward(request,response);
    }
4:UserService(findUserByPage)
//分页条件查询
    @Override
    public PageBean findUserByPage(String _currentPage, String _rows, Map condition) {

        int currentPage = Integer.parseInt(_currentPage);
        int rows = Integer.parseInt(_rows);

        //第1页的上一页永远返回1
        if(currentPage <=0) {
            currentPage = 1;
        }

        //1.创建空的PageBean对象
        PageBean pb = new PageBean();
        //2.设置参数
        pb.setCurrentPage(currentPage);
        pb.setRows(rows);

        //3.调用dao查询总记录数
        int totalCount = dao.findTotalCount(condition);
        pb.setTotalCount(totalCount);
        //4.调用dao查询List集合
        //计算开始的记录索引
        int start = (currentPage - 1) * rows;
        List list = dao.findByPage(start,rows,condition);
        pb.setList(list);

        //5.计算总页码
        int totalPage = (totalCount % rows)  == 0 ? totalCount/rows : (totalCount/rows) + 1;
        pb.setTotalPage(totalPage);


        return pb;
    }
5:UserDaoImpl(findTotalCount)
//查询总记录数
    @Override
    public int findTotalCount() {
        //1.定义模板初始化sql
        String sql = "select count(*) from user";
      
        return template.queryForObject(sql,Integer.class);
    }
6:UserDaoImpl(findByPage) 
 //分页条件查询
    @Override
    public List findByPage(int start, int rows) {
        String sql = "select * from user limit?,? ;
        return template.query(sql,new BeanPropertyRowMapper,start,rows);
    }
三:复杂条件查询 (一):复杂条件查询界面 1:复杂条件查询的界面展示 

 2:复杂条件查询的jsp代码


        

(二):后端代码实现 1:复杂条件查询的思路分析

2:在分页查询的FindUserByPageServlet基础上查询复杂条件的参数集合并传参
//获取条件查询参数
        Map condition = request.getParameterMap();
//2.调用service查询
        UserService service = new UserServiceImpl();
        PageBean pb = service.findUserByPage(currentPage,rows,condition);
        System.out.println(pb);
3:在分页查询的UserService(findUserByPage) 的基础上传参
//3.调用dao查询总记录数
        int totalCount = dao.findTotalCount(condition);
        pb.setTotalCount(totalCount);
        //4.调用dao查询List集合
        //计算开始的记录索引
        int start = (currentPage - 1) * rows;
        List list = dao.findByPage(start,rows,condition);
        pb.setList(list);
4:在分页查询的UserDaoImpl(findTotalCount)基础上判断复杂条件查询是否有值并且获取值
//查询总记录数
    @Override
    public int findTotalCount(Map condition) {
        //1.定义模板初始化sql
        String sql = "select count(*) from user where 1 = 1 ";
        StringBuilder sb = new StringBuilder(sql);
        //2.遍历map
        Set keySet = condition.keySet();
        //定义参数的集合
        List params = new ArrayList();
        for (String key : keySet) {

            //排除分页条件参数
            if("currentPage".equals(key) || "rows".equals(key)){
                continue;
            }

            //获取value
            String value = condition.get(key)[0];
            //判断value是否有值
            if(value != null && !"".equals(value)){
                //有值
                sb.append(" and "+key+" like ? ");
                params.add("%"+value+"%");//?条件的值
            }
        }
        System.out.println(sb.toString());
        System.out.println(params);

        return template.queryForObject(sb.toString(),Integer.class,params.toArray());
    }
 
5:在分页查询的UserDaoImpl(findByPage) 基础上遍历复杂条件查询的集合并为查询到的值做分页处理 
 //分页条件查询
    @Override
    public List findByPage(int start, int rows, Map condition) {
        String sql = "select * from user  where 1 = 1 ";

        StringBuilder sb = new StringBuilder(sql);
        //2.遍历map
        Set keySet = condition.keySet();
        //定义参数的集合
        List params = new ArrayList();
        for (String key : keySet) {

            //排除分页条件参数
            if("currentPage".equals(key) || "rows".equals(key)){
                continue;
            }

            //获取value
            String value = condition.get(key)[0];
            //判断value是否有值
            if(value != null && !"".equals(value)){
                //有值
                sb.append(" and "+key+" like ? ");
                params.add("%"+value+"%");//?条件的值//模糊查询
            }
        }

        //添加分页查询
        sb.append(" limit ?,? ");
        //添加分页查询参数值
        params.add(start);
        params.add(rows);
        sql = sb.toString();
        System.out.println(sql);
        System.out.println(params);

        return template.query(sql,new BeanPropertyRowMapper(User.class),params.toArray());
    } 

以上就是用户信息管理系统的内容啦,希望我的文章对你有所帮助,如果有错误的地方还望大家批评指正,谢谢大家阅读!  

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1321975.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-12
下一篇 2022-06-12

发表评论

登录后才能评论

评论列表(0条)