查询操作:
Servlet
//1. 调用 CustomerDAO 的 getAll() 得到 Customer 的集合
List customers = customerDAO.getAll();
//2. 把 Customer 的集合放入 request 中
request.setAttribute("customers", customers);
//3. 转发页面到 index.jsp(不能使用重定向)
request.getRequestDispatcher("/index.jsp").forward(request, response);
JSP
获取 request 中的 customers 属性
遍历显示
1.模糊查询
1).根据传入的 name, address, phone 进行模糊查询
例子: 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%’
需在CustomerDAO接口中定义一个 getForListWithCriteriaCustomer(CriteriaCustomer cc)
其中 CriteriaCustomer 用于封装查询条件:name, address, phone。
因为查询条件很多时候和 domain 类并不相同,所以要做成一个单独的类拼 SQL:
SQL: "SELECT id, name, address, phone FROM customers WHERE name LIKE ?
AND address LIKE ? ANDphone LIKE ?";
为了正确的填充占位符时,重写了 CriteriaCustomer 的 getter:
2).修改 Servlet:获取请求参数;把请求参数封装为CriteriaCustomer 对象,
再调用 getForListWithCriteriaCustomer(CriteriaCustomer cc) 方法
2.删除操作
1)超链接:delete.do?id=
Servlet 的 delete 方法:
获取 id
调用 DAO 执行删除
重定向到 query.do(若目标页面不需要读取当前请求的 request 属性,就可以使用重定向),
将显示删除后的 Customer 的 List
2).JSP 上的 jQuery 提示:确定要删除 xx 的信息吗?
CustomerDAO
packagecom.aff.mvcapp.dao;importjava.util.List;importcom.aff.mvcapp.domian.Customer;public interfaceCustomerDAO {public List getForListWithCriteriaCustomer(CriteriaCustomer c);public ListgetAll();public voidsave(Customer customer);publicCustomer get(Integer id);public voiddelete(Integer id);/*** 返回和 name 相等的记录数
*
*@paramname
*@return
*/
public longgetCountWithName(String name);public voidupdate(Customer customer);
}
CriteriaCustomer
packagecom.aff.mvcapp.dao;public classCriteriaCustomer {privateString name;privateString address;privateString phone;publicCriteriaCustomer() {super();
}publicCriteriaCustomer(String name, String address, String phone) {super();this.name =name;this.address =address;this.phone =phone;
}public String getName() {
if (name == null) {
name = "%%";
}else
name = "%"+name+"%";
return name;
}public voidsetName(String name) {this.name =name;
}public String getAddress() {
if (address == null) {
address = "%%";
}else
address = "%"+address+"%";
return address;
}public voidsetAddress(String address) {this.address =address;
}public String getPhone() {
if (phone == null) {
phone = "%%";
}else
phone = "%"+phone+"%";
return phone;
}public voidsetPhone(String phone) {this.phone =phone;
}
@OverridepublicString toString() {return "CriteriaCustomer [name=" + name + ", address=" + address + ", phone=" + phone + "]";
}
}
CustomerDAOImpl
packagecom.aff.mvcapp.dao.impl;importjava.util.List;importcom.aff.mvcapp.dao.CriteriaCustomer;importcom.aff.mvcapp.dao.CustomerDAO;importcom.aff.mvcapp.dao.DAO;importcom.aff.mvcapp.domian.Customer;public class CustomerDAOImpl extends DAO implementsCustomerDAO {
@Overridepublic List getForListWithCriteriaCustomer(CriteriaCustomer c) {
String sql = "select id, name, address, phone from customers where name like ? and address like ? and phone like ?";
returngetForList(sql, c.getName(), c.getAddress(), c.getPhone());
}
@Overridepublic ListgetAll() {
String sql= "select id, name, address, phone from customers";returngetForList(sql);
}
@Overridepublic voidsave(Customer customer) {
String sql= "insert into customers(name,address,phone)values(?,?,?)";
update(sql, customer.getName(), customer.getAddress(), customer.getPhone());
}
@OverridepublicCustomer get(Integer id) {
String sql= "select id,name,address,phone from customers where id =?";returnget(sql, id);
}
@Overridepublic voiddelete(Integer id) {
String sql= "delete from customers where id = ?";
update(sql, id);
}
@Overridepublic longgetCountWithName(String name) {
String sql= "select count(id) from customers where name =?";returngetForValue(sql, name);
}
@Overridepublic voidupdate(Customer customer) {
String sql= "update customers set name = ?,address = ? ,phone = ? where id = ?";
update(sql, customer.getName(), customer.getAddress(), customer.getPhone(), customer.getId());
}
}
CustomerServlet
packagecom.aff.mvcapp.servlet;importjava.io.IOException;importjava.lang.reflect.Method;importjava.util.List;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importcom.aff.mvcapp.dao.CriteriaCustomer;importcom.aff.mvcapp.dao.CustomerDAO;importcom.aff.mvcapp.dao.impl.CustomerDAOImpl;importcom.aff.mvcapp.domian.Customer;
@WebServlet("/customerServlet")public class CustomerServlet extendsHttpServlet {private static final long serialVersionUID = 1L;private CustomerDAO customerDAO = newCustomerDAOImpl();protected voiddoGet(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {
doPost(request, response);
}protected voiddoPost(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {//1. 获取ServletPath: /edit.do 或 addCustomer.do
String servletPath =request.getServletPath();//2.去除 / 和 .do 得到类似于 edit 或 addCustomer 这样的字符串
String methodName = servletPath.substring(1);
methodName= methodName.substring(0, methodName.length() - 3);try{//3.利用反射获取 methodName 对应的方法
Method method = getClass().getDeclaredMethod(methodName, HttpServletRequest.class,
HttpServletResponse.class);//4.利用反射调用对应的方法
method.invoke(this, request, response);
}catch(Exception e) {//e.printStackTrace();//可以有一些响应
response.sendRedirect("error.jsp");
}
}private void edit(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {
System.out.println("edit");
}private void update(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {
System.out.println("update");
}private void query(HttpServletRequest request, HttpServletResponse response) throwsServletException, 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() 得到 Customer 的集合
List customers = customerDAO.getForListWithCriteriaCustomer(cc);// 2.把 Customer 的集合放入 request 中
request.setAttribute("customers", customers);// 3.转发页面到 index.jsp 中( 不能使用重定向)
request.getRequestDispatcher("/index.jsp").forward(request, response);
}private void delete(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {
String idstr= request.getParameter("id");
int id = 0;// try-catch的作用 , 防止恶意的输入, idStr 不能转为int类型,若出异常 id直接为0
try{
id=Integer.parseInt(idstr);
customerDAO.delete(id);
}catch(Exception e) {
}
response.sendRedirect("query.do");
}
}
index.jsp
Insert title here$(".delete").click(function() {//找到tr的第二个td也就是name,再获取它的value值
varcontent=$(this).parent().parent().find("td:eq(1)").text();varflag=confirm("确定要删除" +content+ "的信息吗")returnflag;
});
});
CustomerNam: | |
Address: | |
Phone: | |
Add New Customer |
customers=(List) request.getAttribute("customers");if(customers != null &&customers.size()> 0) {%>
IDCustomersNameAddressPhoneUPDATE\DELETE
for(Customer customer : customers) {%>
UPDATEhref="delete.do?id=" class="delete">DELETE
目录