目录
  1.分析
  2.书写步骤
  (1)封装PageBean
  (2)书写Action
  (3)书写Service
  (4)书写Dao
  (5)完成struts以及spring的配置
  (6)书写前台list.jsp页面
二、BaseDao封装
  1.抽取BaseDao
  2.BaseDao设计思路
  3.BaseDao接口书写
  4.BaseDao的实现类
  5.业务Dao中的应用
首页 Java java教程 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

热AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
2 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
2 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

如何使用Python从列表中删除方括号 如何使用Python从列表中删除方括号 Sep 05, 2023 pm 07:05 PM

Python是一款非常有用的软件,可以根据需要用于许多不同的目的。Python可以用于Web开发、数据科学、机器学习等许多其他需要自动化处理的领域。它具有许多不同的功能,可以帮助我们执行这些任务。Python列表是Python的一个非常有用的功能之一。顾名思义,列表包含您希望存储的所有数据。它基本上是一组不同类型的信息。删除方括号的不同方法许多时候,用户会遇到列表项显示在方括号中的情况。在本文中,我们将详细介绍如何去掉这些括号,以便更好地查看您的列表。字符串和替换函数删除括号的最简单方法之一是在

如何使用Python的count()函数计算列表中某个元素的数量 如何使用Python的count()函数计算列表中某个元素的数量 Nov 18, 2023 pm 02:53 PM

如何使用Python的count()函数计算列表中某个元素的数量,需要具体代码示例Python作为一种强大且易学的编程语言,提供了许多内置函数来处理不同的数据结构。其中之一就是count()函数,它可以用来计算列表中某个元素的数量。在本文中,我们将详细介绍如何使用count()函数,并提供具体的代码示例。count()函数是Python的内置函数,用于计算某

制作 iPhone 上 iOS 17 提醒应用程序中的购物清单的方法 制作 iPhone 上 iOS 17 提醒应用程序中的购物清单的方法 Sep 21, 2023 pm 06:41 PM

如何在iOS17中的iPhone上制作GroceryList在“提醒事项”应用中创建GroceryList非常简单。你只需添加一个列表,然后用你的项目填充它。该应用程序会自动将您的商品分类,您甚至可以与您的伴侣或扁平伙伴合作,列出您需要从商店购买的东西。以下是执行此操作的完整步骤:步骤1:打开iCloud提醒事项听起来很奇怪,苹果表示您需要启用来自iCloud的提醒才能在iOS17上创建GroceryList。以下是它的步骤:前往iPhone上的“设置”应用,然后点击[您的姓名]。接下来,选择i

创建杂货清单的方法:使用 iPhone 的'提醒事项”App 创建杂货清单的方法:使用 iPhone 的'提醒事项”App Dec 01, 2023 pm 03:37 PM

在iOS17中,Apple在提醒应用程序中添加了一个方便的小列表功能,以便在您外出购买杂货时为您提供帮助。继续阅读以了解如何使用它并缩短您的商店之旅。当您使用新的“杂货”列表类型(在美国以外名为“购物”)创建列表时,您可以输入各种食品和杂物,并按类别自动组织它们。该组织使您在杂货店或外出购物时更容易找到您需要的东西。提醒中可用的类别类型包括农产品、面包和谷物、冷冻食品、零食和糖果、肉类、乳制品、鸡蛋和奶酪、烘焙食品、烘焙食品、家居用品、个人护理和健康以及葡萄酒、啤酒和烈酒。以下是在iOS17中创

我们可以在Java列表中插入空值吗? 我们可以在Java列表中插入空值吗? Aug 20, 2023 pm 07:01 PM

SolutionYes,Wecaninsertnullvaluestoalisteasilyusingitsadd()method.IncaseofListimplementationdoesnotsupportnullthenitwillthrowNullPointerException.Syntaxbooleanadd(Ee)将指定的元素追加到此列表的末尾。类型参数E −元素的运行时类型。参数e −要追加到此列表的元

Del和remove()在Python中的列表上有什么区别? Del和remove()在Python中的列表上有什么区别? Sep 12, 2023 pm 04:25 PM

在讨论差异之前,让我们先了解一下Python列表中的Del和Remove()是什么。Python列表中的Del关键字Python中的del关键字用于从List中删除一个或多个元素。我们还可以删除所有元素,即删除整个列表。示例使用del关键字从Python列表中删除元素#CreateaListmyList=["Toyota","Benz","Audi","Bentley"]print("List="

使用Python根据列表创建多个目录 使用Python根据列表创建多个目录 Sep 08, 2023 am 08:21 AM

Python凭借其简单性和多功能性,已成为各种应用程序中最流行的编程语言之一。无论您是经验丰富的开发人员还是刚刚开始编码之旅,Python都提供了广泛的功能和库,使复杂的任务变得易于管理。在本文中,我们将探讨一个实际场景,Python可以通过自动执行基于列表创建多个目录的过程来帮助我们。通过利用Python内置模块和技术的强大功能,我们可以有效地处理此任务,而无需手动干预。在本教程中,我们将深入研究创建多个目录的问题,并为您提供使用Python解决该问题的不同方法。在本文结束时,我们的目标是为您

无法显示win7无线网络列表 无法显示win7无线网络列表 Dec 22, 2023 am 08:07 AM

为了方便很多人移动办公,很多笔记本都带有无线网的功能,但有些人的电脑上无法显示WiFi列表,现在就给大家带来win7系统下遇到这种问题的处理方法,一起来看一下吧。win7无线网络列表显示不出来1、右键你电脑右下角的网络图标,选择“打开网络和共享中心”,打开后再点击左边的“更改适配器设置”2、打开后鼠标右键选择无线网络适配器,选择“诊断”3、等待诊断,如果系统诊断出问题那就修复它。4、修复完成之后,就可以看到WiFi列表了。

See all articles