客户列表与筛选客户实现
Servlet
package com.feizhu.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.feizhu.domain.Customer;
import com.feizhu.service.SaveCustomerService;
public class FindAllCustomerServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* 查询所有用户
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 设置编码
request.setCharacterEncoding("utf-8");
// 接受筛选关键字
String custName=request.getParameter("custName");
System.out.println(custName);
// 调用service层 方法
List<Customer> list = new SaveCustomerService().findAll(custName);
// 将查到的数据放入request域中
request.setAttribute("list", list);
// 转发
request.getRequestDispatcher("/jsp/customer/list.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
------------------------------------------------------------------------------------------------------------
service
/**
* 查询所有用户
* @return
*/
public List<Customer> findAll() {
//调用Dao层 对应方法
List<Customer> list=new SaveCustomerDao().findAll();
return list;
}
/**
* 筛选
* @return
*/
public List<Customer> findAll(String custName) {
//调用Dao层 对应方法
List<Customer> list=new SaveCustomerDao().findAll(custName);
return list;
}
----------------------------------------------------------------------------------------------------
Dao
/**
* 查询所有用户
*
* @return
*/
public List<Customer> findAll() {
// QBC查询
Session session = HibernateUtil.openSession();
Transaction tr = session.beginTransaction();
// 查询
Criteria criteria = session.createCriteria(Customer.class);
List<Customer> list = criteria.list();
tr.commit();
session.close();
return list;
}
/**
* 筛选
*
* @return
*/
public List<Customer> findAll(String custName) {
// QBC查询
Session session = HibernateUtil.openSession();
Transaction tr = session.beginTransaction();
// 查询
Criteria criteria = session.createCriteria(Customer.class);
if(custName!=null &&!custName.trim().isEmpty()) {
//添加查询条件
criteria.add(Restrictions.like("cust_name", "%"+custName+"%"));
}
List<Customer> list = criteria.list();
tr.commit();
session.close();
return list;
}
----------------------------------------------------------
备注:由于个人原因,本博客暂停更新。如有问题可联系本人,本人提供技术指导、学习方向、学习路线。本人微信wlp1156107728(添加注明来意) QQ1156107728(添加注明来意)