2018-03-04 web用户项目:全查找和条件查找用户信息

笔记如下

  1. 全查找实现
  • list.jsp

        对不起,当前没有任何的客户信息存在

    

    
                
客户姓名 客户性别 客户生日 客户邮箱 客户手机 客户爱好 客户类型 客户描述 操作
${customer.name} ${customer.gender} ${customer.birthday} ${customer.email} ${customer.cellphone} ${customer.preference} ${customer.type} ${customer.description}
  • web层(FindAllCustomersServlet)
        CustomerService cs = new CustomerService();
        List customers  =  cs.findAll();
        
        
        
        //转发请求给list.jsp页面
        request.setAttribute("customers", customers);
        request.getRequestDispatcher("/list.jsp").forward(request, response);
  • 业务层(CustomerService)

    //查询所有额度客户信息
    public List findAll() {

        //调用dao
    
        return cdao.getAll();
    }

  • dao层(CustomerDaoImpl)
//查询所有客户信息
    /* (non-Javadoc)
     * @see com.chen.customers.dao.CustomerDao#getAll()
     */
    @Override
    public List getAll() {
        
        QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource());
        try {
            return runner.query("select * from customers", new BeanListHandler(Customer.class));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

2.条件查找实现

  • list.jsp
请选择查询条件:
  • web层(ConditionQueryServlet)
//解决乱码
        request.setCharacterEncoding("utf-8");
        
        //获得查询的条件和查询的条件对应的值
        String conditionname = request.getParameter("conditionname");
        String conditionvalue = request.getParameter("conditionvalue");
        
        //调用业务层查询
        CustomerService cs = new CustomerService();
        
        List customers =  cs.conditionQuery(conditionname,conditionvalue);
         
        
        //这里可以复用
        request.setAttribute("customers", customers);
        request.getRequestDispatcher("list.jsp").forward(request, response);
  • 业务层(ConditionQueryServlet)
//条件查询
    public List conditionQuery(String conditionname, String conditionvalue) {
        
        return cdao.conditionQuery(conditionname,conditionvalue);
        
    }
  • dao层
//条件查询
    @Override
    public List conditionQuery(String conditionname, String conditionvalue) {
        
        QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource());
        
        //条件查询
        
        String sql = "select * from customers where  " + conditionname +" like ?" ;
        
        try {
            return runner.query(sql, new BeanListHandler(Customer.class), "%"+conditionvalue+"%");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        
    }

演示:


2018-03-04 web用户项目:全查找和条件查找用户信息_第1张图片
image.png

你可能感兴趣的:(2018-03-04 web用户项目:全查找和条件查找用户信息)