- 浏览: 99645 次
- 性别:
- 来自: 深圳
最新评论
-
shuhai42:
曙海嵌入式学院提供以下课程的培训--中国最大的FPGA,DSP ...
敏捷开发之路,未来软件开发的变革? -
kaki:
建议吸收大家意见,打包下载吧。
泛型dao架构实现,封装crud等基本操作 -
xuhbiao:
不错,学习了。。
泛型dao架构实现,封装crud等基本操作 -
danjp:
LZ
单从你DAO的借口定制来说,我觉得不是太好,特别是更新方 ...
泛型dao架构实现,封装crud等基本操作 -
danjp:
jeff312 写道mfdkydyw4 写道EXvision ...
泛型dao架构实现,封装crud等基本操作
我用的是Spring+Hibernate+Struts框架:
这是我的DAO的核心代码
import org.springframework.orm.hibernate.support.HibernateDaoSupport;
// 用Spring支持的Hibernate方法,使Hibernate对数据库的操作继续瘦身
public List getOfficeBySearchCriteria(final String hsql,final int pageNo,final int page_size) throws DataAccessException // hsql 是如:"select office1 from Office as office1 order by office1.officename";pageNo 是第几页;page_size是每页记录数
{
String sql;
int total_count=0;
List offices=new ArrayList();
//offices= getHibernateTemplate().find("from Office office1 where office1.officename like ?", "%"+officeName+"%");
offices= getHibernateTemplate().find(hsql); //为了得到总记录数
total_count=offices.size();
crossPageInfo= crossPageBean.getCrossPageInfo(total_count,pageNo,page_size);
sql=hsql+ " limit " + (pageNo-1)*page_size + "," +page_size;
offices= getHibernateTemplate().find(sql); //为了得到页记录信息 System.out.println("The list offices size: "+offices.size());
return offices;
}
//其中crossPageBean.getCrossPageInfo只是得到页面的如:总页数、供多少页的信息等一般的翻页信息;
我在Action中是这样调用的:
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception
{
CrossPageInfo crossPageInfo=new CrossPageInfo();
String hsql="select office1 from Office office1 order by office1.officename";
String pageNo=request.getParameter("pageNo");
int pageNoi=1;
if(pageNo==null)
pageNo="1";
pageNoi=Integer.parseInt(pageNo);
int pageSize=5;
//List offices=getOfficeService().getAllOffice();
List offices=getOfficeService().getOfficeBySearchCriteria(hsql,pageNoi,pageSize);
crossPageInfo=getOfficeService().getCrossPageInfo();
System.out.println("The CorssPgaeInfo :"+crossPageInfo.getPageNo());
System.out.println(crossPageInfo.getPageSize());
request.setAttribute("offices",offices);
request.setAttribute("pageInfo",crossPageInfo);
return mapping.findForward("success");
//throw new UnsupportedOperationException("Generated method 'execute(...)' not implemented.");
}
//其中getOfficeService()只是提供接口服务的方法。
我的表现页面是这样的:
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-template.tld" prefix="template" %>
<%@ page import="com.uplus.util.CrossPageInfo"%>
<html>
<head>
<title>
mySearchCList
</title>
</head>
<body bgcolor="#ffffff">
<form name="form1" action="officesearch.do" method="post">
<table >
<tr>
<td>OfficeName:<input name="officeName" type="text"></td><td><input type="submit" name="sb" value="Search"></td>
</tr>
</table>
</form>
<br><a href="/jsp/office/officeadd.jsp">Add</a>
<table bgcolor="#DBE9F1" align="center" class="InputFrameMain" style="MARGIN: 0px" cellSpacing="1" cellPadding="0" BGALIGN="CENTER" BGVALIGn="middle" width="100%" VALIGN="middle" >
<tr><td align="center">OfficeName</td><td align="center">OfficePhone</td></tr>
<logic:iterate id="office" name="offices" >
<tr bgcolor="#ffffff">
<td align="center"><a href="officesee.do?id=<bean:write name='office' property='id'/>" target="_blank"><bean:write name="office" property="officename"/></a></td>
<td align="center"><bean:write name="office" property="officephone"/></td>
<td align="center"><a href="officeedit.do?id=<bean:write name='office' property='id'/>" >Update </a>
<td align="center"><a href="officedel.do?id=<bean:write name='office' property='id'/>" onclick="return confirm('Would You Detele It? ')" >Delete </a>
</tr>
</logic:iterate>
</table>
<%CrossPageInfo cpInfo=(CrossPageInfo)request.getAttribute("pageInfo");%>
<table width="100%" align="center" class="InputFrameMain" style="MARGIN: 0px" cellPadding="0" cellSpacing="0">
<tr ><form action="officelist.do" method="post" onsubmit='return checkform2(this)'>
<td width=70%>Total <font color="blue"><%=cpInfo.getTotalRow()%></font>&items found,Total&<font color="blue"><%=cpInfo.getTotalPage()%></font> Pages,Current No <font color="blue"><%=cpInfo.getPageNo()%> </font>Page.
Go to <input name="pageNo" type="text" size="5" class="input">Page
<input name="sb2" type="submit" class="button" value="Go">
</td></form>
<td width=30% align='left'>
<%if(cpInfo.getPageNo()>1){%>
&<a href="officelist.do?pageNo=1">
<%}%>First</a>
<%if(cpInfo.getPageNo()>1){ %>
&<a href="officelist.do?pageNo=<%=cpInfo.getPageNo()-1%>">
<%}%>Previous</a>
<%if(cpInfo.getPageNo()<cpInfo.getTotalPage()){ %>
&<a href="officelist.do?pageNo=<%=cpInfo.getPageNo()+1%>">
<%}%>Next</a>
<%if(cpInfo.getTotalPage()>cpInfo.getPageNo()){%>
&<a href="officelist.do?pageNo=<%=cpInfo.getTotalPage()%>">
<%}%>Last</a></td>
</tr>
</table>
</body>
</html>
大家可以看一下我的处理过程,其中在DAO里为了得到总计录数执行了一次次数据表查询HSQL;得到数据又执行了一次HSQL,我觉得这样好像有些不太好,大家觉得怎样?大家提出宝贵的意见吧!
sorphi回复:
--------------------------------------------------------------------------------
需要改进:
hsql="select office1 from Office as office1 order by office1.officename"
有两处不好的:
1,实际查出了符合条件的所有po,多么的浪费资源
2,查总记录根本不需要order by这种影响性能的多余的clause
改成"select count(id) from Office where ..."
用query().iterate().next()得到总记录数
另外,分页,hibernate有了相应的包装,不需要你进行这样的sql构造:
sql=hsql+ " limit " + (pageNo-1)*page_size + "," +page_size;
--------------------------------------------------------------------------------
继续:
首先感谢sorphi的建议。
DAO的核心代码改为:
public List getOfficeBySearchCriteria(final String hsql,final int pageNo,final int page_size) throws Exception
{
Session session=getSession();
String sql;
int total_count=0;
int pageNog=1;
total_count=getTotalCount(session,hsql);
//System.out.println("The counts :"+total_count);
List offices=new ArrayList();
crossPageInfo= crossPageBean.getCrossPageInfo(total_count,pageNo,page_size);
pageNog=pageNo;
if(crossPageInfo.getTotalPage()<pageNo)pageNog=crossPageInfo.getTotalPage();
if(pageNo<1)pageNog=1;
// sql=hsql+ " limit " + (pageNog-1)*page_size + "," +page_size;
// offices= getHibernateTemplate().find(sql);
//被屏蔽掉的是原来的处理
//以下是改过的处理方法
Query query=null;
query=session.createQuery(hsql);
int start=(pageNog-1)*page_size;
int rowNum=page_size;
query.setFirstResult(start);
query.setMaxResults(rowNum);
offices = query.list();
return offices;
}
public static int getTotalCount(Session session, String hql) throws Exception
{
Integer amount = new Integer(0);
int sql_from = hql.indexOf(" from");
int sql_orderby=hql.indexOf("order by");//为了改进
String countStr="";
if(sql_orderby>0)
{
countStr="select count(*) "+hql.substring(sql_from,sql_orderby);
}
else
countStr = "select count(*) "+hql.substring(sql_from);
Query query = session.createQuery(countStr);
if(!query.list().isEmpty()){
amount = (Integer) query.list().get(0);
}
else
return 0;
return amount.intValue();
}
发表评论
-
ExtJs+struts+spring+mybatis通用管理后台程序框架
2015-01-20 10:08 1209花了一段时间学习Extjs,总算学有所成,并在项目中有成 ... -
Oracle JOB问题解决汇总
2009-05-23 22:58 3701一。job的运行频率设置 1.每天固定时间运行,比如早 ... -
PostInsertEventListener 插入发生死循环问题
2009-01-15 11:18 2133package com.easou.ad.listener; ... -
怎么让文章自动适应表格
2009-01-09 23:24 890style="word-break:brea ... -
敏捷开发之路,未来软件开发的变革?
2008-12-27 19:51 3518敏捷开发这个名词已经不是第一次听到了,大家都推崇它好,就算是华 ... -
memcached使用与优化
2008-12-26 13:01 165691、客户端在与 memcached 服务建立连接之后,进行存取 ... -
spring hibernate 事务配置 2
2008-11-27 00:13 1181<bean id="txProxyTempl ... -
spring hibernate 事务配置
2008-11-27 00:07 1413<!--事务拦截器,激活事务管理器所必须的bean ... -
我也SHOW下JDBC底层操作工具类
2008-11-10 11:15 3346/* * JDBCUtil.java * * ... -
泛型dao架构实现,封装crud等基本操作
2008-11-10 11:07 4249今天闲着没事,根据公司 ... -
项目沟通7个好习惯
2008-11-07 13:17 876写道 简介 项目沟通7个好习惯 习惯一:态度积极 习惯二:牢记 ... -
webwork+hibernate+spring项目源代码自动生成(附源代码)
2008-11-05 22:39 1513自己写了一个自动生成源代码的工具,代码自动生成,以及一个完整项 ... -
eclipse快捷键大全
2008-11-02 20:58 8301. 常用快捷键 (1)Ctrl+Space 说明:内 ... -
一个js写的StringBuffer类
2008-11-02 11:15 1325function StringBuffer() { this ... -
Spring jdbc 操作实例
2008-11-02 11:09 3138package com.szxhdz.dao; import ... -
oracle分页
2008-11-02 11:07 904从taba中获取第1到10条记录 select * fro ... -
转载40种网页常用小技巧(javascript)--备不时之需
2008-11-01 18:16 913• 转载40种网页常用小技巧(javascript)--备不时 ... -
webwork和spring整合
2008-11-01 18:11 1269始终没有搞明白在appfuse中是通过什么方式将webwork ... -
Struts标记库
2008-11-01 18:10 855Struts标记库JSP视窗组件 ... -
springIOC
2008-11-01 18:07 895首先想说说IoC(Inversion ...
相关推荐
STRUTS2+HIBERNATE分页 实现代码详细的分页实现代码实现代码详细的分页实现代码实现代码详细的分页实现代码实现代码详细的分页实现代码
使用Struts + Spring + Hibernate完成分页笔记使用Struts + Spring + Hibernate完成分页笔记使用Struts + Spring + Hibernate完成分页笔记
2.Action里通过struts2-spring-plugin.jar插件自动根据名字注入。 3.Ajax无刷新异步调用Struts2,返回Json数据,以用户注册为例。 4.在服务端分页查询功能,优点:实时性:跳页才查询。数据量小:只加载当前页的记录...
Spring+Hibernate+Struts实现分页
spring+hibernate+struts实现分页功能,应用软件eclipse、sqlserver
struts2+spring+hibernate3的简易图书管理系统,特别适合初学者集成! 实现了简单的分页和模糊查询。(基本上涵盖了开发ssh2的包),值得拥有哦! 注:数据库sql文件(mysql 5.5)在里面,要自己配好!
struts2 + spring 3 + hibernate3.3整合实现图书馆管理系统登陆和注册,以及一个对书籍表的增删改查,分页显示。
新闻发布系统,用struts+spring+hibernate实现
系统搭建使用struts2 + spring + hibernate + easyui 整合的, 功能包括: 分页查询,自定义页面数据显示量,用户名模糊搜索,创建时间段,更新时间段内用户信息搜索,双击行进行编辑,多行选中删除,右键菜单已经写好...
Struts2 + Spring2 + Hibernate3经典分页(包含java及jsp源码) Struts2 + Spring2 + Hibernate3经典分页(包含java及jsp源码) Struts2 + Spring2 + Hibernate3经典分页(包含java及jsp源码)
spring+struts2+hibernate框架,单表操作,有拦截器和分页功能
struts+spring+hibernate 的一个分页 需要滴拿去
此压缩包含有两个工程,一个工程是struts2+spring2.5+hibernate3.3整合的服务器端技术(全注解)(云端),另一个工程是android的手机应用,包含对ssh整合的云端数据的访问,能够在android输入信息,将数据传递给...
高效率spring+struts+hibernate分页算法
struts2+spring+hibernate分页,事务,自定义切面,事务的配置
重磅推出 struts+spring+hibernate+dwr整合分页例子
传统的网页设计只在一个JSP或者ASP页面中书写所有关于数据库操作的代码,那样做分页可能简单一点,但当把网站分层开发后,分页就比较困难了,下面是我做Spring+Hibernate+Struts2项目时设计的分页代码,与大家分享...
struts2+spring+hibernate分页显示 分页显示一直是web开发中一大烦琐的难题,传统的网页设计只在一个JSP或者ASP页面中书写所有关于数据库操作的代码,那样做分页可能简单一点,但当把网站分层开发后,分页就比较困难...
利用ext3+struts2+hibernate+spring的CRUD+分页这四个框架实现crud,详细说明grid的用法,
本系统运用了spring+hibernate+struts加上分页,文件上传等等供初学者,参考学习!