search
HomeJavajavaTutorialSSH project customer list and BaseDao encapsulation example

一、客户列表

  1.分析

  2.书写步骤

  (1)封装PageBean

public class PageBean {//当前页数private Integer currentPage;//总记录数private Integer totalCount;//每页显示条数private Integer pageSize;//总页数private Integer totalPage;//分页列表数据private List    list;public PageBean(Integer currentPage, Integer totalCount, Integer pageSize) {this.totalCount = totalCount;        this.pageSize =  pageSize;        this.currentPage = currentPage;        if(this.currentPage == null){//如页面没有指定显示那一页.显示第一页.this.currentPage = 1;
        }        if(this.pageSize == null){//如果每页显示条数没有指定,默认每页显示3条this.pageSize = 3;
        }        //计算总页数this.totalPage = (this.totalCount+this.pageSize-1)/this.pageSize;        //判断当前页数是否超出范围//不能小于1if(this.currentPage  this.totalPage){this.currentPage = this.totalPage;
        }
        
    }//计算起始索引public int getStart(){return (this.currentPage-1)*this.pageSize;
    }    public Integer getCurrentPage() {return currentPage;
    }public void setCurrentPage(Integer currentPage) {this.currentPage = currentPage;
    }public Integer getTotalCount() {return totalCount;
    }public void setTotalCount(Integer totalCount) {this.totalCount = totalCount;
    }public Integer getPageSize() {return pageSize;
    }public void setPageSize(Integer pageSize) {this.pageSize = pageSize;
    }public Integer getTotalPage() {return totalPage;
    }public void setTotalPage(Integer totalPage) {this.totalPage = totalPage;
    }public List getList() {return list;
    }public void setList(List list) {this.list = list;
    }

}

  (2)书写Action

public class CustomerAction extends ActionSupport implements ModelDriven<customer> {private Customer customer = new Customer();    private CustomerService cs;private Integer currentPage;private Integer pageSize;public String list() throws Exception {//封装离线查询对象DetachedCriteria dc = DetachedCriteria.forClass(Customer.class);//判断并封装参数if(StringUtils.isNotBlank(customer.getCust_name())){
            dc.add(Restrictions.like("cust_name", "%"+customer.getCust_name()+"%"));
        }        //1 调用Service查询分页数据(PageBean)PageBean pb = cs.getPageBean(dc,currentPage,pageSize);//2 将PageBean放入request域,转发到列表页面显示ActionContext.getContext().put("pageBean", pb);return "list";
    }

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

}</customer>

  (3)书写Service

public class CustomerServiceImpl implements CustomerService {private CustomerDao cd;
    @Overridepublic PageBean getPageBean(DetachedCriteria dc, Integer currentPage, Integer pageSize) {//1 调用Dao查询总记录数Integer totalCount = cd.getTotalCount(dc);//2 创建PageBean对象PageBean pb = new PageBean(currentPage, totalCount, pageSize);//3 调用Dao查询分页列表数据        
        List<customer> list = cd.getPageList(dc,pb.getStart(),pb.getPageSize());//4 列表数据放入pageBean中.并返回        pb.setList(list);return pb;
    }public void setCd(CustomerDao cd) {this.cd = cd;
    }

}</customer>

  (4)书写Dao

public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {public Integer getTotalCount(DetachedCriteria dc) {//设置查询的聚合函数,总记录数        dc.setProjection(Projections.rowCount());
        
        List<long> list = (List<long>) getHibernateTemplate().findByCriteria(dc);        //清空之前设置的聚合函数dc.setProjection(null);        if(list!=null && list.size()>0){
            Long count = list.get(0);return count.intValue();
        }else{return null;
        }
    }public List<customer> getPageList(DetachedCriteria dc, int start, Integer pageSize) {        return (List<customer>) getHibernateTemplate().findByCriteria(dc, start, pageSize);

    }

}</customer></customer></long></long>

  (5)完成struts以及spring的配置

   strus.xml添加代码:

    <action> <result>/jsp/customer/list.jsp</result></action>

   applicationContext.xml添加代码:

    <bean><property></property></bean><bean><property></property></bean><bean><!-- 注入sessionFactory --><property></property></bean>

  (6)书写前台list.jsp页面

   主要通过表单提交隐藏域的数据、jq和ognl表达式来实现。

nbsp;html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><title>客户列表</title> <meta><link><link><script></script><script>function changePage(pageNum){
            //1 将页码的值放入对应表单隐藏域中
                $("#currentPageInput").val(pageNum);
            //2 提交表单
                $("#pageForm").submit();
    };
    
    function changePageSize(pageSize){
            //1 将页码的值放入对应表单隐藏域中
            $("#pageSizeInput").val(pageSize);
        //2 提交表单
            $("#pageForm").submit();
    };
</script><meta>
SSH project customer list and BaseDao encapsulation example SSH project customer list and BaseDao encapsulation example
SSH project customer list and BaseDao encapsulation example
当前位置:客户管理 > 客户列表
" />" />
客户名称:
客户名称 客户级别 客户来源 联系人 电话 手机 操作
修改  删除
共[ ]条记录,[]页                                                 ,每页显示  条                                                 [)" >前一页][)"  >后一页]                                                  到"  />                                                 页                                                
SSH project customer list and BaseDao encapsulation example
SSH project customer list and BaseDao encapsulation example SSH project customer list and BaseDao encapsulation example

二、BaseDao封装

  1.抽取BaseDao

  2.BaseDao设计思路

  3.BaseDao接口书写

public interface BaseDao<t> {//增void save(T t);//删void delete(T t);//删void delete(Serializable id);//改void update(T t);//查 根据id查询    T    getById(Serializable id);//查 符合条件的总记录数    Integer    getTotalCount(DetachedCriteria dc);//查 查询分页列表数据List<t> getPageList(DetachedCriteria dc,Integer start,Integer pageSize);
    
}</t></t>

  4.BaseDao的实现类

public class BaseDaoImpl<t> extends HibernateDaoSupport implements BaseDao<t> {private Class clazz;//用于接收运行期泛型类型
    public BaseDaoImpl() {//获得当前类型的带有泛型类型的父类ParameterizedType ptClass = (ParameterizedType) this.getClass().getGenericSuperclass();//获得运行期的泛型类型clazz = (Class) ptClass.getActualTypeArguments()[0];
    }

    @Overridepublic void save(T t) {
        getHibernateTemplate().save(t);
    }

    @Overridepublic void delete(T t) {
        
        getHibernateTemplate().delete(t);
        
    }

    @Overridepublic void delete(Serializable id) {
        T t = this.getById(id);//先取,再删        getHibernateTemplate().delete(t);
    }

    @Overridepublic void update(T t) {
        getHibernateTemplate().update(t);
    }

    @Overridepublic T getById(Serializable id) {        
        
        return (T) getHibernateTemplate().get(clazz, id);
    }

    @Overridepublic Integer getTotalCount(DetachedCriteria dc) {//设置查询的聚合函数,总记录数        dc.setProjection(Projections.rowCount());
        
        List<long> list = (List<long>) getHibernateTemplate().findByCriteria(dc);        //清空之前设置的聚合函数dc.setProjection(null);        if(list!=null && list.size()>0){
            Long count = list.get(0);return count.intValue();
        }else{return null;
        }
        
    }

    @Overridepublic List<t> getPageList(DetachedCriteria dc, Integer start, Integer pageSize) {
        
        List<t> list = (List<t>) getHibernateTemplate().findByCriteria(dc, start, pageSize);        return list;
    }
}</t></t></t></long></long></t></t>

  5.业务Dao中的应用

public class CustomerDaoImpl extends BaseDaoImpl<customer> implements CustomerDao {
    
}</customer>

The above is the detailed content of SSH project customer list and BaseDao encapsulation example. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use