搜索
首页Javajava教程SSH项目之客户列表和BaseDao封装实例

SSH项目之客户列表和BaseDao封装实例

Jul 23, 2017 am 10:24 AM
javaee列表客户

一、客户列表

  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 < 1){this.currentPage = 1;
        }//不能大于总页数if(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;
    }

}

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

}

  (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);

    }

}

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

   strus.xml添加代码:

    <action name="CustomerAction_*" class="customerAction" method="{1}" > <result name="list"  >/jsp/customer/list.jsp</result></action>

   applicationContext.xml添加代码:

    <bean name="customerAction" class="cn.xyp.web.action.CustomerAction" scope="prototype" ><property name="cs" ref="customerService" ></property></bean><bean name="customerService" class="cn.xyp.service.impl.CustomerServiceImpl" ><property name="cd" ref="customerDao" ></property></bean><bean name="customerDao" class="cn.xyp.dao.impl.CustomerDaoImpl" ><!-- 注入sessionFactory --><property name="sessionFactory" ref="sessionFactory" ></property></bean>

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

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

<%@ 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" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><TITLE>客户列表</TITLE> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><LINK href="${pageContext.request.contextPath }/css/Style.css?1.1.11" type=text/css rel=stylesheet><LINK href="${pageContext.request.contextPath }/css/Manage.css?1.1.11" type=text/cssrel=stylesheet><script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.4.4.min.js?1.1.11"></script><SCRIPT language=javascript>function changePage(pageNum){
            //1 将页码的值放入对应表单隐藏域中
                $("#currentPageInput").val(pageNum);
            //2 提交表单
                $("#pageForm").submit();
    };
    
    function changePageSize(pageSize){
            //1 将页码的值放入对应表单隐藏域中
            $("#pageSizeInput").val(pageSize);
        //2 提交表单
            $("#pageForm").submit();
    };
</SCRIPT><META content="MSHTML 6.00.2900.3492" name=GENERATOR></HEAD><body><TABLE cellSpacing=0 cellPadding=0 width="98%" border=0><Tbody><TR><TD width=15><IMG src="${pageContext.request.contextPath }/images/new_019.jpg"border=0></TD><TD width="100%" background="${pageContext.request.contextPath }/images/new_020.jpg"height=20></TD><TD width=15><IMG src="${pageContext.request.contextPath }/images/new_021.jpg"border=0></TD></TR></Tbody></TABLE><TABLE cellSpacing=0 cellPadding=0 width="98%" border=0><Tbody><TR><TD width=15 background=${pageContext.request.contextPath }/images/new_022.jpg><IMGsrc="${pageContext.request.contextPath }/images/new_022.jpg" border=0></TD><TD vAlign=top width="100%" bgColor=#ffffff><TABLE cellSpacing=0 cellPadding=5 width="100%" border=0><TR><TD class=manageHead>当前位置:客户管理 &gt; 客户列表</TD></TR><TR><TD height=2></TD></TR></TABLE><TABLE borderColor=#cccccc cellSpacing=0 cellPadding=0width="100%" align=center border=0><Tbody><TR><TD height=25><FORM id="pageForm" name="customerForm"action="${pageContext.request.contextPath }/CustomerAction_list"method=post><!-- 隐藏域.当前页码 --><input type="hidden" name="currentPage" id="currentPageInput" value="<s:property value="#pageBean.currentPage" />" /><!-- 隐藏域.每页显示条数 --><input type="hidden" name="pageSize" id="pageSizeInput"       value="<s:property value="#pageBean.pageSize" />" /><TABLE cellSpacing=0 cellPadding=2 border=0><Tbody><TR><TD>客户名称:</TD><TD><INPUT class=textbox id=sChannel2style="WIDTH: 80px" maxLength=50 name="cust_name" value="${param.cust_name}"></TD><TD><INPUT class=button id=sButton2 type=submitvalue=" 筛选 " name=sButton2></TD></TR></Tbody></TABLE></FORM></TD></TR><TR><TD><TABLE id=gridstyle="BORDER-TOP-WIDTH: 0px; FONT-WEIGHT: normal; BORDER-LEFT-WIDTH: 0px; BORDER-LEFT-COLOR: #cccccc; BORDER-BOTTOM-WIDTH: 0px; BORDER-BOTTOM-COLOR: #cccccc; WIDTH: 100%; BORDER-TOP-COLOR: #cccccc; FONT-STYLE: normal; BACKGROUND-COLOR: #cccccc; BORDER-RIGHT-WIDTH: 0px; TEXT-DECORATION: none; BORDER-RIGHT-COLOR: #cccccc"cellSpacing=1 cellPadding=2 rules=all border=0><Tbody><TRstyle="FONT-WEIGHT: bold; FONT-STYLE: normal; BACKGROUND-COLOR: #eeeeee; TEXT-DECORATION: none"><TD>客户名称</TD><TD>客户级别</TD><TD>客户来源</TD><TD>联系人</TD><TD>电话</TD><TD>手机</TD><TD>操作</TD></TR><s:iterator value="#pageBean.list" var="cust" ><TR         style="FONT-WEIGHT: normal; FONT-STYLE: normal; BACKGROUND-COLOR: white; TEXT-DECORATION: none"><TD><s:property value="#cust.cust_name" /></TD><TD><s:property value="#cust.cust_level" /></TD><TD><s:property value="#cust.cust_source" /></TD><TD><s:property value="#cust.cust_linkman" /></TD><TD><s:property value="#cust.cust_phone" /></TD><TD><s:property value="#cust.cust_mobile" /></TD><TD><a href="${pageContext.request.contextPath }/customerServlet?method=edit&custId=${customer.cust_id}">修改</a>  <a href="${pageContext.request.contextPath }/customerServlet?method=delete&custId=${customer.cust_id}">删除</a></TD></TR></s:iterator></Tbody></TABLE></TD></TR><TR><TD><SPAN id=pagelink><DIV
                                                style="LINE-HEIGHT: 20px; HEIGHT: 20px; TEXT-ALIGN: right">共[<B><s:property value="#pageBean.totalCount" /> </B>]条记录,[<B><s:property value="#pageBean.totalPage" /></B>]页
                                                ,每页显示 <%-- changePageSize($(&#39;#pageSizeSelect option&#39;).filter(&#39;:selected&#39;).val()) --%> <select name="pageSize" onchange="changePageSize($(&#39;#pageSizeSelect option:selected&#39;).val())" id="pageSizeSelect" ><option value="3" <s:property value="#pageBean.pageSize==3?&#39;selected&#39;:&#39;&#39;" /> >3</option><option value="5" <s:property value="#pageBean.pageSize==5?&#39;selected&#39;:&#39;&#39;" /> >5</option></select>条
                                                [<A href="javaScript:void(0)" onclick="changePage(<s:property value=&#39;#pageBean.currentPage-1&#39; />)" >前一页</A>]<B><s:property value="#pageBean.currentPage" /></B>[<A href="javaScript:void(0)" onclick="changePage(<s:property value=&#39;#pageBean.currentPage+1&#39; />)"  >后一页</A>] 
                                                到<input type="text" size="3" id="page" name="page" value="<s:property value="#pageBean.currentPage" />"  />
                                                页                                                <input type="button" value="Go" onclick="changePage($(&#39;#page&#39;).val())"/></DIV></SPAN></TD></TR></Tbody></TABLE></TD><TD width=15 background="${pageContext.request.contextPath }/images/new_023.jpg"><IMGsrc="${pageContext.request.contextPath }/images/new_023.jpg" border=0></TD></TR></Tbody></TABLE><TABLE cellSpacing=0 cellPadding=0 width="98%" border=0><Tbody><TR><TD width=15><IMG src="${pageContext.request.contextPath }/images/new_024.jpg"border=0></TD><TD align=middle width="100%"background="${pageContext.request.contextPath }/images/new_025.jpg" height=15></TD><TD width=15><IMG src="${pageContext.request.contextPath }/images/new_026.jpg"border=0></TD></TR></Tbody></TABLE></body></HTML>

二、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);
    
}

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

  5.业务Dao中的应用

public class CustomerDaoImpl extends BaseDaoImpl<Customer> implements CustomerDao {
    
}

以上是SSH项目之客户列表和BaseDao封装实例的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
JVM中的类加载程序子系统如何促进平台独立性?JVM中的类加载程序子系统如何促进平台独立性?Apr 23, 2025 am 12:14 AM

类加载器通过统一的类文件格式、动态加载、双亲委派模型和平台无关的字节码,确保Java程序在不同平台上的一致性和兼容性,实现平台独立性。

Java编译器会产生特定于平台的代码吗?解释。Java编译器会产生特定于平台的代码吗?解释。Apr 23, 2025 am 12:09 AM

Java编译器生成的代码是平台无关的,但最终执行的代码是平台特定的。1.Java源代码编译成平台无关的字节码。2.JVM将字节码转换为特定平台的机器码,确保跨平台运行但性能可能不同。

JVM如何处理不同操作系统的多线程?JVM如何处理不同操作系统的多线程?Apr 23, 2025 am 12:07 AM

多线程在现代编程中重要,因为它能提高程序的响应性和资源利用率,并处理复杂的并发任务。JVM通过线程映射、调度机制和同步锁机制,在不同操作系统上确保多线程的一致性和高效性。

在Java的背景下,'平台独立性”意味着什么?在Java的背景下,'平台独立性”意味着什么?Apr 23, 2025 am 12:05 AM

Java的平台独立性是指编写的代码可以在任何安装了JVM的平台上运行,无需修改。1)Java源代码编译成字节码,2)字节码由JVM解释执行,3)JVM提供内存管理和垃圾回收功能,确保程序在不同操作系统上运行。

Java应用程序仍然可以遇到平台特定的错误或问题吗?Java应用程序仍然可以遇到平台特定的错误或问题吗?Apr 23, 2025 am 12:03 AM

Javaapplicationscanindeedencounterplatform-specificissuesdespitetheJVM'sabstraction.Reasonsinclude:1)Nativecodeandlibraries,2)Operatingsystemdifferences,3)JVMimplementationvariations,and4)Hardwaredependencies.Tomitigatethese,developersshould:1)Conduc

云计算如何影响Java平台独立性的重要性?云计算如何影响Java平台独立性的重要性?Apr 22, 2025 pm 07:05 PM

云计算显着提升了Java的平台独立性。 1)Java代码编译为字节码,由JVM在不同操作系统上执行,确保跨平台运行。 2)使用Docker和Kubernetes部署Java应用,提高可移植性和可扩展性。

Java的平台独立性在广泛采用中扮演着什么角色?Java的平台独立性在广泛采用中扮演着什么角色?Apr 22, 2025 pm 06:53 PM

Java'splatformindependenceallowsdeveloperstowritecodeonceandrunitonanydeviceorOSwithaJVM.Thisisachievedthroughcompilingtobytecode,whichtheJVMinterpretsorcompilesatruntime.ThisfeaturehassignificantlyboostedJava'sadoptionduetocross-platformdeployment,s

容器化技术(例如Docker)如何影响Java平台独立性的重要性?容器化技术(例如Docker)如何影响Java平台独立性的重要性?Apr 22, 2025 pm 06:49 PM

容器化技术如Docker增强而非替代Java的平台独立性。1)确保跨环境的一致性,2)管理依赖性,包括特定JVM版本,3)简化部署过程,使Java应用更具适应性和易管理性。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)