jsp 多条件组合查询

时间:2022-06-21 02:54:48

web层:

    public String query(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        /*
         * 1. 封装表单数据到Customer对象中,它只有四个属性(cname、gender、cellphone、email)
         *   它就是一个条件
         * 2. 使用Customer调用service方法,得到List<Customer>
         * 3. 保存到request域中
         * 4. 转发到list.jsp
         */
        Customer criteria = CommonUtils.toBean(request.getParameterMap(), Customer.class);
        List<Customer> cstmList = customerService.query(criteria);
        request.setAttribute("cstmList", cstmList);
        return "/list.jsp";
    }

 

service层:

    /**
     * 多条件组合查询
     * @param criteria
     * @return
     */
    public List<Customer> query(Customer criteria) {
        return customerDao.query(criteria);
    }

domain层:

/**
 * 领域对象 与表单和数据库表对应
 * 
 * @author cxf
 * 
 */
public class Customer {
    /*
     * 对应数据库表
     */
    private String cid;// 主键
    private String cname;// 客户名称
    private String gender;// 客户性别
    private String birthday;// 客户生日
    private String cellphone;// 客户手机
    private String email;// 客户邮箱
    private String description;// 客户的描述

    public String getCid() {
        return cid;
    }

    public void setCid(String cid) {
        this.cid = cid;
    }

    public String getCname() {
        return cname;
    }

    public void setCname(String cname) {
        this.cname = cname;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    public String getCellphone() {
        return cellphone;
    }

    public void setCellphone(String cellphone) {
        this.cellphone = cellphone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public String toString() {
        return "Customer [cid=" + cid + ", cname=" + cname + ", gender="
                + gender + ", birthday=" + birthday + ", cellphone="
                + cellphone + ", email=" + email + ", description="
                + description + "]";
    }
}

 

 

dao层:

 /**

     * 多条件组合查询
     * @param criteria
     * @return
     */
    public List<Customer> query(Customer criteria) {
        try {
            /*
             * 1. 给出sql模板
             * 2. 给出参数
             * 3. 调用query方法,使用结果集处理器:BeanListHandler
             */
            /*
             * 一、 给出sql模板
             * 二、 给出参数!
             */
            /*
             * 1. 给出一个sql语句前半部
             */
            StringBuilder sql = new StringBuilder("select * from t_customer where 1=1");
            /*
             * 2. 判断条件,完成向sql中追加where子句
             */
            /*
             * 3. 创建一个ArrayList,用来装载参数值
             */
            List<Object> params = new ArrayList<Object>();
            String cname = criteria.getCname();
            if(cname != null && !cname.trim().isEmpty()) {
                sql.append(" and cname like ?");
                params.add("%" + cname + "%");
            }
            
            String gender = criteria.getGender();
            if(gender != null && !gender.trim().isEmpty()) {
                sql.append(" and gender=?");
                params.add(gender);
            }
            
            String cellphone = criteria.getCellphone();
            if(cellphone != null && !cellphone.trim().isEmpty()) {
                sql.append(" and cellphone like ?");
                params.add("%" + cellphone + "%");
            }
            
            String email = criteria.getEmail();
            if(email != null && !email.trim().isEmpty()) {
                sql.append(" and email like ?");
                params.add("%" + email + "%");
            }
            
            /*
             * 三、执行query
             */
            return qr.query(sql.toString(), 
                    new BeanListHandler<Customer>(Customer.class), 
                    params.toArray());
        } catch(SQLException e) {
            throw new RuntimeException(e);
        }
    }