目錄
  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.能量晶體解釋及其做什麼(黃色晶體)
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
3 週前 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解決該問題的不同方法。在本文結束時,我們的目標是為您

使用Python的reverse()函數反轉列表 使用Python的reverse()函數反轉列表 Nov 18, 2023 pm 02:14 PM

使用Python的reverse()函數反轉列表,需要具體程式碼範例在Python中,我們經常需要在程式設計中對列表進行操作,其中反轉列表是常見的一種需求,這時候我們可以使用Python內建的reverse ()函數來實現。 reverse()函數的作用是反轉列表中的元素順序,即將列表中第一個元素變為最後一個元素,第二個元素變為倒數第二個元素,以此類推。下面是使用Py

See all articles