码迷,mamicode.com
首页 > 其他好文 > 详细

多条件查询

时间:2020-01-30 23:20:13      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:beanutils   and   turn   ted   info   dog   class   targe   encoding   

1、流程图

技术图片

 

 因为在定义的JavaBean中没有与之对应的实体(学生姓氏、性别),因此需要创建一个VO(只有姓氏和性别),将表单提交的数据封装到VO实体中,再封装到MAP集合中,将该实体作为参数查询对应的学生集合。因为有两个条件(姓氏和性别),因此,需要用到多条件查询。

2、核心代码

(1)表单页面

<form id="Form1" name="Form1"
      action="${pageContext.request.contextPath}/studentselectservlet"
      method="get">
    学生姓氏:<input type="text" name="sname" value="${studentlist.sname }">&nbsp;&nbsp;
    学生性别:<select id="sex" name="sex">
    <option value="">不限</option>
    <option value="男"></option>
    <option value="女"></option>
    <input type="submit" value="搜索">

(2)Servlet

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        Map<String, String[]> properties = request.getParameterMap();
        Condition condition = new Condition();
        try {
            BeanUtils.populate(condition, properties);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        StudentService studentService=new StudentService();
        List<Student> studentList = null;
        try {
            studentList = studentService.StudentSelectService(condition);/
        } catch (SQLException e) {
            e.printStackTrace();
        }
        request.setAttribute("studentList", studentList);
        request.getRequestDispatcher("/student/list.jsp").forward(request,response);
    }

(3)Dao层:

 public List<Student> selectStudent(Condition condition) throws SQLException {
        Connection con=null;
        con = C3p0Utils.getConnection();
        QueryRunner qr = new QueryRunner();
        List<String> list = new ArrayList<String>();
        String sql = "select * from student where 1=1";
        if(condition.getSname()!=null&&!condition.getSname().trim().equals("")){
            sql+=" and sname like ? ";
            list.add(condition.getSname().trim()+"%");
        }
        if(condition.getSex()!=null&&!condition.getSex().trim().equals("")){
            sql+=" and sex=? ";
            list.add(condition.getSex().trim());
        }
        List<Student> studentList = qr.query(con,sql, new BeanListHandler<Student>(Student.class) , list.toArray());
        return studentList;
    }

(4)jsp:

 <c:forEach items="${studentList}" var="stu" varStatus="vs">
                        <%--forEach语句实现学生信息的罗列,var表示一个迭代的变量,items表示迭代的集合--%>
                        <tr onmouseover="this.style.backgroundColor = ‘white‘"
                            onmouseout="this.style.backgroundColor = ‘#F5FAFE‘;">

                            <td style="CURSOR: hand; HEIGHT: 22px" align="center"
                                width="6%">${stu.studentno}</td>
                            <td style="CURSOR: hand; HEIGHT: 22px" align="center"
                                width="6%">${stu.sname}</td>

                            <td style="CURSOR: hand; HEIGHT: 22px" align="center"
                                width="15%">${stu.classno}</td>
                            <td style="CURSOR: hand; HEIGHT: 22px" align="center"
                                width="15%">${stu.birthday}</td>

                            <td style="CURSOR: hand; HEIGHT: 22px" align="center"
                                width="5%">${stu.sex}</td>
                            <td style="CURSOR: hand; HEIGHT: 22px" align="center"
                                width="10%">${stu.phone}</td>

                            <td style="CURSOR: hand; HEIGHT: 22px" align="center"
                                width="10%">${stu.point}</td>

3、结果

(1)数据库:

技术图片

 

(2)查询结果:

技术图片

 

多条件查询

标签:beanutils   and   turn   ted   info   dog   class   targe   encoding   

原文地址:https://www.cnblogs.com/zhai1997/p/12244073.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!