jsp 多條件組合查詢


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);
        }
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM