JavaWeb_MVC案例之(模糊)查询
MVC案例之(模糊)查询
-1. 查询操作:
1)接上次笔记中CustomerServlet中query方法具体代码:
/**
* @Title: query
* @Description: TODO
* @param request
* @param response
* @return void
* @throws IOException
* @throws ServletException
*/
private void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.调用CustomerDAO的getAll()得到Customer的集合。
List<Customer> customers = customerDAO.getAll();
//2.把Customer的集合放入request中。
request.setAttribute("customer", customers);
//3.转发页面到index.jsp(不能使用重定向)。
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
2)index.jsp:
<%@page import="com.xs.mvc_crud.domain.Customer"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="query.do">
<table border="1" cellpadding="10" cellspacing="0">
<tr>
<td>Name:</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td>Address:</td>
<td><input type="text" name="address" /></td>
</tr>
<tr>
<td>Phone:</td>
<td><input type="text" name="phone" /></td>
</tr>
<tr>
<td><input type="submit" value="Query" /></td>
<td><a href="">Add New Customer</a></td>
</tr>
</table>
</form>
<br>
<br>
<hr>
<br>
<br>
<%
List<Customer> customers = (List<Customer>)request.getAttribute("customer");
if(customers != null && customers.size() > 0) {
%>
<table border="1" cellpadding="10" cellspacing="0">
<tr>
<th>Id</th>
<th>Name</th>
<th>Address</th>
<th>Phone</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<%
for(Customer customer: customers) {
%>
<tr>
<td><%= customer.getId() %></td>
<td><%= customer.getName() %></td>
<td><%= customer.getAddress() %></td>
<td><%= customer.getPhone() %></td>
<td><a href="">Edit</a></td>
<td><a href="">Delete</a></td>
</tr>
<%
}
%>
</table>
<%
}
%>
</body>
</html>
- 2.模糊查询
1)根据传入的 name,address,phone 进行模糊查询。
2)例子:name:a、address:b、phone:3, 则 SQL 语句的样子为:
SELECT id, name, address, phone FROM customers WHERE name LIKE ‘%a%’ AND address LIKE ‘%b%’ AND phone LIKE ‘%3%’
.
3)需要在 CustomerDAO 接口中定义一个 getForListWithCriteriaCustomer(CriteriaCustomer cc)。 其中 CriteriaCustomer 用于封装查询条件:name,address,phone。因为查询条件很多时候和 domain 类并不相同,所以要做成一个单独的类。
CriteriaCustomer 类:
/**
* All rights Reserved,Designed By XS
* @Title: CriteriaCustomer.java
* @Package com.xs.mvc_crud.dao
* @Description: TODO
* @author: XS
* @date: 2019年3月9日 下午4:29:45
* @version V1.0
*/
package com.xs.mvc_crud.dao;
/**
* @ClassName: CriteriaCustomer
* @Description: TODO
* @author: XS
* @date: 2019年3月9日 下午4:29:45
* @version V1.0
*/
public class CriteriaCustomer {
private String name;
private String address;
private String phone;
/**
* @Title: CriteriaCustomer
* @Description: TODO
* @param name
* @param address
* @param phone
*/
public CriteriaCustomer(String name, String address, String phone) {
super();
this.name = name;
this.address = address;
this.phone = phone;
}
/**
* @return the name
*/
public String getName() {
if (name == null)
name = "%%";
else
name = "%" + name + "%";
return name;
}
/**
* @param name: the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the address
*/
public String getAddress() {
if (address == null)
address = "%%";
else
address = "%" + address + "%";
return address;
}
/**
* @param address: the address to set
*/
public void setAddress(String address) {
this.address = address;
}
/**
* @return the phone
*/
public String getPhone() {
if (phone == null)
phone = "%%";
else
phone = "%" + phone + "%";
return phone;
}
/**
* @param phone: the phone to set
*/
public void setPhone(String phone) {
this.phone = phone;
}
}
4)拼 SQL:
```oracle
SELECT id, name, address, phone FROM customers WHERE name LIKE ? AND address LIKE ? AND phone LIKE ?
为了正确的填充占位符时,重写了 CriteriaCustomer 的 getter,具体的重写方式见CriteriaCustomer 实体类。
5)修改 Servlet:获取请求参数;把请求参数封装为
CriteriaCustomer 对象,再调用 getForListWithCriteriaCustomer(CriteriaCustomer cc) 方法。
CustomerDAO 类中加入:
/**
* @Title: getForListWithCriteriaCustomer
* @Description: 封装了查询条件,返回满足查询条件的List。
* @param cc
* @return
* @return List<Customer>
*/
public List<Customer> getForListWithCriteriaCustomer(CriteriaCustomer cc);
CustomerDAOJdbcImpl 中具体实现 getForListWithCriteriaCustomer() 方法:
/**
* <p>Title: getForListWithCriteriaCustomer</p>
* <p>Description: </p>
* @see com.xs.mvc_crud.dao.CustomerDAO#getForListWithCriteriaCustomer(com.xs.mvc_crud.dao.CriteriaCustomer)
* @param cc
* @return
*/
@Override
public List<Customer> getForListWithCriteriaCustomer(CriteriaCustomer cc){
String sql = "SELECT id, name, address, phone FROM customers "
+ "WHERE name LIKE ? AND address LIKE ? AND phone LIKE ?";
return getForList(sql, cc.getName(), cc.getAddress(), cc.getPhone());
}
修改CustomerServlet中的query方法:
/**
* @Title: query
* @Description: TODO
* @param request
* @param response
* @return void
* @throws IOException
* @throws ServletException
*/
private void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
String address = request.getParameter("address");
String phone = request.getParameter("phone");
CriteriaCustomer cc = new CriteriaCustomer(name, address, phone);
//1.调用CustomerDAO的getForListWithCriteriaCustomer(cc)得到Customer的集合。
List<Customer> customers = customerDAO.getForListWithCriteriaCustomer(cc);
//2.把Customer的集合放入request中。
request.setAttribute("customer", customers);
//3.转发页面到index.jsp(不能使用重定向)。
request.getRequestDispatcher("/index.jsp").forward(request, response);
}