将学习笔记5的测试类放到实际应用中进行实现
要用到dao,serivice,servlet类进行分级编写:
编写的顺序是dao-service-servlet
首先是dao的编写:
CstCustomer接口类(code)
public interface CstCustomerDao {
//插入客户信息
public void insert(CstCustomer customer);
//查询记录总数
/**
*
* @param detachedCriteria 存储了查询条件,可以在任何地方拼接detachedCriteria对象
* @return
*/
public Long findCustomerCount(DetachedCriteria detachedCriteria);
//分页查询数据列表
/**
*
* @param detachedCriteria 存储了查询条件
* @param firstResult 起始记录下标,从0开始
* @param maxResults 每页显示记录数
* @return
*/
public List<CstCustomer> findCustomerList(DetachedCriteria detachedCriteria,int firstResult,int maxResults);
}
注意findCustomerList方法里的参数是(DetachedCriteria detachedCriteria,int firstResult,int maxResults)
CstCustomerImpl类(code)
对接口的方法进行重写
public class CstCustomerDaoImpl implements CstCustomerDao{
public void insert(CstCustomer customer) {
//创建session
Session session = HibernateUtil.openSession();
//开启事务
Transaction transaction = session.beginTransaction();
try {
session.save(customer);
//提交事务
transaction.commit();
} catch (Exception e) {
e.printStackTrace();
transaction.rollback();
} finally {
session.close();
}
}
@Override
public Long findCustomerCount(DetachedCriteria detachedCriteria) {
//转成可执行Criteria
Session session = HibernateUtil.openSession();
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
//设置rowCount的投影列
criteria.setProjection(Projections.rowCount());
Long total = (Long)criteria.uniqueResult();
return total;
}
@Override
public List<CstCustomer> findCustomerList(DetachedCriteria detachedCriteria, int firstResult, int maxResults) {
//转成可执行Criteria
Session session = HibernateUtil.openSession();
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
//设置分页参数
criteria.setFirstResult(firstResult);//起始记录下表
criteria.setMaxResults(maxResults);//每页显示个数
return criteria.list();
}
}
然后是service的编写:
CustomerService类(code)
public interface CustomerService {
//新增客户
public void insertCustomer(CstCustomer cstCustomer);
//查询记录总数
public Long findCustomerCount(CstCustomer cstCustomer);
//查询记录列表
public List<CstCustomer> findCustomerList(CstCustomer cstCustomer,int firstResult,int maxResults);
}
注意到findCustomerList方法里的参数是(CstCustomer cstCustomer,int firstResult,int maxResults)
【因为需要在service根据cstCustomer中数据进行动态拼装查询条件】
CustomerServiceImpl类(code)
对接口的方法进行重写
public class CustomerServiceImpl implements CustomerService{
@Override
public void insertCustomer(CstCustomer cstCustomer) {
//调用dao插入客户信息
CstCustomerDao CstCustomerDao = new CstCustomerDaoImpl();
CstCustomerDao.insert(cstCustomer);
}
@Override
public Long findCustomerCount(CstCustomer cstCustomer) {
//创建DetachedCriteria
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(CstCustomer.class);
//根据cstCustomer中数据动态拼装查询条件
if(cstCustomer!=null) {
//拼接客户名称查询条件,使用like
if(cstCustomer.getCustName()!=null && !cstCustomer.getCustName().equals("")) {
detachedCriteria.add(Restrictions.like("custName", "%"+cstCustomer.getCustName()+"%"));
}
//拼接联系人查询条件
if(cstCustomer.getCustLinkman()!=null && !cstCustomer.getCustLinkman().equals("")) {
detachedCriteria.add(Restrictions.eq("custLinkman", cstCustomer.getCustLinkman()));
}
}
CstCustomerDao CstCustomerDao = new CstCustomerDaoImpl();
return CstCustomerDao.findCustomerCount(detachedCriteria);
}
@Override
public List<CstCustomer> findCustomerList(CstCustomer cstCustomer, int firstResult, int maxResults) {
//创建DetachedCriteria
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(CstCustomer.class);
if(cstCustomer!=null) {
//拼接客户名称查询条件,使用like
if(cstCustomer.getCustName()!=null && !cstCustomer.getCustName().equals("")) {
detachedCriteria.add(Restrictions.like("custName", "%"+cstCustomer.getCustName()+"%"));
}
//拼接联系人查询条件,这里使用eq
if(cstCustomer.getCustLinkman()!=null && !cstCustomer.getCustLinkman().equals("")) {
detachedCriteria.add(Restrictions.eq("custLinkman", cstCustomer.getCustLinkman()));
}
}
CstCustomerDao CstCustomerDao = new CstCustomerDaoImpl();
return CstCustomerDao.findCustomerList(detachedCriteria, firstResult, maxResults);
}
}
servlet类(code)
public class CustomerServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取请求的方法名
String method = req.getParameter("method");
if(method == null || method.equals("") || method.equals("add")){
//转发到添加客户页面
req.getRequestDispatcher("/jsp/customer/add.jsp").forward(req, resp);
}else if(method.equals("addsubmit")){
this.addsubmit(req, resp);
}else if(method.equals("list")){
this.list(req, resp);
}
}
//查询客户提交
private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
CustomerService customerService = new CustomerServiceImpl();
//----------查询条件--------------
CstCustomer query_cstCustomer = new CstCustomer();
//客户名称
String custName = req.getParameter("custName");
//客户联系人
String custLinkman = req.getParameter("custLinkman");
query_cstCustomer.setCustName(custName);
query_cstCustomer.setCustLinkman(custLinkman);
//查询记录总数
long total = customerService.findCustomerCount(query_cstCustomer);
//-----------分页参数-------------
//每页显示个数
String pageSizeString = req.getParameter("pageSize");
int pageSize = Integer.parseInt(pageSizeString == null?"15":pageSizeString);
//计算总页数
Double num = Math.ceil(total*1.0/pageSize);
int totalPage = num.intValue();
//当前页码
String pageString = req.getParameter("page");
int page = Integer.parseInt(pageString == null||pageString.equals("")?"1":pageString);
if(page<=0){
page = 1;
}
if(page>totalPage){
page = totalPage;
}
//根据分页参数计算出起始记录下标
int firstResult = pageSize * (page - 1);
List<CstCustomer> list = customerService.findCustomerList(query_cstCustomer, firstResult, pageSize);
//当前页码
req.setAttribute("page", page);
//总页数
req.setAttribute("totalPage", totalPage);
//每页显示个数
req.setAttribute("pageSize", pageSize);
//总数
req.setAttribute("total", total);
//列表
req.setAttribute("list", list);
//成功
req.getRequestDispatcher("/jsp/customer/list.jsp").forward(req, resp);
}
//添加客户提交
private void addsubmit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
//客户信息
String custName = req.getParameter("custName");//客户名称
String custLevel = req.getParameter("custLevel");//客户级别
String custSource = req.getParameter("custSource");//信息来源
String custLinkman = req.getParameter("custLinkman");//联系人
String custPhone = req.getParameter("custPhone");//固定电话
String custMobile = req.getParameter("custMobile");//移动电话
//客户详细信息
String custAddress = req.getParameter("custAddress");//联系地址
String custZip = req.getParameter("custZip");//邮政编码
CustomerService customerService = new CustomerServiceImpl();
//客户信息
CstCustomer cstCustomer = new CstCustomer();
cstCustomer.setCustName(custName);
cstCustomer.setCustPhone(custPhone);
cstCustomer.setCustLinkman(custLinkman);
cstCustomer.setCustMobile(custMobile);
cstCustomer.setCustLevel(custLevel);
//调用新的 service接口
try {
customerService.insertCustomer(cstCustomer);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
//失败
req.getRequestDispatcher("/jsp/error.jsp").forward(req, resp);
return ;
}
//成功
req.getRequestDispatcher("/jsp/success.jsp").forward(req, resp);
}
}