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

客户列表与筛选客户实现

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(添加注明来意)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值