客户列表与筛选客户实现

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

----------------------------------------------------------

Hibernate-08客户列表与筛选客户实现

Hibernate-08客户列表与筛选客户实现

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-09-09
  • 2021-08-15
  • 2022-12-23
  • 2021-06-24
  • 2021-05-19
猜你喜欢
  • 2021-08-08
  • 2021-08-24
  • 2022-01-29
  • 2021-04-27
  • 2021-10-31
相关资源
相似解决方案