03总代码

Customer

package cn.itcast.domain;

public class Customer {
    
    /*
     * CREATE TABLE `cst_customer` (
      `cust_id` BIGINT(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)',
      `cust_name` VARCHAR(32) NOT NULL COMMENT '客户名称(公司名称)',
      `cust_source` VARCHAR(32) DEFAULT NULL COMMENT '客户信息来源',
      `cust_industry` VARCHAR(32) DEFAULT NULL COMMENT '客户所属行业',
      `cust_level` VARCHAR(32) DEFAULT NULL COMMENT '客户级别',
      `cust_linkman` VARCHAR(64) DEFAULT NULL COMMENT '联系人',
      `cust_phone` VARCHAR(64) DEFAULT NULL COMMENT '固定电话',
      `cust_mobile` VARCHAR(16) DEFAULT NULL COMMENT '移动电话',
      PRIMARY KEY (`cust_id`)
    ) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
     */
    private Long cust_id;
    
    private String cust_name;
    private String cust_source;
    private String cust_industry;
    private String cust_level;
    private String cust_linkman;
    private String cust_phone;
    private String cust_mobile;
    public Long getCust_id() {
        return cust_id;
    }
    public void setCust_id(Long cust_id) {
        this.cust_id = cust_id;
    }
    public String getCust_name() {
        return cust_name;
    }
    public void setCust_name(String cust_name) {
        this.cust_name = cust_name;
    }
    public String getCust_source() {
        return cust_source;
    }
    public void setCust_source(String cust_source) {
        this.cust_source = cust_source;
    }
    public String getCust_industry() {
        return cust_industry;
    }
    public void setCust_industry(String cust_industry) {
        this.cust_industry = cust_industry;
    }
    public String getCust_level() {
        return cust_level;
    }
    public void setCust_level(String cust_level) {
        this.cust_level = cust_level;
    }
    public String getCust_linkman() {
        return cust_linkman;
    }
    public void setCust_linkman(String cust_linkman) {
        this.cust_linkman = cust_linkman;
    }
    public String getCust_phone() {
        return cust_phone;
    }
    public void setCust_phone(String cust_phone) {
        this.cust_phone = cust_phone;
    }
    public String getCust_mobile() {
        return cust_mobile;
    }
    public void setCust_mobile(String cust_mobile) {
        this.cust_mobile = cust_mobile;
    }
    @Override
    public String toString() {
        return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + "]";
    }
    
    
    

}

CustomerAction

package cn.itcast.web.action;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.domain.Customer;
import cn.itcast.service.CustomerService;
import cn.itcast.utils.PageBean;

public class CustomerAction extends ActionSupport implements ModelDriven {
    private Customer customer = new Customer();
    
    private CustomerService cs ;
    
    //利用属性驱动来封装Customer对象没有的属性
    //这样传递Action对象的时候
    private Integer currentPage;
    private Integer  pageSize;

    
    public String list()
    {
        
        //1.封装离线查询对象
        DetachedCriteria dc = DetachedCriteria.forClass(Customer.class);
            //判断并封装参数
        if(StringUtils.isNotBlank(customer.getCust_name()))
        {
            dc.add(Restrictions.like("cust_name", "%"+customer.getCust_name()+"%"));
        }
        //2.调用Service查询分页数据
        PageBean bean = cs.getPageBean(dc, currentPage, pageSize);
        //3.将PageBean放入request域,转发到列表页面显示
                ActionContext.getContext().put("pageBean", bean);
                return "list";
    }
    @Override
    public Customer getModel() {
        return customer;
    }

    public void setCs(CustomerService cs)
    {
        this.cs = cs;
    }

    

    public Integer getCurrentPage()
    {
        return currentPage;
    }


    public void setCurrentPage(Integer currentPage)
    {
        this.currentPage = currentPage;
    }


    public Integer getPageSize()
    {
        return pageSize;
    }


    public void setPageSize(Integer pageSize)
    {
        this.pageSize = pageSize;
    }


    
    
}

applicationContext.xml



    
    
    
    
    
        
        
        
        
    
    
    
    
        
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
        
        
        
        
            
                
            
                org.hibernate.dialect.MySQLDialect
                
                
                true
                true
                update
            
        
        
        
    
    
    
    
    
        
    
    
    
        
    
    
    
    
    
    
        
    
    
    
        
    
    
    
    
    
    
        
        
    
    
    
        
        
    


struts.xml



    
    
    
    
    
        
        
        
        
    
    
    
    
        
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
        
        
        
        
            
                
            
                org.hibernate.dialect.MySQLDialect
                
                
                true
                true
                update
            
        
        
        
    
    
    
    
    
        
    
    
    
        
    
    
    
    
    
    
        
    
    
    
        
    
    
    
    
    
    
        
        
    
    
    
        
        
    


CustomerService

package cn.itcast.service;

import org.hibernate.criterion.DetachedCriteria;

import cn.itcast.utils.PageBean;

public interface CustomerService
{
    //分页业务方法
    PageBean getPageBean(DetachedCriteria dc,Integer currentPage,Integer pageSize);
}

CustomerServiceImpl

package cn.itcast.service.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;

import cn.itcast.dao.CustomerDao;
import cn.itcast.domain.Customer;
import cn.itcast.service.CustomerService;
import cn.itcast.utils.PageBean;

public class CustomerServiceImpl implements CustomerService 
{
    private CustomerDao cd;

    @Override
    public PageBean getPageBean(DetachedCriteria dc, Integer currentPage, Integer pageSize)
    {
        //1.调用Dao查询总记录数
        Integer totalCount=cd.getTotalCount(dc);
        //2.创建PageBean对象
        PageBean bean = new PageBean(currentPage, pageSize, totalCount);
        //3.调用Dao查询分页列表数据
        List list=cd.getPageList(dc,bean.getStart(),bean.getPageSize());
        //4.列表数据放入到pageBean中,并返回
        bean.setList(list);
        return bean;
    }

    public void setCd(CustomerDao cd)
    {
        this.cd = cd;
    }

}

CustomerDao

package cn.itcast.dao;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;

import cn.itcast.domain.Customer;

public interface CustomerDao
{


    Integer getTotalCount(DetachedCriteria dc);

    List getPageList(DetachedCriteria cd, int start, Integer pageSize);




}

CustomerDaoImpl

package cn.itcast.dao.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

import cn.itcast.dao.CustomerDao;
import cn.itcast.domain.Customer;

public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao
{

    @Override
    public Integer getTotalCount(DetachedCriteria dc)
    {
        //查询总记录数
        //利用HibernateTemplate来做
        //设置查询的聚合函数,总记录数
        dc.setProjection(Projections.rowCount());
        
        List list = (List) getHibernateTemplate().findByCriteria(dc);
        
        
        //清空之前的之前使用过的(dc)离线对象。
        dc.setProjection(null);
        if(list!=null && list.size() >0)
        {
            Long count = list.get(0);
            return count.intValue(); 
        }
        else
        {
            return null;
        }
            
        
    }

    

    @Override
    public List getPageList(DetachedCriteria cd, int start, Integer pageSize)
    {
        List list=(List) getHibernateTemplate().findByCriteria( cd, start, pageSize);
        return list;
    }

}

list.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib  prefix="s" uri="/struts-tags" %>



客户列表 










    
        
        
当前位置:客户管理 > 客户列表
" /> " />
客户名称:
客户名称 客户级别 客户来源 联系人 电话 手机 操作
修改    删除
共[ ]条记录,[]页 ,每页显示 <%-- changePageSize($('#pageSizeSelect option').filter(':selected').val()) --%> 条 [前一页] [后一页] 到 " /> 页

你可能感兴趣的:(03总代码)