MVC Case - fuzzy queries

Fuzzy query:

- querying the passed name, password attribute column

- need to define a getForListWithCriteriaCustomer (CriteriaCustomer cc) in CustomerDAO interface.

Wherein CriteriaCustomer for packaging query: name, address, phone.

Because the query many times and domain class is not the same, so to make a separate class

- fight SQL:

  • SQL:"SELECT ID,NAME,PASSWORD FROM CUSTOMER WHERE"+"NAME LIKE ? AND PASSWORD LIKE ? ";
  • In order to correctly fill placeholders, rewritten getter CriteriaCustomer of: 

- Modify the Servlet : parameter acquisition request; CriteriaCustomer encapsulate the request parameter object, calls getForListWithCriteriaCustomer (CriteriaCustomer cc) Method

 

step:

1. First, the method to add CustomerDAO interface.

public List<Customer> getListWithCirteriaCustomer(CriteriaCustomer cc);

  

2. Create a CriteriaCustomer class to encapsulate the member variables

package com.mvcapp.entity;

public class CriteriaCustomer {
    private String name;
    private String password;

    public CriteriaCustomer(String name, String password) {
        this.name = name;
        this.password = password;
    }

    public CriteriaCustomer() {
    }

    public String getName() {
        if (name == null){
            name="%%";
        }else{
            name="%"+ name +"%";
        }
        return name;
    }

    @Override
    public String toString() {
        return "CriteriaCustomer{" +
                "name='" + name + '\'' +
                ", password='" + password + '\'' +
                '}';
    }


    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        if (password == null){
            password="%%";
        }else{
            password="%"+ password +"%";
        }
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

  

3. The implementation of the method implemented in the class CustomerDAOimpl

 public List<Customer> getListWithCirteriaCustomer(CriteriaCustomer cc) {
        String sql = "SELECT ID,NAME,PASSWORD FROM CUSTOMER WHERE" + " NAME LIKE ? AND PASSWORD LIKE ?";
        return getForList(sql,cc.getName(),cc.getPassword());//调用DAO层中方法
    }

  

4. A test may be implemented on this method

 @Test
    public void testGetListWithCriteriaCustomer(){
        CriteriaCustomer criteriaCustomer = new CriteriaCustomer("Y",null);
        List<Customer> customers = customerDAO.getListWithCirteriaCustomer(criteriaCustomer);
        System.out.println(customers);
    }

  

5. Modify Servlet, modified query method

 private void query(HttpServletRequest req,HttpServletResponse resp){
        String name = req.getParameter("name");
        String password = req.getParameter("password");
        CriteriaCustomer criteriaCustomer = new CriteriaCustomer(name,password);

        List<Customer> list =  customerDAO.getListWithCirteriaCustomer(criteriaCustomer);
        req.setAttribute("list",list);
        try {
            req.getRequestDispatcher("/query.jsp").forward(req,resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

  

effect:

Guess you like

Origin www.cnblogs.com/yangHS/p/11137658.html