美文网首页
2018-03-04 web用户项目:全查找和条件查找用户信息

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

作者: 紫杉叶子 | 来源:发表于2018-03-05 07:56 被阅读0次

笔记如下

  1. 全查找实现
  • list.jsp
<c:if test="${empty customers}">
        对不起,当前没有任何的客户信息存在
</c:if>
    
<c:if test="${not empty customers}">
    <table border="1" align="center" width="100%">
            <tr>
                <th>客户姓名</th>
                <th>客户性别</th>
                <th>客户生日</th>
                <th>客户邮箱</th>
                <th>客户手机</th>
                <th>客户爱好</th>
                <th>客户类型</th>
                <th>客户描述</th>
                <th>操作</th>
            </tr>
            <c:forEach items="${customers}" var="customer">
                <tr>
                    <td>${customer.name}</td>
                    <td>${customer.gender}</td>
                    <td>${customer.birthday}</td>
                    <td>${customer.email}</td>
                    <td>${customer.cellphone}</td>
                    <td>${customer.preference}</td>
                    <td>${customer.type}</td>
                    <td>${customer.description}</td>
                </tr>
            </c:forEach>
            
        </table>
</c:if>
  • web层(FindAllCustomersServlet)
        CustomerService cs = new CustomerService();
        List<Customer> customers  =  cs.findAll();
        
        
        
        //转发请求给list.jsp页面
        request.setAttribute("customers", customers);
        request.getRequestDispatcher("/list.jsp").forward(request, response);
  • 业务层(CustomerService)

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

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

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

    }

2.条件查找实现

  • list.jsp
<form action="${pageContext.request.contextPath }/conditionquery"  method="post">
                
                        请选择查询条件:
                        
                            <select name="conditionname">
                                <option value="name"
                                    <c:if test="">
                                        seclected="selected"
                                    </c:if>
                                >按姓名查询</option>
                                
                                <option value="cellphone"
                                    <c:if test="">
                                        seclected="selected"
                                    </c:if>
                                >按手机号查询</option>
                                <option value="description"
                                    <c:if test="">
                                        seclected="selected"
                                    </c:if>
                                >按描述查询</option>
                            </select>
                            <input type="text" name="conditionvalue" value="${param.conditionvalue}">
                            <input type="submit" value="查询">
                        
        </form>
  • web层(ConditionQueryServlet)
//解决乱码
        request.setCharacterEncoding("utf-8");
        
        //获得查询的条件和查询的条件对应的值
        String conditionname = request.getParameter("conditionname");
        String conditionvalue = request.getParameter("conditionvalue");
        
        //调用业务层查询
        CustomerService cs = new CustomerService();
        
        List<Customer> customers =  cs.conditionQuery(conditionname,conditionvalue);
         
        
        //这里可以复用
        request.setAttribute("customers", customers);
        request.getRequestDispatcher("list.jsp").forward(request, response);
  • 业务层(ConditionQueryServlet)
//条件查询
    public List<Customer> conditionQuery(String conditionname, String conditionvalue) {
        
        return cdao.conditionQuery(conditionname,conditionvalue);
        
    }
  • dao层
//条件查询
    @Override
    public List<Customer> 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>(Customer.class), "%"+conditionvalue+"%");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        
    }

演示:


image.png

相关文章

网友评论

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

      本文链接:https://www.haomeiwen.com/subject/tfvffftx.html