lijiannan_1981
著名写手
著名写手
  • 铜币15枚
  • 威望0点
  • 贡献值0点
阅读:1291回复:7

使用Spring和Hibernate快一年了

楼主#
更多 发布于:2006-01-17 16:39
      在项目中使用Spring和Hibernate已经快一年了,由于是第一次解除,在开发的过程中遇到了很多问题,现在基本上都解决了,但也有一些问题比较郁闷。都说Spring对Hibernate的支持很不错,说它们是绝佳搭配,确实是这样:

(1)Spring对Hibernate的支持确实很不错,它提供的HibernateDaoSupport类封装了SessionFactory,用户不需要关心SessonFactory的创建和和Session的获取;

(2)HibernateTemplate提供了对大多数Session操作的封装,而且加上Spring的声明型事务管理,这一切工作的很好。

(3)AOP的设计思想实在另我爱不释手,但是AOP充其量是OOP的友好的补充罢了;
  
      但是,实际应用中有一些问题,比如分页,Hibernate提供了分页机制,而Spring没有提供对其的支持,但提供了一个PagedListHolder,一次把所有数据都取出来放到PagedListHolder里,然后把PagedListHolder放到Session中,这样会有两个问题,首先是第一次访问时服务器压力很大,内存消耗较多,其次是Session中的数据可能会过期,所以Spring提供PagedListHolder对于一般的应用还可以,但对于不允许数据过期的应用来说就不合适了。这时候用户得自己在DAO中来处理分页问题,而这时候不能使用HibernateTemplate,那么也就只能自己处理异常情况,关闭Session的问题。



[ 2006-01-19 09:12:27 lijiannan_1981 修改 ]
无。
0000
作家
作家
  • 铜币143枚
  • 威望34点
  • 贡献值1点
1C#
发布于:2006-01-18 08:34
Re:使用Spring和Hibernate快一年了
这两个东西偶一直想学习, 可惜...哎,米有机会啊~
但提供了一个PagedListHolder,一次把所有数据都取出来放到PagedListHolder里,然后把PagedListHolder放到Session中
偶对这里的分页机制有一点不理解, 一次把所有数据全取出来是不是相当于一条 select a, b, c from table_name where d=e 然后把所有结果都保存在内存中? 这样的话, 内存消耗看起来是挺吓人的, 而且我觉得用户大多数情况下不会把所有的页(分页后的)都看一遍, 比如说一共有20页内容, 一般用户看了10页就已经很有耐心了, 后面的10页完全没有作用, 那这样起码后面10页的内存消耗是没有任何意义的, 只是加重了服务器负担, 大量用户访问的时候...
.--. |o_o | |:_/ | // \ \ (| | ) /'\_ _/`\ \___)=(___/
lijiannan_1981
著名写手
著名写手
  • 铜币15枚
  • 威望0点
  • 贡献值0点
2C#
发布于:2006-01-18 11:43
Re:使用Spring和Hibernate快一年了
这两个东西偶一直想学习, 可惜...哎,米有机会啊~偶对这里的分页机制有一点不理解, 一次把所有数据全取出来是不是相当于一条 select a, b, c from table_name where d=e 然后把所有结果都保存在内存中? 这样的话, 内..


先生说的是很有道理

可客户看不看在他们呀,我们这个功能如果不作就是不是一般的软件bug了,为此我设计了一个分页的标签类今天开源给大家吧,自认为写的不错:

package wap.util;

import java.util.List;

/**
 * <p>Title:class Page </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company:Aria</p>
 * @author leener
 * @version 1.0
 */

public class Page implements java.io.Serializable {

private static final long serialVersionUID = 7474535942081689862L;

public static final int DEFAULT_PAGE_SIZE = 10;

public static final int DEFAULT_MAX_LINKED_PAGES = 10;

private int pageSize = DEFAULT_PAGE_SIZE;

private int page = 0;

private int recordCount = 0;

private int maxLinkedPages = DEFAULT_MAX_LINKED_PAGES;

private List source;

/**
* Create a new holder instance with the given source list.
*/
public Page(List source, int recordCount, int page, int pageSize) {
setSource(source);
setRecordCount(recordCount);
setPage(page);
setPageSize(pageSize);
}

public List getSource() {
return this.source;
}

public void setSource(List source) {
this.source = source;
}

public int getRecordCount() {
return this.recordCount;
}

public void setRecordCount(int recordCount) {
this.recordCount = recordCount;
}

public int getMaxLinkedPages() {
return this.maxLinkedPages;
}

public void setMaxLinkedPages(int maxLinkedPages) {
this.maxLinkedPages = maxLinkedPages;
}

public int getPage() {
return this.page;
}

public void setPage(int page) {
this.page = page;
}

public int getPageSize() {
return this.pageSize;
}

public void setPageSize(int pageSize) {
if (pageSize != this.pageSize)
this.pageSize = pageSize;
}

/**
* Return the number of pages for the current source list.
*/
public int getPageCount() {
float nrOfPages = (float) getRecordCount() / getPageSize();
return (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages);
}

/**
* Return if the current page is the first one.
*/
public boolean isFirstPage() {
return getPage() == 0;
}

/**
* Return if the current page is the last one.
*/
public boolean isLastPage() {
return getPage() == getPageCount() -1;
}

/**
* Switch to previous page.
* Will stay on first page if already on first page.
*/
public void previousPage() {
if (!isFirstPage()) {
this.page--;
}
}

/**
* Switch to next page.
* Will stay on last page if already on last page.
*/
public void nextPage() {
if (!isLastPage()) {
this.page++;
}
}

/**
* Return the total number of elements in the source list.
*/
public int getNrOfElements() {
return getRecordCount();
}

/**
* Return the element index of the first element on the current page.
* Element numbering starts with 0.
*/
public int getFirstElementOnPage() {
return (getPageSize() * getPage());
}

/**
* Return the element index of the last element on the current page.
* Element numbering starts with 0.
*/
public int getLastElementOnPage() {
int endIndex = getPageSize() * (getPage() + 1);
return (endIndex > getRecordCount() ? getRecordCount() : endIndex) -1;
}

/**
* Return a sub-list representing the current page.
*/
public List getPageList() {
return getSource();
}

/**
* Return the first page to which create a link around the current page.
*/
public int getFirstLinkedPage() {
return Math.max(0, this.getPage() - (getMaxLinkedPages() /2));
}

/**
* Return the last page to which create a link around the current page.
*/
public int getLastLinkedPage() {
return Math.min(getFirstLinkedPage() + getMaxLinkedPages() -1, this.getPageCount() -1);
}
}

-----------------------------------------------------------------------------------------------------------------------------------------------------------------
在项目文件中增加如下描述文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE taglib
  PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
         "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<shortname>LEENER</shortname>
<description>LEENER自定义标记库</description>

<tag>
<name>page</name>
<tag-class>wap.taglib.admin.PageTag</tag-class>
<body-content>empty</body-content>
<description>分页标记</description>
<attribute>
<name>page</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<description>存有分页数据的Page对象</description>
</attribute>
<attribute>
<name>useForm</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<description>是否使用Form</description>
</attribute>
<attribute>
<name>formName</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
<description>Form的名字(如果使用Form)</description>
</attribute>
<attribute>
<name>uri</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
<description>请求的URL(如果未使用Form)</description>
</attribute>
</tag>

</taglib>


我的回答满意吗先生,我认为这是解决分页问题的最优方案了:) [ 2006-01-18 11:43:38 lijiannan_1981 修改 ]
无。
万贤
作家
作家
  • 铜币1枚
  • 威望24点
  • 贡献值1点
3C#
发布于:2006-01-18 13:07
Re:使用Spring和Hibernate快一年了
麻烦老大可以用c++再重写一边么?赫赫~~
mumu
写手
写手
  • 铜币0枚
  • 威望0点
  • 贡献值0点
4C#
发布于:2006-01-19 02:45
Re:使用Spring和Hibernate快一年了
谢天谢地看不懂:(
每个方法写注释是你们老大要求的还是工具自动生成的?

用c++写cgi怕更恐怖.
王小波说:“中年妇女在中国是一种自然灾害,这倒不是因为她们不好看,而是因为她们故意要恶心人。” 一天,我乘坐公交车,一位MM突然转过头来对我说:“你帅吗?”我说:“我不帅!”MM突然给我一巴掌,并说:“我最讨厌说谎的人了!” 如果你更热爱金钱而非自由,更习惯于被奴役的安宁而畏惧令人充满活力的争取自由的抗争,那么,请你静静地走开。我们不会乞求你的建议或是帮助。伏下身去讨好那喂养你的人吧。但愿身上的锁链不会给你造成太多的痛苦,但愿未来的人们不会记起你曾经是我们的国人 Samuel Adams: 18世纪美国独立革命重要领袖,著有“殖民者的权利”
lijiannan_1981
著名写手
著名写手
  • 铜币15枚
  • 威望0点
  • 贡献值0点
5C#
发布于:2006-01-19 09:11
Re:使用Spring和Hibernate快一年了
to mumu:
龙头这家伙麻烦的很,要求我们写注释,好在有工具生成,开发人员自己写的注释全部要求汉语
无。
mumu
写手
写手
  • 铜币0枚
  • 威望0点
  • 贡献值0点
6C#
发布于:2006-01-19 23:24
Re:使用Spring和Hibernate快一年了
比对http://www.wait4c.com/bbs/message.asp?id=518916
有点感慨,好的代码是不需要注释的.因为代码比注释还简洁.

lijiannan别生气哈,管窥之见:)通常好的函数名胜于好的注释.
王小波说:“中年妇女在中国是一种自然灾害,这倒不是因为她们不好看,而是因为她们故意要恶心人。” 一天,我乘坐公交车,一位MM突然转过头来对我说:“你帅吗?”我说:“我不帅!”MM突然给我一巴掌,并说:“我最讨厌说谎的人了!” 如果你更热爱金钱而非自由,更习惯于被奴役的安宁而畏惧令人充满活力的争取自由的抗争,那么,请你静静地走开。我们不会乞求你的建议或是帮助。伏下身去讨好那喂养你的人吧。但愿身上的锁链不会给你造成太多的痛苦,但愿未来的人们不会记起你曾经是我们的国人 Samuel Adams: 18世纪美国独立革命重要领袖,著有“殖民者的权利”
0000
作家
作家
  • 铜币143枚
  • 威望34点
  • 贡献值1点
7C#
发布于:2006-01-20 09:25
Re:使用Spring和Hibernate快一年了
不过写注释对于生成doc文档有好处哦~~~真接 javadoc 就可以了~~方法名再长能表达的内容也是有限的, 我觉得还是写一点的好~~
.--. |o_o | |:_/ | // \ \ (| | ) /'\_ _/`\ \___)=(___/
游客

返回顶部