<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Tin&#039;s Blog &#187; Java</title>
	<atom:link href="http://www.diamondtin.com/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.diamondtin.com</link>
	<description>you are coming a long way...</description>
	<lastBuildDate>Thu, 05 Jan 2012 00:19:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>解决Mac环境下的Java Version问题</title>
		<link>http://www.diamondtin.com/2009/solve_java_version_issues_of_mac/</link>
		<comments>http://www.diamondtin.com/2009/solve_java_version_issues_of_mac/#comments</comments>
		<pubDate>Mon, 27 Jul 2009 02:01:32 +0000</pubDate>
		<dc:creator>tin</dc:creator>
				<category><![CDATA[Tech.技术]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[terminal]]></category>

		<guid isPermaLink="false">http://tin.zztin.com/?p=778</guid>
		<description><![CDATA[前一段时间遇到了一个奇怪的问题：我的Mac下面的Lotus Notes 8.5和FreeMind 0.9rc4都不能工作了，它们的共同特点就是都基于Java。我试验了在Terminal里面启动/Applications/Notes/Contents/MacOS/Notes的话是可以... ]]></description>
			<content:encoded><![CDATA[<p>前一段时间遇到了一个奇怪的问题：我的Mac下面的Lotus Notes 8.5和FreeMind 0.9rc4都不能工作了，它们的共同特点就是都基于Java。我试验了在Terminal里面启动/Applications/Notes/Contents/MacOS/Notes的话是可以启动的，但是会启动以前的Basic界面（后来我才知道可能它是个Cabon应用，而不是Java版本）。所以就一直凑合着用。<br />
可是昨天我尝试运行/Applications/Utilities/Java Preference的时候它也无法启动了，我这才觉得这不是个简单问题，必须要彻底解决。</p>
<p>
我回忆了一下，我最开始的时候尝试用Java Preference的时候修改了JDK的顺序，将JDK6放在最上方，可是在Terminal里面（java -version）依然只可以访问1.5。所以当时就手动修改了系统的指向：
</p>
<p><code><br />
/System/Library/Frameworks/JavaVM.frameworks/Versions/Current -&gt; /System/Library/Frameworks/JavaVM.frameworks/Versions/1.6<br />
/System/Library/Frameworks/JavaVM.frameworks/Versions/CurrentJDK -&gt; /System/Library/Frameworks/JavaVM.frameworks/Versions/1.6<br />
</code></p>
<p>这样修改以后就工作了，命令行里面执行java -version显示：</p>
<p><code>java version "1.6.0_13"<br />
Java(TM) SE Runtime Environment (build 1.6.0_13-b03-211)<br />
Java HotSpot(TM) 64-Bit Server VM (build 11.3-b02-83, mixed mode)<br />
</code></p>
<p>但是大家千万不要这样做，这个其实是后面很多问题的一个重要原因。</p>
<p>如果你修改了上面的指向，我推荐你unlink它们，然后下载Java for Mac OSX 10.5 Update 4 （http://www.apple.com/downloads/macosx/apple/application_updates/javaformacosx105update4.html）重新安装一下就可以重新生成这些link了。不过记得要unlink自己手工建立的符号链接（比如我的是Current, CurrentJDK, 1.6），否则升级包无法替换你建立的那些。</p>
<p>但是我这样做了以后还是没有解决所有问题，Java Preference可以正常启动了，不过它设置的顺序依然不管用，java -version总是返回1.5。我突然想到估计是运行我们的Ant脚本曾经提醒过JAVA_HOME的问题，我手动设置过一个，忘记去掉了。所以去查我的.profile，发现了罪魁祸首：</p>
<p><code>export JAVA_HOME='/Library/Java/Home'</code></p>
<p>这个设置是错误的，虽然它存在，可是它不会根据Java Preference修改的结果去寻找你的Java的bin和lib，也就造成了后面的一系列错误。</p>
<p>修改为（或者删掉JAVA_HOME声明也是安全的）：<br />
<code>export JAVA_HOME='/System/Library/Frameworks/JavaVM.framework/Current/Home'</code></p>
<p>问题解决，教训是如果你手头有工具，应该学会使用它，如果没有耐心学习就错误的使用工具，一开始感觉能解决问题，可是后来它会造成越来越多的错误。那么决绝方案就是回到原点，恢复，学习，正确的使用工具。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.diamondtin.com/2009/solve_java_version_issues_of_mac/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Webwork 2.2的Action是否使用Spring的prototype­获取的性能对比</title>
		<link>http://www.diamondtin.com/2006/webwork-22%e7%9a%84action%e6%98%af%e5%90%a6%e4%bd%bf%e7%94%a8spring%e7%9a%84prototype%c2%ad%e8%8e%b7%e5%8f%96%e7%9a%84%e6%80%a7%e8%83%bd%e5%af%b9%e6%af%94/</link>
		<comments>http://www.diamondtin.com/2006/webwork-22%e7%9a%84action%e6%98%af%e5%90%a6%e4%bd%bf%e7%94%a8spring%e7%9a%84prototype%c2%ad%e8%8e%b7%e5%8f%96%e7%9a%84%e6%80%a7%e8%83%bd%e5%af%b9%e6%af%94/#comments</comments>
		<pubDate>Thu, 16 Feb 2006 21:06:00 +0000</pubDate>
		<dc:creator>tin</dc:creator>
				<category><![CDATA[Tech.技术]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://tin.zztin.com/2006/02/webwork-22%e7%9a%84action%e6%98%af%e5%90%a6%e4%bd%bf%e7%94%a8spring%e7%9a%84prototype%c2%ad%e8%8e%b7%e5%8f%96%e7%9a%84%e6%80%a7%e8%83%bd%e5%af%b9%e6%af%94/</guid>
		<description><![CDATA[本文在060216进行了修改，因为发现了测试中的错误！注意5.5和7的内容。 1、引子：其实是ajoo的这篇“Nuts和Spring 1.2.6 效率对比”和“IoC容器的prototype性能测试 ”，他们在Javaeye上详细讨论了Spring... ]]></description>
			<content:encoded><![CDATA[<div>
<p><font color="#ff0000">本文在060216进行了修改，因为发现了测试中的错误！注意5.5和7的内容。</font></p>
<p>1、引子：<br />其实是ajoo的这篇“Nuts和Spring 1.2.6 效率对比”和“IoC容器的prototype性能测试 ”，他们在Javaeye上详细讨论了Spring的prototype的缺陷。<br />Spring的prototype指的就是singleton=&quot;false&quot;的bean，具体可以看Spring参考手册“3.2.5. To singleton or not to singleton”介绍。
</p>
<p>2、Webwork 2.2的Spring结合问题：<br />Webwork 2.2已经抛弃自己的IoC，默认使用Spring的IoC。<br />上在OpenSymphony的官方Wiki，和jscud后来的几篇文章中没有特别提出prototype的问题。但是托他们的福，我们已经顺利的使Spring和Webwork良好的协同工作起来了。<br />可是而后的一些问题却把prototype的问题搞得神秘起来……<br />ajoo的测试中指出Spring的prototype性能很差，参见后面参考中的一篇文章和Javaeye的讨论。<br />而后又发现robbin在Javaeye的Wiki上面的“集成webwork和spring”中的最后注到：<br />“注意：目前并不推荐使用Spring来管理Webwork Action，因为对于prototype类型的bean来说，Spring创建bean和调用bean的效率是很低的！更进一步信息请看IoC容器的prototype性能测试”<br />这就使我们常用的Spring+Webwork2.2的连接中使用的prototype的问题被摆出来了。<br />我现在的项目中使用了prototype的方式将Webwork Action使用Spring进行显示的装配，我担心这个性能的问题会很严重，所以今天花了半天时间具体测试了一下。
</p>
<p>3、Prototype VS autowire的解释：<br />我不知道怎么命名两种方式好，所以这里先做个解释：<br />spring的配置中Action会有个id，如：</p>
<div><img src="http://spaces.msn.com/Images/OutliningIndicators/None.gif" align="top" /><span style="color:#0000ff">&lt;</span><span style="color:#800000">bean </span><span style="color:#ff0000">id</span><span style="color:#0000ff">=&quot;someAction&quot;</span><span style="color:#ff0000"> class</span><span style="color:#0000ff">=&quot;com.tin.action.SomeAction&quot;</span><span style="color:#ff0000"> parent</span><span style="color:#0000ff">=&quot;basicActionWithAuthtication&quot;</span><span style="color:#ff0000"> singleton</span><span style="color:#0000ff">=&quot;false&quot;</span><span style="color:#0000ff">&gt;</span><span style="color:#000000"><br /><img src="http://spaces.msn.com/Images/OutliningIndicators/None.gif" align="top" /> </span><span style="color:#0000ff">&lt;</span><span style="color:#800000">property </span><span style="color:#ff0000">name</span><span style="color:#0000ff">=&quot;someDAO&quot;</span><span style="color:#0000ff">&gt;</span><span style="color:#000000"><br /><img src="http://spaces.msn.com/Images/OutliningIndicators/None.gif" align="top" /> </span><span style="color:#0000ff">&lt;</span><span style="color:#800000">ref </span><span style="color:#ff0000">bean</span><span style="color:#0000ff">=&quot;someDAO&quot;</span><span style="color:#ff0000"> </span><span style="color:#0000ff">/&gt;</span><span style="color:#000000"><br /><img src="http://spaces.msn.com/Images/OutliningIndicators/None.gif" align="top" /> </span><span style="color:#0000ff">&lt;/</span><span style="color:#800000">property</span><span style="color:#0000ff">&gt;</span><span style="color:#000000"><br /><img src="http://spaces.msn.com/Images/OutliningIndicators/None.gif" align="top" /></span><span style="color:#0000ff">&lt;/</span><span style="color:#800000">bean</span><span style="color:#0000ff">&gt;</span></div>
</p>
<p>我指的prototype方式就是在xwork中这样配置：</p>
<div><img src="http://spaces.msn.com/Images/OutliningIndicators/None.gif" align="top" /><span style="color:#0000ff">&lt;</span><span style="color:#800000">action </span><span style="color:#ff0000">name</span><span style="color:#0000ff">=&quot;someAction&quot;</span><span style="color:#ff0000"> class</span><span style="color:#0000ff">=&quot;someAction&quot;</span><span style="color:#0000ff">&gt;</span></div>
</p>
<p>而autowire方式就是指在xwork中这样配置：</p>
<div><img src="http://spaces.msn.com/Images/OutliningIndicators/None.gif" align="top" /><span style="color:#0000ff">&lt;</span><span style="color:#800000">action </span><span style="color:#ff0000">name</span><span style="color:#0000ff">=&quot;someAction&quot;</span><span style="color:#ff0000"> class</span><span style="color:#0000ff">=&quot;com.tin.action.SomeAction&quot;</span><span style="color:#0000ff">&gt;</span></div>
</p>
<p>看起来相同，但其实不同（我以前发过帖子，其中说这几种方法都可，但是其实它们的机制是不同的。
</p>
<p>4、Portotye和autowire在XWork的SpringObjectFactory中是如何运作的：<br />我们先看一下代码，就能明白两者的区别了：</p>
<div><img src="http://spaces.msn.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img style="display:none" src="http://spaces.msn.com/Images/OutliningIndicators/ContractedBlock.gif" align="top" /><span style="color:#0000ff">public</span><span style="color:#000000"> Object buildBean(String beanName, Map extraContext) </span><span style="color:#0000ff">throws</span><span style="color:#000000"> Exception </span><span style="border-right:#808080 1px solid;border-top:#808080 1px solid;display:none;border-left:#808080 1px solid;border-bottom:#808080 1px solid;background-color:#ffffff"><img src="http://spaces.msn.com/Images/dot.gif" /></span><span><span style="color:#000000">{<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img style="display:none" src="http://spaces.msn.com/Images/OutliningIndicators/ContractedSubBlock.gif" align="top" /> </span><span style="color:#0000ff">try</span><span style="color:#000000"> </span><span style="border-right:#808080 1px solid;border-top:#808080 1px solid;display:none;border-left:#808080 1px solid;border-bottom:#808080 1px solid;background-color:#ffffff"><img src="http://spaces.msn.com/Images/dot.gif" /></span><span><span style="color:#000000">{<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" /> </span><span style="color:#0000ff">return</span><span style="color:#000000"> appContext.getBean(beanName);<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img style="display:none" src="http://spaces.msn.com/Images/OutliningIndicators/ContractedSubBlock.gif" align="top" />        }</span></span><span style="color:#000000"> </span><span style="color:#0000ff">catch</span><span style="color:#000000"> (NoSuchBeanDefinitionException e) </span><span style="border-right:#808080 1px solid;border-top:#808080 1px solid;display:none;border-left:#808080 1px solid;border-bottom:#808080 1px solid;background-color:#ffffff"><img src="http://spaces.msn.com/Images/dot.gif" /></span><span><span style="color:#000000">{<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" />            Class beanClazz </span><span style="color:#000000">=</span><span style="color:#000000"> getClassInstance(beanName);<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" /> </span><span style="color:#0000ff">return</span><span style="color:#000000"> buildBean(beanClazz, extraContext);<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />        }</span></span><span style="color:#000000"><br /><img src="http://spaces.msn.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />    }</span></span><span style="color:#000000"><br /><img src="http://spaces.msn.com/Images/OutliningIndicators/None.gif" align="top" /><br /><img src="http://spaces.msn.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img style="display:none" src="http://spaces.msn.com/Images/OutliningIndicators/ContractedBlock.gif" align="top" /> </span><span style="color:#0000ff">public</span><span style="color:#000000"> Object buildBean(Class clazz, Map extraContext) </span><span style="color:#0000ff">throws</span><span style="color:#000000"> Exception </span><span style="border-right:#808080 1px solid;border-top:#808080 1px solid;display:none;border-left:#808080 1px solid;border-bottom:#808080 1px solid;background-color:#ffffff"><img src="http://spaces.msn.com/Images/dot.gif" /></span><span><span style="color:#000000">{<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" />        Object bean;<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" /><br /><img src="http://spaces.msn.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img style="display:none" src="http://spaces.msn.com/Images/OutliningIndicators/ContractedSubBlock.gif" align="top" /> </span><span style="color:#0000ff">try</span><span style="color:#000000"> </span><span style="border-right:#808080 1px solid;border-top:#808080 1px solid;display:none;border-left:#808080 1px solid;border-bottom:#808080 1px solid;background-color:#ffffff"><img src="http://spaces.msn.com/Images/dot.gif" /></span><span><span style="color:#000000">{<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" />            bean </span><span style="color:#000000">=</span><span style="color:#000000"> autoWiringFactory.autowire(clazz, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, </span><span style="color:#0000ff">false</span><span style="color:#000000">);<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img style="display:none" src="http://spaces.msn.com/Images/OutliningIndicators/ContractedSubBlock.gif" align="top" />        }</span></span><span style="color:#000000"> </span><span style="color:#0000ff">catch</span><span style="color:#000000"> (UnsatisfiedDependencyException e) </span><span style="border-right:#808080 1px solid;border-top:#808080 1px solid;display:none;border-left:#808080 1px solid;border-bottom:#808080 1px solid;background-color:#ffffff"><img src="http://spaces.msn.com/Images/dot.gif" /></span><span><span style="color:#000000">{<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" /> </span><span style="color:#008000">//</span><span style="color:#008000"> Fall back</span><span style="color:#008000"><br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" /></span><span style="color:#000000">            bean </span><span style="color:#000000">=</span><span style="color:#000000"> </span><span style="color:#0000ff">super</span><span style="color:#000000">.buildBean(clazz, extraContext);<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />        }</span></span><span style="color:#000000"><br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" /><br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" />        bean </span><span style="color:#000000">=</span><span style="color:#000000"> autoWiringFactory.applyBeanPostProcessorsBeforeInitialization(bean, bean.getClass().getName());<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" /> </span><span style="color:#008000">//</span><span style="color:#008000"> We don&#8217;t need to call the init-method since one won&#8217;t be registered.</span><span style="color:#008000"><br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" /></span><span style="color:#000000">        bean </span><span style="color:#000000">=</span><span style="color:#000000"> autoWiringFactory.applyBeanPostProcessorsAfterInitialization(bean, bean.getClass().getName());<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" /><br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" /> </span><span style="color:#0000ff">return</span><span style="color:#000000"> autoWireBean(bean, autoWiringFactory);<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />    }</span></span><span style="color:#000000"><br /><img src="http://spaces.msn.com/Images/OutliningIndicators/None.gif" align="top" /><br /><img src="http://spaces.msn.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img style="display:none" src="http://spaces.msn.com/Images/OutliningIndicators/ContractedBlock.gif" align="top" /> </span><span style="color:#0000ff">public</span><span style="color:#000000"> Object autoWireBean(Object bean) </span><span style="border-right:#808080 1px solid;border-top:#808080 1px solid;display:none;border-left:#80808<br />
0 1px solid;border-bottom:#808080 1px solid;background-color:#ffffff"><img src="http://spaces.msn.com/Images/dot.gif" /></span><span><span style="color:#000000">{<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" /> </span><span style="color:#0000ff">return</span><span style="color:#000000"> autoWireBean(bean, autoWiringFactory);<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />    }</span></span><span style="color:#000000"><br /><img src="http://spaces.msn.com/Images/OutliningIndicators/None.gif" align="top" /></span></div>
</p>
<p>如果按照autowire配置会使用第二个buildBean方法，而prototype会使用第一个buildBean方法。
</p>
<p>5、我的测试，首先测试SpringObjectFactory的理论效率：</p>
<div><img src="http://spaces.msn.com/Images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img style="display:none" src="http://spaces.msn.com/Images/OutliningIndicators/ContractedBlock.gif" align="top" /><span style="color:#0000ff">public</span><span style="color:#000000"> </span><span style="color:#0000ff">class</span><span style="color:#000000"> testSpringObjectFactory </span><span style="color:#0000ff">extends</span><span style="color:#000000"> TestCase </span><span style="border-right:#808080 1px solid;border-top:#808080 1px solid;display:none;border-left:#808080 1px solid;border-bottom:#808080 1px solid;background-color:#ffffff"><img src="http://spaces.msn.com/Images/dot.gif" /></span><span><span style="color:#000000">{<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" /> </span><span style="color:#0000ff">protected</span><span style="color:#000000"> FileSystemXmlApplicationContext appContext;<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" /> </span><span style="color:#0000ff">protected</span><span style="color:#000000"> SpringObjectFactory sof </span><span style="color:#000000">=</span><span style="color:#000000"> </span><span style="color:#0000ff">null</span><span style="color:#000000">;<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" /> </span><span style="color:#0000ff">protected</span><span style="color:#000000"> Map map </span><span style="color:#000000">=</span><span style="color:#000000"> </span><span style="color:#0000ff">null</span><span style="color:#000000">;<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img style="display:none" src="http://spaces.msn.com/Images/OutliningIndicators/ContractedSubBlock.gif" align="top" /> </span><span style="color:#0000ff">final</span><span style="color:#000000"> String[] paths </span><span style="color:#000000">=</span><span style="color:#000000"> </span><span style="border-right:#808080 1px solid;border-top:#808080 1px solid;display:none;border-left:#808080 1px solid;border-bottom:#808080 1px solid;background-color:#ffffff"><img src="http://spaces.msn.com/Images/dot.gif" /></span><span><span style="color:#000000">{<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" /> </span><span style="color:#000000">&quot;</span><span style="color:#000000">WebRoot/WEB-INF/applicationContext.xml</span><span style="color:#000000">&quot;</span><span style="color:#000000">,<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" /> </span><span style="color:#000000">&quot;</span><span style="color:#000000">WebRoot/WEB-INF/spring-daos.xml</span><span style="color:#000000">&quot;</span><span style="color:#000000">,<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" /> </span><span style="color:#000000">&quot;</span><span style="color:#000000">WebRoot/WEB-INF/spring-actions.xml</span><span style="color:#000000">&quot;</span><span style="color:#000000"><br /><img src="http://spaces.msn.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />        }</span></span><span style="color:#000000">;<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" /><br /><img src="http://spaces.msn.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img style="display:none" src="http://spaces.msn.com/Images/OutliningIndicators/ContractedSubBlock.gif" align="top" /> </span><span style="color:#0000ff">protected</span><span style="color:#000000"> </span><span style="color:#0000ff">void</span><span style="color:#000000"> setUp() </span><span style="color:#0000ff">throws</span><span style="color:#000000"> Exception </span><span style="border-right:#808080 1px solid;border-top:#808080 1px solid;display:none;border-left:#808080 1px solid;border-bottom:#808080 1px solid;background-color:#ffffff"><img src="http://spaces.msn.com/Images/dot.gif" /></span><span><span style="color:#000000">{<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" /> </span><span style="color:#0000ff">super</span><span style="color:#000000">.setUp();<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" />        appContext </span><span style="color:#000000">=</span><span style="color:#000000"> </span><span style="color:#0000ff">new</span><span style="color:#000000"> FileSystemXmlApplicationContext(paths);<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" /><br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" />        sof </span><span style="color:#000000">=</span><span style="color:#000000"> </span><span style="color:#0000ff">new</span><span style="color:#000000"> SpringObjectFactory();<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" />        sof.setApplicationContext(appContext);<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" />        sof.setAutowireStrategy(AutowireCapableBeanFactory.AUTOWIRE_BY_NAME);<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" /><br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" />        map </span><span style="color:#000000">=</span><span style="color:#000000"> </span><span style="color:#0000ff">new</span><span style="color:#000000"> HashMap();<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />    }</span></span><span style="color:#000000"><br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" /><br /><img src="http://spaces.msn.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img style="display:none" src="http://spaces.msn.com/Images/OutliningIndicators/ContractedSubBlock.gif" align="top" /> </span><span style="color:#0000ff">public</span><span style="color:#000000"> </span><span style="color:#0000ff">void</span><span style="color:#000000"> testSpringObjectFacotyWithAutowire() </span><span style="border-right:#808080 1px solid;border-top:#808080 1px solid;display:none;border-left:#808080 1px solid;border-bottom:#808080 1px solid;background-color:#ffffff"><img src="http://spaces.msn.com/Images/dot.gif" /></span><span><span style="color:#000000">{<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" /> </span><span style="color:#0000ff">long</span><span style="color:#000000"> begin </span><span style="color:#000000">=</span><span style="color:#000000"> System.currentTimeMillis();<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" /><br /><img src="http://spaces.msn.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img style="display:none" src="http://spaces.msn.com/Images/OutliningIndicators/ContractedSubBlock.gif" align="top" /> </span><span style="color:#0000ff">try</span><span style="color:#000000"> </span><span style="border-right:#808080 1px solid;border-top:#808080 1px solid;display:none;border-left:#808080 1px solid;border-bottom:#808080 1px solid;background-color:#ffffff"><img src="http://spaces.msn.com/Images/dot.gif" /></span><span><span style="color:#000000">{<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img style="display:none" src="http://spaces.msn.com/Images/OutliningIndicators/ContractedSubBlock.gif" align="top" /> </span><span style="color:#0000ff">for</span><span style="color:#000000"> (</span><span style="color:#0000ff">int</span><span style="color:#000000"> i </span><span style="color:#000000">=</span><span style="color:#000000"> </span><span style="color:#000000">0</span><span style="color:#000000">; i </span><span style="color:#000000">&lt;</span><span style="color:#000000"> </span><spa<br />
n style="color:#000000">100000</span><span style="color:#000000">; i</span><span style="color:#000000">++</span><span style="color:#000000">) </span><span style="border-right:#808080 1px solid;border-top:#808080 1px solid;display:none;border-left:#808080 1px solid;border-bottom:#808080 1px solid;background-color:#ffffff"><img src="http://spaces.msn.com/Images/dot.gif" /></span><span><span style="color:#000000">{<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" />                sof.buildBean(</span><span style="color:#000000">&quot;</span><span style="color:#000000">com.wqh.action.XinfangNewsAction</span><span style="color:#000000">&quot;</span><span style="color:#000000">, map);<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />            }</span></span><span style="color:#000000"><br /><img src="http://spaces.msn.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img style="display:none" src="http://spaces.msn.com/Images/OutliningIndicators/ContractedSubBlock.gif" align="top" />        }</span></span><span style="color:#000000"> </span><span style="color:#0000ff">catch</span><span style="color:#000000"> (Exception e) </span><span style="border-right:#808080 1px solid;border-top:#808080 1px solid;display:none;border-left:#808080 1px solid;border-bottom:#808080 1px solid;background-color:#ffffff"><img src="http://spaces.msn.com/Images/dot.gif" /></span><span><span style="color:#000000">{<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" />            e.printStackTrace();<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />        }</span></span><span style="color:#000000"><br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" /><br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" /> </span><span style="color:#0000ff">long</span><span style="color:#000000"> end </span><span style="color:#000000">=</span><span style="color:#000000"> System.currentTimeMillis();<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" />        System.out.println(</span><span style="color:#000000">&quot;</span><span style="color:#000000">**************************Used time:</span><span style="color:#000000">&quot;</span><span style="color:#000000"> </span><span style="color:#000000">+</span><span style="color:#000000"><br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" />            (begin </span><span style="color:#000000">-</span><span style="color:#000000"> end));<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />    }</span></span><span style="color:#000000"><br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" /><br /><img src="http://spaces.msn.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img style="display:none" src="http://spaces.msn.com/Images/OutliningIndicators/ContractedSubBlock.gif" align="top" /> </span><span style="color:#0000ff">public</span><span style="color:#000000"> </span><span style="color:#0000ff">void</span><span style="color:#000000"> testSpringObjectFacotyWithPrototype() </span><span style="border-right:#808080 1px solid;border-top:#808080 1px solid;display:none;border-left:#808080 1px solid;border-bottom:#808080 1px solid;background-color:#ffffff"><img src="http://spaces.msn.com/Images/dot.gif" /></span><span><span style="color:#000000">{<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" /> </span><span style="color:#0000ff">long</span><span style="color:#000000"> begin </span><span style="color:#000000">=</span><span style="color:#000000"> System.currentTimeMillis();<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" /><br /><img src="http://spaces.msn.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img style="display:none" src="http://spaces.msn.com/Images/OutliningIndicators/ContractedSubBlock.gif" align="top" /> </span><span style="color:#0000ff">try</span><span style="color:#000000"> </span><span style="border-right:#808080 1px solid;border-top:#808080 1px solid;display:none;border-left:#808080 1px solid;border-bottom:#808080 1px solid;background-color:#ffffff"><img src="http://spaces.msn.com/Images/dot.gif" /></span><span><span style="color:#000000">{<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img style="display:none" src="http://spaces.msn.com/Images/OutliningIndicators/ContractedSubBlock.gif" align="top" /> </span><span style="color:#0000ff">for</span><span style="color:#000000"> (</span><span style="color:#0000ff">int</span><span style="color:#000000"> i </span><span style="color:#000000">=</span><span style="color:#000000"> </span><span style="color:#000000">0</span><span style="color:#000000">; i </span><span style="color:#000000">&lt;</span><span style="color:#000000"> </span><span style="color:#000000">100000</span><span style="color:#000000">; i</span><span style="color:#000000">++</span><span style="color:#000000">) </span><span style="border-right:#808080 1px solid;border-top:#808080 1px solid;display:none;border-left:#808080 1px solid;border-bottom:#808080 1px solid;background-color:#ffffff"><img src="http://spaces.msn.com/Images/dot.gif" /></span><span><span style="color:#000000">{<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" />                sof.buildBean(</span><span style="color:#000000">&quot;</span><span style="color:#000000">xinfangNewsAction</span><span style="color:#000000">&quot;</span><span style="color:#000000">, map);<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />            }</span></span><span style="color:#000000"><br /><img src="http://spaces.msn.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img style="display:none" src="http://spaces.msn.com/Images/OutliningIndicators/ContractedSubBlock.gif" align="top" />        }</span></span><span style="color:#000000"> </span><span style="color:#0000ff">catch</span><span style="color:#000000"> (Exception e) </span><span style="border-right:#808080 1px solid;border-top:#808080 1px solid;display:none;border-left:#808080 1px solid;border-bottom:#808080 1px solid;background-color:#ffffff"><img src="http://spaces.msn.com/Images/dot.gif" /></span><span><span style="color:#000000">{<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" />            e.printStackTrace();<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />        }</span></span><span style="color:#000000"><br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" /><br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" /> </span><span style="color:#0000ff">long</span><span style="color:#000000"> end </span><span style="color:#000000">=</span><span style="color:#000000"> System.currentTimeMillis();<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" />        System.out.println(</span><span style="color:#000000">&quot;</span><span style="color:#000000">**************************Used time:</span><span style="color:#000000">&quot;</span><span style="color:#000000"> </span><span style="color:#000000">+</span><span style="color:#000000"><br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" />            (begin </span><span style="color:#000000">-</span><span style="color:#000000"> end));<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/Ex<br />
pandedSubBlockEnd.gif" align="top" />    }</span></span><span style="color:#000000"><br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" /> <br /><img src="http://spaces.msn.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img style="display:none" src="http://spaces.msn.com/Images/OutliningIndicators/ContractedSubBlock.gif" align="top" /> </span><span style="color:#0000ff">public</span><span style="color:#000000"> </span><span style="color:#0000ff">void</span><span style="color:#000000"> testSpringObjectFacotyWithSpringProxyableObjectFactory() </span><span style="border-right:#808080 1px solid;border-top:#808080 1px solid;display:none;border-left:#808080 1px solid;border-bottom:#808080 1px solid;background-color:#ffffff"><img src="http://spaces.msn.com/Images/dot.gif" /></span><span><span style="color:#000000">{<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" />        sof </span><span style="color:#000000">=</span><span style="color:#000000"> </span><span style="color:#0000ff">new</span><span style="color:#000000"> SpringProxyableObjectFactory();<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" />        sof.setApplicationContext(appContext);<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" />        sof.setAutowireStrategy(AutowireCapableBeanFactory.AUTOWIRE_BY_NAME);<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" /><br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" /> </span><span style="color:#0000ff">long</span><span style="color:#000000"> begin </span><span style="color:#000000">=</span><span style="color:#000000"> System.currentTimeMillis();<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" /><br /><img src="http://spaces.msn.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img style="display:none" src="http://spaces.msn.com/Images/OutliningIndicators/ContractedSubBlock.gif" align="top" /> </span><span style="color:#0000ff">try</span><span style="color:#000000"> </span><span style="border-right:#808080 1px solid;border-top:#808080 1px solid;display:none;border-left:#808080 1px solid;border-bottom:#808080 1px solid;background-color:#ffffff"><img src="http://spaces.msn.com/Images/dot.gif" /></span><span><span style="color:#000000">{<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img style="display:none" src="http://spaces.msn.com/Images/OutliningIndicators/ContractedSubBlock.gif" align="top" /> </span><span style="color:#0000ff">for</span><span style="color:#000000"> (</span><span style="color:#0000ff">int</span><span style="color:#000000"> i </span><span style="color:#000000">=</span><span style="color:#000000"> </span><span style="color:#000000">0</span><span style="color:#000000">; i </span><span style="color:#000000">&lt;</span><span style="color:#000000"> </span><span style="color:#000000">100000</span><span style="color:#000000">; i</span><span style="color:#000000">++</span><span style="color:#000000">) </span><span style="border-right:#808080 1px solid;border-top:#808080 1px solid;display:none;border-left:#808080 1px solid;border-bottom:#808080 1px solid;background-color:#ffffff"><img src="http://spaces.msn.com/Images/dot.gif" /></span><span><span style="color:#000000">{<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" />                sof.buildBean(</span><span style="color:#000000">&quot;</span><span style="color:#000000">com.wqh.action.XinfangNewsAction</span><span style="color:#000000">&quot;</span><span style="color:#000000">, map);<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />            }</span></span><span style="color:#000000"><br /><img src="http://spaces.msn.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img style="display:none" src="http://spaces.msn.com/Images/OutliningIndicators/ContractedSubBlock.gif" align="top" />        }</span></span><span style="color:#000000"> </span><span style="color:#0000ff">catch</span><span style="color:#000000"> (Exception e) </span><span style="border-right:#808080 1px solid;border-top:#808080 1px solid;display:none;border-left:#808080 1px solid;border-bottom:#808080 1px solid;background-color:#ffffff"><img src="http://spaces.msn.com/Images/dot.gif" /></span><span><span style="color:#000000">{<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" />            e.printStackTrace();<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />        }</span></span><span style="color:#000000"><br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" /><br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" /> </span><span style="color:#0000ff">long</span><span style="color:#000000"> end </span><span style="color:#000000">=</span><span style="color:#000000"> System.currentTimeMillis();<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" />        System.out.println(</span><span style="color:#000000">&quot;</span><span style="color:#000000">**************************Used time:</span><span style="color:#000000">&quot;</span><span style="color:#000000"> </span><span style="color:#000000">+</span><span style="color:#000000"><br /><img src="http://spaces.msn.com/Images/OutliningIndicators/InBlock.gif" align="top" />            (begin </span><span style="color:#000000">-</span><span style="color:#000000"> end));<br /><img src="http://spaces.msn.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />    }</span></span><span style="color:#000000"><br /><img src="http://spaces.msn.com/Images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span></span></div>
</p>
<p>重要的是测试结果：<br /><font color="#ffa500">**************************Used time:-16875<br />**************************Used time:-80500<br />**************************Used time:-12703（使用SpringProxyableObjectFactory()这个实现）</font><br />prototype是autowire运行时间的4.77X倍，十分可观。
</p>
<p>5.5 巨大的反差，原来是我搞错了配置，发现了幕后黑手：<br />第二天，我又重新运行了5里面的测试。但是结果令人吃惊，运行了十多次，结果于昨天反差巨大，prototype方式获得的bean反而性能最快！<br />摘要两次测量结果<br /><font color="#ffa500">**************************Autowire Used time:-17578<br />**************************Prototype Used time:-7609<br />**************************Proxy Used time:-13063<br />&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />**************************Autowire Used time:-17047<br />**************************Prototype Used time:-7609<br />**************************Proxy Used time:-12797</font><br />这是为什么呢？我百思不得其解，问题出在哪里呢？后来经过跟踪svn里面的提交纪录。我发现，我在昨天测试以后，把spring配置文件中的&lt;beans default-autowire=&quot;autodetect&quot;&gt;变成了&lt;beans&gt;。也就是没有打开自动检测的autowire！<br />而后就真相大白了。我有配置上default-autowire=&quot;autodetect&quot;进行测试，结果：<br /><font color="#ffa500">**************************Autowire Used time:-16937<br />**************************Prototype Used time:-79750<br />**************************Proxy Used time:-12578</font><br />这和昨天的测试结果完全相同。也就是说我昨天写的4.77x的结果其实没有实际意义。倒是说明了Spring和Webwork集成的文章上面说的default-autowire=&quot;autodetect&quot;是很坏的实践，即失去了name的灵活性也带来了巨大的性能损失。<br />而如果使用默认的Spring autowire配置下，prototype的性能已经很好了，实际上它工作起来应该是最快的。</p>
<p>6、在实际的Web项目中的性能对比：<br />我使用了我的一个小项目，就是反复调用一个action获取一个页面，其中有一个DAO注入。使用了JMeter进行了一个测试：2个线程，间隔0.5秒，循环50次，对比“据和报告中的”Throughput，单位/sec。<br /><font color="#ffa500">使用autowire方式：Avg. 148.34（吞吐量越高越好）<br />使用prototype方式：Avg. 138.5</font><br />也就是说在实际应用中两者也是有性能差距的，后者大约是前者性能的93%。<br />具体代码我不放出了，因为意义不大，大家也可以自己动手试验一下。<br /><font color="#ff0000">补充说明：<br />首先注意这个测试是在default-autowire=&quot;autodetect&quot;下进行的。<br />测试的这个Action其实是一个空Action，它没有调用service和DAO，只是直接return SUCCESS，然后dispatcher到一个静态内容的jsp页面。我的本意是为了能够在获取Action占据的时间比例比较高的情况下分析性能区别。但是实际上却间接的夸大了在真正的实际应用中的性能差距。实际应用中如果加上service、DAO等逻辑的执行时间、模板View的渲染时间还有广域网上的网络传输时间，那么获取Action实例的时间差距可能就微乎其微了。<br /></font>
</p>
<p>7、后续：<br /><font color="#000000">经过今天的思考，可以说完全改变了想法，重新汇总一下：<br />a、在不使用default-autowire=&quot;autodetect&quot;时，Webwork 2.2的xwork中的action class使用spring的bean id配置的理论性能最好。而且，我认为如果不是为了追求配置上的简单，严重推荐关闭spring的default-autowire。<br />b、在使用default-autowire=&quot;autodetect、name、class&quot;时，需要考虑你的需求。如果不使用Spring AOP提供的功能则在Webwork 2.2的xwork中的action class使用class全名比较好。如果使用Spring AOP的功能，则还是使用bean id。<br />c、在Spring中是否使用default-autowire是个需要慎重考虑的问题。autowire如果打开，命名会受到限制（class则更不推荐，受限更大，参考相关文档），它所带来的配置简化我认为只算是小小的语法糖果，背后却是吃掉它所埋下的隐患。<br />d、6中的测试还是有些说明意义的。7%的性能差距是在使用了default-autowire的方式下得出的，其中测试的那个action其实没有执行什么逻辑，而是一个直接dispatcher到success view的action，如果有商业逻辑包装，则性能差据估计会更小。因为实际上Action的执行过程、service、DAO等逻辑的执行过程和模板View的渲染过程（网络延迟）才是耗时大户。所以，关于性能应该下的结论是，prototype与否，在实际应用中性能差距是很小的，基本可以忽略不计。我们讨论的更多是编码的更好的实践。<br />e、autowire不使用Spring AOP相对还是trade off，因为虽然配置简单一点，但是对于使用Spring的声明性事务等内容会带来麻烦。虽然XML不那么好，但是显示配置带来的好处还是很多的。<br />f、谢谢robbin的提示。关于事务我也是无奈，放弃Action事务后难道给DAO多封装一层事务？如何没有事务依然使用HibernateDAOSurpport？Acegi的确不适合Web，使用WW的Inteceptor可以实现更舒适的解决方案。<br />g、SpringProxyableObjectFactory的问题……使用上难道只能改代码？找了半天没有这个东西的介绍。看来还是需要看看代码。不过发现现在Webwork和Xwork的代码又变动了很多……<br />h、我的测试是在Webwork2.2+Spring 1.2.6环境下测试的</font></p>
<p>8、参考资源：<br />Nuts和Spring 1.2.6 效率对比<br /><a href="http://www.javaeye.com/pages/viewpage.action?pageId=786"><u><font color="#800080">http://www.javaeye.com/pages/viewpage.action?pageId=786</font></u></a><br />IoC容器的prototype性能测试 <br /><a href="http://forum.javaeye.com/viewtopic.php?t=17622&amp;postdays=0&amp;postorder=asc&amp;start=0"><u><font color="#800080">http://forum.javaeye.com/viewtopic.php?t=17622&amp;postdays=0&amp;postorder=asc&amp;start=0</font></u></a><br />JavaEye的Wiki：集成webwork和spring<br /><a href="http://www.javaeye.com/pages/viewpage.action?pageId=860"><u><font color="#800080">http://www.javaeye.com/pages/viewpage.action?pageId=860</font></u></a><br />WebWork &#8211; Spring官方Wiki<br /><a href="http://www.opensymphony.com/webwork/wikidocs/Spring.html"><u><font color="#800080">http://www.opensymphony.com/webwork/wikidocs/Spring.html</font></u></a><br />webwork2 + spring 结合的几种方法的小结<br /><a href="http://forum.javaeye.com/viewtopic.php?t=9990"><u><font color="#800080">http://forum.javaeye.com/viewtopic.php?t=9990</font></u></a><br />WebWork2.2中结合Spring:&quot;新的方式&quot; <br /><a href="http://spaces.msn.com/scud/archive/2005/09/21/13667.html"><u><font color="#0000ff">http://www.blogjava.net/scud/archive/2005/09/21/13667.html</font></u></a><br />我为什么不推荐对Action进行事务控制<br /><a href="http://www.javaeye.com/pages/viewpage.action?pageId=1205"><u><font color="#800080">http://www.javaeye.com/pages/viewpage.action?pageId=1205</font></u></a><br />我为什么不推荐使用Acegi<br /><a href="http://www.javaeye.com/pages/viewpage.action?pageId=1199"><u><font color="#800080">http://www.javaeye.com/pages/viewpage.action?pageId=1199</font></u></a></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.diamondtin.com/2006/webwork-22%e7%9a%84action%e6%98%af%e5%90%a6%e4%bd%bf%e7%94%a8spring%e7%9a%84prototype%c2%ad%e8%8e%b7%e5%8f%96%e7%9a%84%e6%80%a7%e8%83%bd%e5%af%b9%e6%af%94/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>HQL的中文问题小议</title>
		<link>http://www.diamondtin.com/2006/hql%e7%9a%84%e4%b8%ad%e6%96%87%e9%97%ae%e9%a2%98%e5%b0%8f%e8%ae%ae/</link>
		<comments>http://www.diamondtin.com/2006/hql%e7%9a%84%e4%b8%ad%e6%96%87%e9%97%ae%e9%a2%98%e5%b0%8f%e8%ae%ae/#comments</comments>
		<pubDate>Fri, 10 Feb 2006 23:51:00 +0000</pubDate>
		<dc:creator>tin</dc:creator>
				<category><![CDATA[Tech.技术]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://tin.zztin.com/2006/02/hql%e7%9a%84%e4%b8%ad%e6%96%87%e9%97%ae%e9%a2%98%e5%b0%8f%e8%ae%ae/</guid>
		<description><![CDATA[遇到了HQL的中文问题：hql的String中包括了中文以后就会出错。经过多次查询，转码（多字节、单字节）还是不能解决问题，说明某些环节压根就对多字节支持不正常。后来经过多次试验，发现... ]]></description>
			<content:encoded><![CDATA[<div>遇到了HQL的中文问题：<br />hql的String中包括了中文以后就会出错。<br />经过多次查询，转码（多字节、单字节）还是不能解决问题，说明某些环节压根就对多字节支持不正常。<br />后来经过多次试验，发现是hibernate3的hql语法解释器org.hibernate.hql.ast.ASTQueryTranslatorFactory出的问题，看到有人推荐用org.hibernate.hql.classic.ClassicQueryTranslatorFactory代替。<br />那当然是不可以的，否则相当于退回Hibernate2。<br />解决方法是不要在hql中写中文。<br />而是去使用?占位符和setParameters。<br />比如在Spring HibernateTemplate里面可以：<br />String hql = &quot;select count(*) from Xinfangnews xfnews where xfnews.shifoufabu = 0 and (xfnews.title like ? or xfnews.content like ?)&quot;;<br />Object[] parameters = { &quot;%&quot; + keyWord + &quot;%&quot;, &quot;%&quot; + keyWord + &quot;%&quot; };<br />getHibernateTemplate().find(hql, parameters);<br />如此可以运行了。<br />不过我走了点弯路：<br />本来写成&quot;like &#8216;%?%&#8217;&quot;，然后往里面插keyWord，告诉我找不到?占位符。后来才想到&#8221;中的?已经不是占位符了。<br />然后就解决了：<br /><font color="#ff0000">所以就是说不能在HQL进行语法解析之前插入中文，而需要在解析之后插入。</font><br />还要注意，我试验了一下，在Mysql5中已经指定了使用UTF-8语言的时候，不必须在Hibernate连接的地方指明使用jdbc的encoding：<br />如很多人说需要这样配置：&lt;prop key=&quot;hibernate.connection.url&quot;&gt;jdbc:mysql://10.206.21.171:3306/xinfangweb?useUnicode=true&amp;amp;characterEncoding=GBK&amp;amp;autoReconnect=true&lt;/prop&gt;<br />但实际上是用这样的配置：&lt;prop key=&quot;hibernate.connection.url&quot;&gt;jdbc:mysql://10.206.21.171:3306/xinfangweb&lt;/prop&gt;<br />也是可以工作的，当然还是写上比较稳妥。</div>
]]></content:encoded>
			<wfw:commentRss>http://www.diamondtin.com/2006/hql%e7%9a%84%e4%b8%ad%e6%96%87%e9%97%ae%e9%a2%98%e5%b0%8f%e8%ae%ae/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Generic Hibernate DAO试验</title>
		<link>http://www.diamondtin.com/2006/generic-hibernate-dao%e8%af%95%e9%aa%8c/</link>
		<comments>http://www.diamondtin.com/2006/generic-hibernate-dao%e8%af%95%e9%aa%8c/#comments</comments>
		<pubDate>Mon, 30 Jan 2006 15:21:00 +0000</pubDate>
		<dc:creator>tin</dc:creator>
				<category><![CDATA[Tech.技术]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://tin.zztin.com/2006/01/generic-hibernate-dao%e8%af%95%e9%aa%8c/</guid>
		<description><![CDATA[我们项目升级Weblogic 9.1，所以可以用JDK5的东西了。 先试验一下GenericHibernateDAO，作为一个DAO的基类，实现了一些简单的方法，方便子DAO封装。 使用方法类似：  public class HibernateTownDAO extends Gener... ]]></description>
			<content:encoded><![CDATA[<div>
<p><a></a>我们项目升级Weblogic 9.1，所以可以用JDK5的东西了。 <br />先试验一下GenericHibernateDAO，作为一个DAO的基类，实现了一些简单的方法，方便子DAO封装。 
</p>
<p>使用方法类似： <br /> public class HibernateTownDAO extends GenericHibernateDAO&lt;Town&gt; <br />implements ITownDAO{ <br /> { <br />        public HibernateTownDAO() { super(Town.class); } } <br /> } 
</p>
<p>这个东西基本上是从EclipseWork里面来的……</p>
</div>
<div>IGenericDAO大家可以用Eclipse的抽出Interface……</div>
<div> </div>
<div>参考这几篇： <br /><a href="http://blog.hibernate.org/cgi-bin/blosxom.cgi/Christian Bauer/java/..."><u><font color="#551a8b">http://blog.hibernate.org/cgi-bin/blosxom.cgi/Christian%20Bauer/java/&#8230;</font></u></a> </div>
<div><a href="http://www.hibernate.org/328.html">http://www.hibernate.org/328.html</a><br /><a href="http://www.matrix.org.cn/thread.shtml?topicId=34892&amp;forumId=26"><u><font color="#551a8b">http://www.matrix.org.cn/thread.shtml?topicId=34892&amp;forumId=26</font></u></a> <br /><a href="http://www.javaworld.com.tw/roller/page/ingramchen?entry=2005_10_20_Generic_Hibernate_DAO"><u><font color="#551a8b">http://www.javaworld.com.tw/roller/page/ingramchen?entry=2005_10_20_G&#8230;</font></u></a> <br />还参考了limo的Blog和limo与徐x赠送的部分代码，thx</div>
<div> </div>
<div>代码如下：</div>
<div>package com.goldnet.dao.hibernateDAO;</div>
<div>import org.apache.commons.beanutils.BeanUtils;</div>
<div>import org.hibernate.Criteria;<br />import org.hibernate.Session;</div>
<div>import org.hibernate.criterion.Order;<br />import org.hibernate.criterion.Restrictions;</div>
<div>import org.springframework.orm.hibernate3.HibernateCallback;<br />import org.springframework.orm.hibernate3.support.HibernateDaoSupport;</div>
<div>import com.goldnet.dao.IDAO.IGenericDAO;</div>
<div>import java.io.Serializable;</div>
<div>import java.util.Collection;<br />import java.util.Iterator;</div>
<div>/**<br /> * @author TIN<br /> * @comment <br /> * @param &lt;E&gt;<br /> * <br /> * 范型的抽象HibernateDAO，继承于Spring提供的HibernateDaoSupport，该DAO封装了大部分的Domain对象访问方法。<br /> * 使用范型的目的在于可以显示的规定传入/返回的类型。直接的目的在于减少使用DAO时的强行类型转换。<br /> * <br /> * TODO:getTopsByProperty(int, String)方法<br /> * TODO:execute(HibernateCallback callback)方法?<br /> * TODO:execute(HQL hql)方法?<br /> * TODO:execute(SQL sql)方法?<br /> * TODO:findByCriteria(Criterion&#8230; criterion)方法?<br /> * TODO:getAOfflineListCopy(List list)方法?获取一个离线list，可以在部分地方获取session无关的List，避免使用OpenSessionInView<br /> * TODO:String getPersistentObjectName()方法?<br /> * TODO:使用变长参数修改一些传入Properties和Value的地方，扩展一些方法<br /> * TODO:研究一下DetachedCreteria<br /> * <br /> */</div>
<div>public class GenericHibernateDAO&lt;E&gt; extends HibernateDaoSupport implements IGenericDAO&lt;E&gt; {</div>
<div> private static final long serialVersionUID = 1312483055446936306L;</div>
<div> private final Class clazz;</div>
<div> public GenericHibernateDAO(final Class&lt;E&gt; clazz) {<br />  this.clazz = clazz;<br /> }</div>
<div> /**<br />  * @see com.goldnet.dao.IDAO.IGenericDAO#saveOrUpdate(java.lang.Object,<br />  *      java.io.Serializable)<br />  * @deprecated<br />  * <br />  * @param newObject 持久化的类<br />  * @param id 持久化类的标识id<br />  * <br />  * 存储一个Entity，由于它的实现方式与HibernateDAO的saveOrUpdate雷同，都检测了id是否存在，但却多一个参数<br />  * 所以暂时部推荐，但是该实现对saveOrUpdate机理表现比较清晰，所以没有删除<br />  */<br /> public void saveOrUpdate(E newObject, Serializable id) {<br />  if ((id != null) &amp;&amp; !getHibernateTemplate().contains(id)) {<br />   E dbObject = this.loadById(id);</div>
<div>   try {<br />    BeanUtils.copyProperties(dbObject, newObject);<br />   } catch (Exception e) {<br />    logger.error(e, e);<br />   }</div>
<div>   newObject = dbObject;<br />  }</div>
<div>  if (logger.isDebugEnabled()) {<br />   logger.debug(&quot;[saveOrUpdate with id Parameter] Entity&lt;&quot;<br />     + this.clazz.getName() + &quot;&gt;.&quot;);<br />   logger<br />     .debug(&quot;this method is deprecated! Please use saveOrUpdate(Object) instead.&quot;);<br />  }</div>
<div>  getHibernateTemplate().saveOrUpdate(newObject);<br /> }</div>
<div> /**<br />  * @see com.goldnet.dao.IDAO.IGenericDAO#saveOrUpdate(java.lang.Object)<br />  * <br />  * @param newObject 持久化的类<br />  * <br />  * 持久化一个Entity，一般用在不知持久化的是VO或PO时<br />  */<br /> public void saveOrUpdate(E newObject) {<br />  if (logger.isDebugEnabled()) {<br />   logger<br />     .debug(&quot;[saveOrUpdate] Entity&lt;&quot; + this.clazz.getName()<br />       + &quot;&gt;.&quot;);<br />  }</div>
<div>  getHibernateTemplate().saveOrUpdate(newObject);<br /> }</div>
<div> /**<br />  * @see com.goldnet.dao.IDAO.IGenericDAO#save(java.lang.Object)<br />  *<br />  * @param object 持久化的类<br />  * <br />  * 持久化一个Entity，用在持久化PO时<br />  */<br /> public void save(E object) {<br />  if (logger.isDebugEnabled()) {<br />   logger.debug(&quot;[save] Entity&lt;&quot; + this.clazz.getName() + &quot;&gt;.&quot;);<br />  }</div>
<div>  getHibernateTemplate().save(object);<br /> }</div>
<div> /**<br />  * @see com.goldnet.dao.IDAO.IGenericDAO#update(java.lang.Object)<br />  *<br />  * @param object 持久化的类<br />  * <br />  * 持久化一个Entity，符合jsr-220规范，用在持久化一个VO的Entity（但是它已经在库中存在）时，持久化它并返回它的PO<br />  */<br /> @SuppressWarnings(&quot;unchecked&quot;)<br /> public E merge(E object) {<br />  if (logger.isDebugEnabled()) {<br />   logger.debug(&quot;[merge] Entity&lt;&quot; + this.clazz.getName() + &quot;&gt;.&quot;);<br />  }</div>
<div>  return (E) getHibernateTemplate().merge(object);<br /> }</div>
<div> /**<br />  * @see com.goldnet.dao.IDAO.IGenericDAO#isEntityWithPropertyOfValueExist(java.lang.String,<br />  *      java.lang.Object)<br />  *<br />  * @param propertyName Entity属性的名称<br />  * @param value Entity属性的值<br />  * @return<br />  * <br />  * 具有propertyName的Entity是否存在？如果true则存在，如果false则不存在。<br />  */<br /> public boolean isEntityWithPropertyOfValueExist(String propertyName,<br />   Object value) {<br />  if (logger.isDebugEnabled()) {<br />   logger.debug(&quot;[isEntityWithPropertyOfValueExist] Entity&lt;&quot;<br />     + this.clazz.getName() + &quot;&gt;, propName=&quot; + propertyName<br />     + &quot;, value=&quot; + value + &quot;.&quot;);<br />  }</div>
<div>  if (getHibernateTemplate().find(<br />    &quot;from &quot; + this.clazz.getName() + &quot; table where table.&quot;<br />      + propertyName + &quot;=?&quot;, value).size() &gt; 0) {<br />   return true;<br />  }</div>
<div>  return false;<br /> }</div>
<div> /**<br />  * @see com.goldnet.dao.IDAO.IGenericDAO#isEntityIdExist(java.io.Serializable)<br />  *<br />  * @param id Entity的标识id<br />  * @return<br />  * <br />  * 具有该id的Entity是否存在？如果true则存在，如果false则不存在。用来检测一个VO是否已经有对应的PO存在<br />  */<br /> public boolean isEntityIdExist(Serializable id) {<br />  if (logger.isDebugEnabled()) {<br />   logger.debug(&quot;[isEntityIdExist] Entity&lt;&quot; + this.clazz.getName()<br />     + &quot;&gt; with id=&quot; + id + &quot;.&quot;);<br />  }</div>
<div>  return getHibernateTemplate().contains(id);<br /> }</div>
<div> /**<br />  * @see com.goldnet.dao.IDAO.IGenericDAO#delete(java.lang.Object)<br />  *<br />  * @param object Entity对象<br />  * <br />  * 删除一个Entity<br />  */<br /> public void delete(E object) {<br />  if (logger.isDebugEnabled()) {<br />   logger.debug(&quot;[delete] Entity&lt;&quot; + this.clazz.getName() + &quot;&gt;.&quot;);<br />  }</div>
<div>  getHibernateTemplate().delete(object);<br /> }</div>
<div> /**<br />  * @see com.goldnet.dao.IDAO.IGenericDAO#deleteById(java.io.Serializable)<br />  *<br />  * @param id Entity的标识id<br />  * <br />  * 根据Entity的标识id删除一个Entity<br />  */<br /> public void deleteById(Serializable id) {<br />  if (logger.isDebugEnabled()) {<br />   logger.debug(&quot;[deleteById] Entity&lt;&quot; + this.clazz.getName()<br />     + &quot;&gt; with id=&quot; + id + &quot;.&quot;);<br />  }</div>
<div>  getHibernateTemplate().delete(<br />    getHibernateTemplate().get(this.clazz, id));<br /> }</div>
<div> /**<br />  * @see com.goldnet.dao.IDAO.IGenericDAO#deleteByProperty(java.lang.String,<br />  *      java.lang.Object)<br />  *<br />  * @param propertyName Entity属性的名称<br />  * @param value Entity属性的值<br />  * <br />  * 删除一组具有相同propertyName属性值（value）的Entity，用来批量删除<br />  */<br /> @SuppressWarnings(&quot;unchecked&quot;)<br /> public void deleteByProperty(String propertyName, Object value) {<br />  Iterator&lt;E&gt; result = listByProperty(propertyName, value).iterator();</div>
<div>  while (result.hasNext()) {<br />   delete(result.next());<br />  }</div>
<div>  if (logger.isDebugEnabled()) {<br />   logger.debug(&quot;[deleteByProperty] Entity&lt;&quot; + this.clazz.getName()<br />     + &quot;&gt;, propName=&quot; + propertyName + &quot;, value=&quot; + value + &quot;.&quot;);<br />  }<br /> }</div>
<div> /**<br />  * @see com.goldnet.dao.IDAO.IGenericDAO#loadById(java.io.Serializable)<br />  *<br />  * @param id Entity的标识id<br />  * @return<br />  * <br />  * 根据Entity的标识id获取一个Entity的PO<br />  */<br /> @SuppressWarnings(&quot;unchecked&quot;)<br /> public E loadById(Serializable id) {<br />  if (logger.isDebugEnabled()) {<br />   logger.debug(&quot;[loadById] Entity&lt;&quot; + this.clazz.getName() + &quot;&gt;, id=&quot;<br />     + id + &quot;.&quot;);<br />  }</div>
<div>  return (E) getHibernateTemplate().get(this.clazz, id);<br /> }</div>
<div> /**<br />  * @see com.goldnet.dao.IDAO.IGenericDAO#loadByProperty(java.lang.String,<br />  *      java.lang.Object)<br />  *<br />  * @param propertyName Entity属性的名称<br />  * @param value Entity属性的值<br />  * @return<br />  * <br />  * TODO:判断结果是否是unique？<br />  * 获取一个具有相同propertyName属性值（value）的Entity，注意改属性必须是unique的，否则改方法将没有意义<br />  */<br /> @SuppressWarnings(&quot;unchecked&quot;)<br /> public E loadByProperty(String propertyName, Object value) {<br />  Collection result = getHibernateTemplate().find(<br />    &quot;from &quot; + this.clazz.getName() + &quot; table where table.&quot;<br />      + propertyName + &quot;=?&quot;, value);<br />  Iterator&lt;E&gt; it = result.iterator();</div>
<div>  if (logger.isDebugEnabled()) {<br />   logger.debug(&quot;[loadByProperty] Entity&lt;&quot; + this.clazz.getName()<br />     + &quot;&gt;, propName=&quot; + propertyName + &quot;, value=&quot; + value + &quot;.&quot;);<br />  }</div>
<div>  if (it.hasNext()) {<br />   return it.next();<br />  } else {<br />   return null;<br />  }<br /> }</div>
<div> /**<br />  * @see com.goldnet.dao.IDAO.IGenericDAO#loadEntitySum()<br />  *<br />  * @return<br />  * <br />  * 获取一种Entity的总数<br />  */<br /> public int loadEntitySum() {<br />  if (logger.isDebugEnabled()) {<br />   logger.debug(&quot;[loadEntitySum] Entity&lt;&quot; + this.clazz.getName()<br />     + &quot;&gt;.&quot;);<br />  }</div>
<div>  return ((Integer) getHibernateTemplate().find(<br />    &quot;select count(*) from &quot; + this.clazz.getName()).iterator()<br />    .next()).intValue();<br /> }</div>
<div> /**<br />  * @see com.goldnet.dao.IDAO.IGenericDAO#loadEntitySumByProperty(java.lang.String,<br />  *      java.lang.Object)<br />  *<br />  * @param propertyName Entity属性的名称<br />  * @param value Entity属性的值<br />  * @return<br />  * <br />  * 获取一组具有相同propertyName属性值（value）的Entity的个数<br />  */<br /> public int loadEntitySumByProperty(String propertyName, Object value) {<br />  if (logger.isDebugEnabled()) {<br />   logger.debug(&quot;[loadEntitySum] Entity&lt;&quot; + this.clazz.getName()<br />     + &quot;&gt;, propName=&quot; + propertyName + &quot;, value=&quot; + value + &quot;.&quot;);<br />  }</div>
<div>  return ((Integer) getHibernateTemplate().find(<br />    &quot;select count(*) from &quot; + this.clazz.getName()<br />      + &quot; table where table.&quot; + propertyName + &quot;=?&quot;, value)<br />    .iterator().next()).intValue();<br /> }</div>
<div> /**<br />  * @see com.goldnet.dao.IDAO.IGenericDAO#loadLastEntity()<br />  *<br />  * @param orderByPropertyName 用来排序的属性的名称<br />  * @return<br />  * <br />  * TODO:和获取Tops是否重复？<br />  * 返回最后一个Entity，用来获取如最新加入的Entity这样的方法<br />  * 注意改方法实际上返回的是降序排列的list中的第一个！<br />  */<br /> @SuppressWarnings(&quot;unchecked&quot;)<br /> public E loadLastEntity(final String orderByPropertyName) {</div>
<div>  if (logger.isDebugEnabled()) {<br />   logger.debug(&quot;[listLastEntity] Entity&lt;&quot; + this.clazz.getName()<br />     + &quot;&gt; and orderByPropertyName=&quot; + orderByPropertyName<br />     + &quot;, Note:it&#8217;s DESC.&quot;);<br />  }</div>
<div>  return (E) getHibernateTemplate().execute(new HibernateCallback() {<br />   public Object doInHibernate(Session session) {<br />    Criteria criteria = session.createCriteria(clazz);<br />    criteria.addOrder(Order.desc(orderByPropertyName));<br />    releaseSession(session);</div>
<div>    try {<br />     return criteria.list().iterator().next();<br />    } catch (Exception e) {<br />     logger<br />       .warn(&quot;[listLastEntity] faild! the entity list is empty!&quot;);</div>
<div>     return null;<br />    }<br />   }<br />  });<br /> }</div>
<div> /**<br />  * @see com.goldnet.dao.IDAO.IGenericDAO#listAll()<br />  *<br />  * @return<br />  * <br />  * 获取所有的Entity的list<br />  */<br /> @SuppressWarnings(&quot;unchecked&quot;)<br /> public Collection&lt;E&gt; listAll() {<br />  if (logger.isDebugEnabled()) {<br />   logger.debug(&quot;[listAll] Entity&lt;&quot; + this.clazz.getName() + &quot;&gt;.&quot;);<br />  }</div>
<div>  return getHibernateTemplate().find(&quot;from &quot; + this.clazz.getName());<br /> }</div>
<div> /**<br />  * @see com.goldnet.dao.IDAO.IGenericDAO#listAllOrderBy(boolean, java.lang.String)<br />  *<br />  * @param isAsc 是否使用升序<br />  * @param orderByPropertyName 排序的属性<br />  * @return<br />  * <br />  * 获取所有的Entity的list，结果按照orderByPropertyName属性升序/降序（isAsc true则升序，否则降序）<br />  */<br /> @SuppressWarnings(&quot;unchecked&quot;)<br /> public Collection&lt;E&gt; listAllOrderBy(final boolean isAsc,<br />   final String orderByPropertyName) {<br />  final Class clazztemp = this.clazz;</div>
<div>  if (logger.isDebugEnabled()) {<br />   logger.debug(&quot;[listAllOrderBy] Entity&lt;&quot; + this.clazz.getName()<br />     + &quot;&gt;, isAsc?=&quot; + isAsc + &quot;, orderByPropertyName=&quot;<br />     + orderByPropertyName + &quot;.&quot;);<br />  }</div>
<div>  return (Collection&lt;E&gt;) getHibernateTemplate().execute(<br />    new HibernateCallback() {<br />     public Object doInHibernate(Session session) {<br />      Criteria criteria = session.createCriteria(clazztemp);<br />      criteria.addOrder(isAsc ? Order<br />        .asc(orderByPropertyName) : Order<br />        .desc(orderByPropertyName));<br />      releaseSession(session);</div>
<div>      return criteria.list();<br />     }<br />    });<br /> }</div>
<div> /**<br />  * @see com.goldnet.dao.IDAO.IGenericDAO#listByPage(int, int)<br />  *<br />  * @param pagesize 页的大小，每页最多Entity个数<br />  * @param pageno 页码<br />  * @return<br />  * <br />  * 获取一页的Entity的list，该页是所有Entity中的第pageno页，每页最多有pagesize个Entity，分页显示Entity时使用<br />  */<br /> @SuppressWarnings(&quot;unchecked&quot;)<br /> public Collection&lt;E&gt; listByPage(final int pagesize, final int pageno) {<br />  final Class clazztemp = this.clazz;</div>
<div>  if (logger.isDebugEnabled()) {<br />   logger.debug(&quot;[listByPage] Entity&lt;&quot; + this.clazz.getName()<br />     + &quot;&gt;, pagesize=&quot; + pagesize + &quot;, pageno=&quot; + pageno + &quot;.&quot;);<br />  }</div>
<div>  return (Collection&lt;E&gt;) getHibernateTemplate().execute(<br />    new HibernateCallback() {<br />     public Object doInHibernate(Session session) {<br />      Criteria criteria = session.createCriteria(clazztemp);<br />      criteria.setFirstResult(pagesize * pageno);<br />      criteria.setMaxResults(pagesize);<br />      releaseSession(session);</div>
<div>      return criteria.list();<br />     }<br />    });<br /> }</div>
<div> /**<br />  * @see com.goldnet.dao.IDAO.IGenericDAO#listByPageOrderBy(int, int, boolean,<br />  *      java.lang.String)<br />  *      <br />  * @param pagesize 页的大小，每页最多Entity个数<br />  * @param pageno 页码<br />  * @param isAsc 是否使用升序<br />  * @param orderByPropertyName 排序的属性<br />  * @return<br />  * <br />  * 获取一页的Entity的list，该页内容按照orderByPropertyName属性升序/降序（isAsc true则升序，否则降序），该页是所有Entity中的第pageno页，每页最多有pagesize个Entity<br />  * 分页显示排序过的Entity时使用<br />  */<br /> @SuppressWarnings(&quot;unchecked&quot;)<br /> public Collection&lt;E&gt; listByPageOrderBy(final int pagesize,<br />   final int pageno, final boolean isAsc,<br />   final String orderByPropertyName) {<br />  final Class clazztemp = this.clazz;</div>
<div>  if (logger.isDebugEnabled()) {<br />   logger.debug(&quot;[listByPageOrderBy] Entity&lt;&quot; + this.clazz.getName()<br />     + &quot;&gt;, pagesize=&quot; + pagesize + &quot;, isAsc?=&quot; + isAsc<br />     + &quot;, orderByPropertyName&quot; + orderByPropertyName<br />     + &quot;, pageno=&quot; + pageno + &quot;.&quot;);<br />  }</div>
<div>  return (Collection&lt;E&gt;) getHibernateTemplate().execute(<br />    new HibernateCallback() {<br />     public Object doInHibernate(Session session) {<br />      Criteria criteria = session.createCriteria(clazztemp);<br />      criteria<br />        .setFirstResult(pagesize * pageno)<br />        .setMaxResults(pagesize)<br />        .addOrder(<br />          isAsc ? Order.asc(orderByPropertyName)<br />            : Order<br />              .desc(orderByPropertyName));<br />      releaseSession(session);</div>
<div>      return criteria.list();<br />     }<br />    });<br /> }</div>
<div> /**<br />  * @see com.goldnet.dao.IDAO.IGenericDAO#listByProperty(java.lang.String,<br />  *      java.lang.Object)<br />  *      <br />  * @param propertyName Entity属性的名称<br />  * @param value Entity属性的值<br />  * @return<br />  * <br />  * 获取一组具有相同propertyName属性值（value）的Entity的列表list，返回一组符合条件的Entity<br />  */<br /> @SuppressWarnings(&quot;unchecked&quot;)<br /> public Collection&lt;E&gt; listByProperty(String propertyName, Object value) {<br />  if (logger.isDebugEnabled()) {<br />   logger.debug(&quot;[listByProperty] Entity&lt;&quot; + this.clazz.getName()<br />     + &quot;&gt;, propertyName&quot; + propertyName + &quot;, value=&quot; + value<br />     + &quot;.&quot;);<br />  }</div>
<div>  return getHibernateTemplate().find(<br />    &quot;from &quot; + this.clazz.getName() + &quot; table where table.&quot;<br />      + propertyName + &quot;=?&quot;, value);<br /> }</div>
<div> /**<br />  * @see com.goldnet.dao.IDAO.IGenericDAO#listByPageByProperty(java.lang.String,<br />  *      java.lang.Object, boolean, java.lang.String)<br />  *      <br />  * @param pagesize 页的大小，每页最多Entity个数<br />  * @param pageno 页码<br />  * @param listByPropertyName Entity属性的名称<br />  * @param value Entity属性的值<br />  * @return<br />  * <br />  * 获取一页的Entity的list，该页内容为具有相同propertyName属性值（value）的Entity，该页是所有符合条件Entity中的第pageno页，每页最多有pagesize个Entity<br />  * 分页显示匹配某一属性值的Entity时使用<br />  */<br /> @SuppressWarnings(&quot;unchecked&quot;)<br /> public Collection&lt;E&gt; listByPageByProperty(final int pageno,<br />   final int pagesize, final String listByPropertyName,<br />   final Object value) {<br />  final Class clazztemp = this.clazz;</div>
<div>  if (logger.isDebugEnabled()) {<br />   logger.debug(&quot;[listByPageByProperty] Entity&lt;&quot;<br />     + this.clazz.getName() + &quot;&gt;, pageno=&quot; + pageno<br />     + &quot;, pagesize=&quot; + pagesize + &quot;, listByPropertyName=&quot;<br />     + listByPropertyName + &quot;, value&quot; + value + &quot;.&quot;);<br />  }</div>
<div>  return (Collection&lt;E&gt;) getHibernateTemplate().execute(<br />    new HibernateCallback() {<br />     public Object doInHibernate(Session session) {<br />      Criteria criteria = session.createCriteria(clazztemp);<br />      criteria.setFirstResult(pagesize * pageno)<br />        .setMaxResults(pagesize).add(<br />          Restrictions.eq(listByPropertyName,<br />            value));<br />      releaseSession(session);</div>
<div>      return criteria.list();<br />     }<br />    });<br /> }</div>
<div> /**<br />  * @see com.goldnet.dao.IDAO.IGenericDAO#listByPropertyOrderBy(java.lang.String,<br />  *      java.lang.Object, boolean, java.lang.String)<br />  *      <br />  * @param listByPropertyName Entity属性的名称<br />  * @param value Entity属性的值<br />  * @param isAsc 是否使用升序<br />  * @param orderByPropertyName 排序的属性<br />  * @return<br />  * <br />  * 获取所有的具有匹配propertyName属性值（value）的Entity的list，按照orderByPropertyName属性升序/降序（isAsc true则升序，否则降序）<br />  * 显示匹配属性值的列表且要求排序时使用<br />  */<br /> @SuppressWarnings(&quot;unchecked&quot;)<br /> public Collection&lt;E&gt; listByPropertyOrderBy(final String listByPropertyName,<br />   final Object value, final boolean isAsc,<br />   final String orderByPropertyName) {<br />  final Class clazztemp = this.clazz;</div>
<div>  if (logger.isDebugEnabled()) {<br />   logger.debug(&quot;[listByPropertyOrderBy] Entity&lt;&quot;<br />     + this.clazz.getName() + &quot;&gt;, listByPropertyName=&quot;<br />     + listByPropertyName + &quot;, value&quot; + value + &quot;, isAsc?=&quot;<br />     + isAsc + &quot;, orderByPropertyName=&quot; + orderByPropertyName<br />     + &quot;.&quot;);<br />  }</div>
<div>  return (Collection&lt;E&gt;) getHibernateTemplate().execute(<br />    new HibernateCallback() {<br />     public Object doInHibernate(Session session) {<br />      Criteria criteria = session.createCriteria(clazztemp);<br />      criteria<br />        .add(Restrictions.eq(listByPropertyName, value))<br />        .addOrder(<br />          isAsc ? Order.asc(orderByPropertyName)<br />            : Order<br />              .desc(orderByPropertyName));<br />      releaseSession(session);</div>
<div>      return criteria.list();<br />     }<br />    });<br /> }</div>
<div> /**<br />  * @see com.goldnet.dao.IDAO.IGenericDAO#listByPageByPropertyOrderBy(int, int,<br />  *      java.lang.String, java.lang.Object, boolean, java.lang.String)<br />  *      <br />  * @param pagesize 页的大小，每页最多Entity个数<br />  * @param pageno 页码<br />  * @param listByPropertyName Entity属性的名称<br />  * @param value Entity属性的值<br />  * @param isAsc 是否使用升序<br />  * @param orderByPropertyName 排序的属性<br />  * @return<br />  * <br />  * 获取一页的Entity的list，该页内容为具有相同propertyName属性值（value）的Entity，该页内容按照orderByPropertyName属性升序/降序（isAsc true则升序，否则降序），<br />  * 该页是所有符合条件Entity中的第pageno页，每页最多有pagesize个Entity<br />  * 分页显示匹配某一属性值的Entity时使用<br />  */<br /> @SuppressWarnings(&quot;unchecked&quot;)<br /> public Collection&lt;E&gt; listByPageByPropertyOrderBy(final int pagesize,<br />   final int pageno, final String listByPropertyName,<br />   final Object value, final boolean isAsc,<br />   final String orderByPropertyName) {<br />  final Class clazztemp = this.clazz;</div>
<div>  if (logger.isDebugEnabled()) {<br />   logger.debug(&quot;[listByPageByPropertyOrderBy] Entity&lt;&quot;<br />     + this.clazz.getName() + &quot;&gt;, pageno=&quot; + pageno<br />     + &quot;, pagesize=&quot; + pagesize + &quot;, listByPropertyName=&quot;<br />     + listByPropertyName + &quot;, value&quot; + value + &quot;, isAsc?=&quot;<br />     + isAsc + &quot;, orderByPropertyName=&quot; + orderByPropertyName<br />     + &quot;.&quot;);<br />  }</div>
<div>  return (Collection&lt;E&gt;) getHibernateTemplate().execute(<br />    new HibernateCallback() {<br />     public Object doInHibernate(Session session) {<br />      Criteria criteria = session.createCriteria(clazztemp);<br />      criteria<br />        .setFirstResult(pagesize * pageno)<br />        .setMaxResults(pagesize)<br />        .add(Restrictions.eq(listByPropertyName, value))<br />        .addOrder(<br />          isAsc ? Order.asc(orderByPropertyName)<br />            : Order<br />              .desc(orderByPropertyName));<br />      releaseSession(session);</div>
<div>      return criteria.list();<br />     }<br />    });<br /> }<br />}</div>
]]></content:encoded>
			<wfw:commentRss>http://www.diamondtin.com/2006/generic-hibernate-dao%e8%af%95%e9%aa%8c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Webwork 2.2的form tag在使用jsp view时onsubmit不工作</title>
		<link>http://www.diamondtin.com/2006/webwork-22%e7%9a%84form-tag%e5%9c%a8%e4%bd%bf%e7%94%a8jsp-view%e6%97%b6onsubmit%e4%b8%8d%e5%b7%a5%e4%bd%9c/</link>
		<comments>http://www.diamondtin.com/2006/webwork-22%e7%9a%84form-tag%e5%9c%a8%e4%bd%bf%e7%94%a8jsp-view%e6%97%b6onsubmit%e4%b8%8d%e5%b7%a5%e4%bd%9c/#comments</comments>
		<pubDate>Thu, 19 Jan 2006 21:24:00 +0000</pubDate>
		<dc:creator>tin</dc:creator>
				<category><![CDATA[Tech.技术]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://tin.zztin.com/2006/01/webwork-22%e7%9a%84form-tag%e5%9c%a8%e4%bd%bf%e7%94%a8jsp-view%e6%97%b6onsubmit%e4%b8%8d%e5%b7%a5%e4%bd%9c/</guid>
		<description><![CDATA[起因是使用Michael Chen的JSValidation，需要手动写onsubmit，结果居然发现webwork 2.2的正式版本里面的form tag里面的onsubmit在使用jsp view的时候没法输出，苦恼于此。暂时通过修改模板修改了一下，但是... ]]></description>
			<content:encoded><![CDATA[<div></div>
<p><span>起因是使用Michael Chen的JSValidation，需要手动写onsubmit，结果居然发现webwork 2.2的正式版本里面的form tag里面的onsubmit在使用jsp view的时候没法输出，苦恼于此。暂时通过修改模板修改了一下，但是如此很不爽。所以就想测试一下到底是不是我们项目的问题还是ww 2.2的问题。但一直太忙，今天才抽出功夫测。以下是测试纪录，希望知道缘由的朋友帮我指点一下。 </p>
<p>测试使用Webwork发行包中演示的starter项目，其中使用了ftl模板作为表现层，我按它的方式制造了newPerson.jsp。 <br />下面是两个页面的源文件： <br />newPerson.jsp <br /></span></p>
<table cellspacing="1" cellpadding="3" width="90%" align="center" border="0">
<tbody>
<tr>
<td><span><b>java代码: </b></span>
</td>
</tr>
<tr>
<td>
<div style="font-family:'Courier New', Courier, monospace">&lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=utf-<font face="Courier New"><span style="color:#000000">8</span>&quot;%&gt; <br />&lt;%@ taglib uri=&quot;webwork&quot; prefix=&quot;ww&quot;%&gt; <br />&lt;html&gt; <br />&lt;head&gt; <br />    &lt;title&gt;<span style="font-weight:bold;color:#990066">New</span> Person&lt;/title&gt; <br />    &lt;link href=&quot;&lt;ww:url value=&quot;/webwork/jscalendar/calendar-blue.<span style="color:#000000">css</span>&quot; /&gt;&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; media=&quot;all&quot;/&gt; <br />&lt;/head&gt; </p>
<p>&lt;body&gt; <br />&lt;ww:form action=&quot;newPerson&quot; onsubmit=&quot;dosomething&quot; validate=&quot;<span style="font-weight:bold;color:#990066">false</span>&quot;&gt; <br />    &lt;ww:textfield label=&quot;<span style="color:#aaaadd">Name</span>&quot; name=&quot;person.<span style="color:#000000">name</span>&quot;/&gt; <br />    &lt;ww:datepicker  name=&quot;date&quot; id=&quot;ecoInf.<span style="color:#000000">turninDate</span>&quot; template=&quot;datepicker.<span style="color:#000000">ftl</span>&quot; language=&quot;en&quot; format=&quot;%Y-%m-%d&quot; label=&quot;<span style="color:#aaaadd">Time</span></font>&quot;/&gt; <br />    &lt;ww:submit value=&quot;Create person&quot;/&gt; <br />&lt;/ww:form&gt; <br />&lt;/body&gt; <br />&lt;/html&gt;</div>
<p></td>
</tr>
</tbody>
</table>
<p><span>newPerson.ftl <br /></span></p>
<table cellspacing="1" cellpadding="3" width="90%" align="center" border="0">
<tbody>
<tr>
<td><span><b>java代码: </b></span>
</td>
</tr>
<tr>
<td>
<div style="font-family:'Courier New', Courier, monospace">&lt;html&gt; <br />&lt;head&gt; <br />    &lt;title&gt;<font face="Courier New"><span style="font-weight:bold;color:#990066">New</span> Person&lt;/title&gt; <br />    &lt;link href=&quot;&lt;@ww.<span style="color:#000000">url</span> value=&quot;/webwork/jscalendar/calendar-blue.<span style="color:#000000">css</span>&quot; /&gt;&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; media=&quot;all&quot;/&gt; <br />&lt;/head&gt; </p>
<p>&lt;body&gt; <br />&lt;@ww.<span style="color:#000000">form</span> action=&quot;newPerson&quot; onsubmit=&quot;dosomething&quot;&gt; <br />    &lt;@ww.<span style="color:#000000">textfield</span> label=&quot;<span style="color:#aaaadd">Name</span>&quot; name=&quot;person.<span style="color:#000000">name</span>&quot;/&gt; <br />    &lt;@ww.<span style="color:#000000">datepicker</span>  name=&quot;date&quot; id=&quot;ecoInf.<span style="color:#000000">turninDate</span>&quot; template=&quot;datepicker.<span style="color:#000000">ftl</span>&quot; language=&quot;en&quot; format=&quot;%Y-%m-%d&quot; label=&quot;<span style="color:#aaaadd">Time</span>&quot;/&gt; <br />    &lt;@ww.<span style="color:#000000">submit</span> value=&quot;Create person&quot;/&gt; <br />&lt;/@ww.<span style="color:#000000">form</span></font>&gt; <br />&lt;/body&gt; <br />&lt;/html&gt;</div>
<p></td>
</tr>
</tbody>
</table>
<p><span><br />程序逻辑继续使用starter项目里面的内容。 <br />xwork中配置如下： <br /></span></p>
<table cellspacing="1" cellpadding="3" width="90%" align="center" border="0">
<tbody>
<tr>
<td><span><b>java代码: </b></span>
</td>
</tr>
<tr>
<td>
<div style="font-family:'Courier New', Courier, monospace">&lt;action name=&quot;newPerson&quot; <font face="Courier New"><span style="font-weight:bold;color:#990066">class</span>=&quot;com.<span style="color:#000000">acme</span>.<span style="color:#000000">CreatePerson</span>&quot;&gt; <br />            &lt;result name=&quot;success&quot; type=&quot;redirect&quot;&gt;listPeople.<span style="color:#000000">action</span>&lt;/result&gt; <br />            &lt;!&#8211; <br />            &lt;result name=&quot;input&quot; type=&quot;dispatcher&quot;&gt;newPerson.<span style="color:#000000">jsp</span>&lt;/result&gt; <br />            &#8211;&gt; <br />            &lt;result name=&quot;input&quot; type=&quot;freemarker&quot;&gt;newPerson.<span style="color:#000000">ftl</span></font>&lt;/result&gt; <br />        &lt;/action&gt;</div>
<p></td>
</tr>
</tbody>
</table>
<p><span>两个&quot;success&quot;的result轮流切换。 </p>
<p>试验结果如下： <br />1、webwork.properties配置如下，使用vm的template，此时使用两种result的结果是相同的： <br /></span></p>
<table cellspacing="1" cellpadding="3" width="90%" align="center" border="0">
<tbody>
<tr>
<td><span><b>java代码: </b></span>
</td>
</tr>
<tr>
<td>
<div style="font-family:'Courier New', Courier, monospace">webwork.<font face="Courier New"><span style="color:#000000">ui</span>.<span style="color:#000000">theme</span>=simple <br />webwork.<span style="color:#000000">ui</span>.<span style="color:#000000">templateDir</span>=template/archive <br />webwork.<span style="color:#000000">ui</span>.<span style="color:#000000">templateSuffix</span></font>=vm</div>
<p></td>
</tr>
</tbody>
</table>
<p><span>显示结果，显然onsubmit没有输出，此时datapicker标签没有输出（因为老的vm模板没有提供datapicker标签）： <br /></span></p>
<table cellspacing="1" cellpadding="3" width="90%" align="center" border="0">
<tbody>
<tr>
<td><span><b>java代码: </b></span>
</td>
</tr>
<tr>
<td>
<div style="font-family:'Courier New', Courier, monospace">&lt;form namespace=&quot;/&quot; id=&quot;newPerson&quot; name=&quot;newPerson&quot; action=&quot;/starter/newPerson.<span style="color:#000000"><font face="Courier New">action</font></span>&quot;&gt;</div>
<p></td>
</tr>
</tbody>
</table>
<p><span>2、webwork.properties配置如下，此时使用ftl的result和ftl的template： <br /></span></p>
<table cellspacing="1" cellpadding="3" width="90%" align="center" border="0">
<tbody>
<tr>
<td><span><b>java代码: </b></span>
</td>
</tr>
<tr>
<td>
<div style="font-family:'Courier New', Courier, monospace">webwork.<font face="Courier New"><span style="color:#000000">ui</span>.<span style="color:#000000">theme</span>=simple <br />webwork.<span style="color:#000000">ui</span>.<span style="color:#000000">templateDir</span>=template <br />webwork.<span style="color:#000000">ui</span>.<span style="color:#000000">templateSuffix</span></font>=ftl</div>
<p></td>
</tr>
</tbody>
</table>
<p><span>显示结果，onsubmit输出正常： <br /></span></p>
<table cellspacing="1" cellpadding="3" width="90%" align="center" border="0">
<tbody>
<tr>
<td><span><b>java代码: </b></span>
</td>
</tr>
<tr>
<td>
<div style="font-family:'Courier New', Courier, monospace">&lt;form id=&quot;newPerson&quot; name=&quot;newPerson&quot; onsubmit=&quot;dosomething&quot; action=&quot;/starter/newPerson.<span style="color:#000000"><font face="Courier New">action</font></span>&quot;&gt;</div>
<p></td>
</tr>
</tbody>
</table>
<p><span><br />3、webwork.properties配置如下，此时使用jsp的result和ftl的template： <br /></span></p>
<table cellspacing="1" cellpadding="3" width="90%" align="center" border="0">
<tbody>
<tr>
<td><span><b>java代码: </b></span>
</td>
</tr>
<tr>
<td>
<div style="font-family:'Courier New', Courier, monospace">webwork.<font face="Courier New"><span style="color:#000000">ui</span>.<span style="color:#000000">theme</span>=simple <br />webwork.<span style="color:#000000">ui</span>.<span style="color:#000000">templateDir</span>=template <br />webwork.<span style="color:#000000">ui</span>.<span style="color:#000000">templateSuffix</span></font>=ftl</div>
<p></td>
</tr>
</tbody>
</table>
<p><span><br />显示结果，onsubmit没有输出： <br /></span></p>
<table cellspacing="1" cellpadding="3" width="90%" align="center" border="0">
<tbody>
<tr>
<td><span><b>java代码: </b></span>
</td>
</tr>
<tr>
<td>
<div style="font-family:'Courier New', Courier, monospace">&lt;form namespace=&quot;/&quot; id=&quot;newPerson&quot; name=&quot;newPerson&quot; action=&quot;/starter/newPerson.<span style="color:#000000"><font face="Courier New">action</font></span>&quot;&gt;</div>
<p></td>
</tr>
</tbody>
</table>
<p><span><br />此时尝试使用theme=&quot;xhtml&quot;还是没有输出。 </p>
<p>从上面的测试来看，似乎是个bug，可是在webwork网站却没有查到相关issue，怀疑是我这里的环境问题。以上测试在tomcat 5.5.12 or Weblogic 9.1 @ SUN JDK 1.5b6上面进行。希望哪位解决了上面问题指导一下我，谢谢。</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.diamondtin.com/2006/webwork-22%e7%9a%84form-tag%e5%9c%a8%e4%bd%bf%e7%94%a8jsp-view%e6%97%b6onsubmit%e4%b8%8d%e5%b7%a5%e4%bd%9c/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Spring+Hibernate中使用Mysql存储过程初步</title>
		<link>http://www.diamondtin.com/2006/springhibernate%e4%b8%ad%e4%bd%bf%e7%94%a8mysql%e5%ad%98%e5%82%a8%e8%bf%87%e7%a8%8b%e5%88%9d%e6%ad%a5/</link>
		<comments>http://www.diamondtin.com/2006/springhibernate%e4%b8%ad%e4%bd%bf%e7%94%a8mysql%e5%ad%98%e5%82%a8%e8%bf%87%e7%a8%8b%e5%88%9d%e6%ad%a5/#comments</comments>
		<pubDate>Thu, 19 Jan 2006 21:22:00 +0000</pubDate>
		<dc:creator>tin</dc:creator>
				<category><![CDATA[Tech.技术]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://tin.zztin.com/2006/01/springhibernate%e4%b8%ad%e4%bd%bf%e7%94%a8mysql%e5%ad%98%e5%82%a8%e8%bf%87%e7%a8%8b%e5%88%9d%e6%ad%a5/</guid>
		<description><![CDATA[Hibernate中使用Mysql存储过程 1、我使用了mysql-connector-java-5.0.0-beta-bin.jar（其实用老的mysql-connector-java-3.1.8-bin.jar也可以调用存储过程的）这个最新的mysql驱动。 2、数据库我使用了mysql-5.0.18-win32，... ]]></description>
			<content:encoded><![CDATA[<p><span>Hibernate中使用Mysql存储过程 <br />1、我使用了mysql-connector-java-5.0.0-beta-bin.jar（其实用老的mysql-connector-java-3.1.8-bin.jar也可以调用存储过程的）这个最新的mysql驱动。 <br />2、数据库我使用了mysql-5.0.18-win32，安装后建立了一个简单的数据表。 <br />sql如下： <br /></span></p>
<table cellspacing="1" cellpadding="3" width="90%" align="center" border="0">
<tbody>
<tr>
<td><span><b>java代码: </b></span>
</td>
</tr>
<tr>
<td>
<div style="font-family:'Courier New', Courier, monospace">create database testprocedure; <br />use testprocedure; <br />create table testtable <font face="Courier New"><span style="color:#000000">(</span>id <span style="font-weight:bold;color:#990066">int</span><span style="color:#000000">(</span><span style="color:#000000">11</span><span style="color:#000000">)</span> AUTO_INCREMENT, content varchar<span style="color:#000000">(</span><span style="color:#000000">255</span><span style="color:#000000">)</span>, readcount <span style="font-weight:bold;color:#990066">int</span><span style="color:#000000">(</span><span style="color:#000000">11</span><span style="color:#000000">)</span> DEFAULT <span style="color:#000000">0</span>,primary key <span style="color:#000000">(</span>id<span style="color:#000000">)</span><span style="color:#000000">)</span></font>; <br />desc testtable;（查看是否建立正确）</div>
<p></td>
</tr>
</tbody>
</table>
<p><span><br />3、建立一个专用的用户（可选，建立时请使用具有grant权限的用户如root）： <br /></span></p>
<table cellspacing="1" cellpadding="3" width="90%" align="center" border="0">
<tbody>
<tr>
<td><span><b>java代码: </b></span>
</td>
</tr>
<tr>
<td>
<div style="font-family:'Courier New', Courier, monospace">grant select,delete,update,create,alter,execute on testtable.* to testprocedure@&quot;localhost&quot; identified by &quot;test&quot;;</div>
<p></td>
</tr>
</tbody>
</table>
<p><span>用户名为testprocedure，密码test。注意权限中的execute，它就是执行call procedure的权限。在你的Hibernate配置中使用该帐户。 <br />4、建立一个存储过程： <br />sql如下： <br /></span></p>
<table cellspacing="1" cellpadding="3" width="90%" align="center" border="0">
<tbody>
<tr>
<td><span><b>java代码: </b></span>
</td>
</tr>
<tr>
<td>
<div style="font-family:'Courier New', Courier, monospace">delimiter <font face="Courier New"><span style="color:#6666ff">//</span> <br />（注意<span style="color:#6666ff">//是新的命令结束符，方便我们建立procedure）</span> <br />create procedure readcountplusone <span style="color:#000000">(</span>inputid <span style="font-weight:bold;color:#990066">int</span><span style="color:#000000">)</span> <br />begin <br />update testtable set readcount = readcount + <span style="color:#000000">1</span> where id = inputid; <br />end<span style="color:#6666ff">//</span></font> <br />（建立存储过程完毕） <br />delimiter ; <br />（恢复命令结束符为;）</div>
<p></td>
</tr>
</tbody>
</table>
<p><span>5、测试一下存储过程： <br /></span></p>
<table cellspacing="1" cellpadding="3" width="90%" align="center" border="0">
<tbody>
<tr>
<td><span><b>java代码: </b></span>
</td>
</tr>
<tr>
<td>
<div style="font-family:'Courier New', Courier, monospace">insert into testtable values <font face="Courier New"><span style="color:#000000">(</span><span style="font-weight:bold;color:#990066">null</span>,<span style="color:#0000ff">&#8216;test&#8217;</span>,<span style="color:#000000">0</span><span style="color:#000000">)</span>; <br />select * from testtable; <br />call readcountplusone<span style="color:#000000">(</span><span style="color:#000000">1</span><span style="color:#000000">)</span></font>; <br />select * from testtable;</div>
<p></td>
</tr>
</tbody>
</table>
<p><span>应该看到原先readcount为0，call以后变成1，而且每次call都加1。 <br />如果执行有错，可以删除procedure重新建立。 <br />删除的命令为drop procedure readcountplusone; <br />6、开始在我们的Hibernate+Spring support项目中使用procedure： <br />HBM映射我们不说了，这里没有使用named query。Hibernate+Spring的配置这里也不多说了，应该可以搜寻到很多文章。 <br />我的DAO是extends HibernateDAO，具体的使用方法可以参照其他很多讲Spring hibernate support的文章。 <br />我们建立一个方法，比较丑陋（只是测试，大家有好方法可以提），假设对应testtable的pojo为TestPojo，它的getId()返回id对应的值： <br /></span></p>
<table cellspacing="1" cellpadding="3" width="90%" align="center" border="0">
<tbody>
<tr>
<td><span><b>java代码: </b></span>
</td>
</tr>
<tr>
<td>
<div style="font-family:'Courier New', Courier, monospace"><font face="Courier New"><span style="font-weight:bold;color:#990066">public</span> <span style="font-weight:bold;color:#990066">void</span> readCountPlusOne<span style="color:#000000">(</span><span style="font-weight:bold;color:#990066">final</span> TestPojo pojo<span style="color:#000000">)</span> <span style="color:#000000">{</span> <br />        getHibernateTemplate<span style="color:#000000">(</span><span style="color:#000000">)</span>.<span style="color:#000000">execute</span><span style="color:#000000">(</span><span style="font-weight:bold;color:#990066">new</span> HibernateCallback<span style="color:#000000">(</span><span style="color:#000000">)</span> <span style="color:#000000">{</span> <br /> <span style="font-weight:bold;color:#990066">public</span> <span style="color:#aaaadd">Object</span> doInHibernate<span style="color:#000000">(</span>Session session<span style="color:#000000">)</span> <span style="color:#000000">{</span> <br /> <span style="font-weight:bold;color:#990066">try</span> <span style="color:#000000">{</span> <br /> <span style="color:#aaaadd">Connection</span> conn = session.<span style="color:#000000">connection</span><span style="color:#000000">(</span><span style="color:#000000">)</span>; </p>
<p> <span style="color:#aaaadd">String</span> sql = &quot;<span style="color:#000000">{</span>call readcountplusone<span style="color:#000000">(</span>?<span style="color:#000000">)</span><span style="color:#000000">}</span>&quot;; <br /> <span style="color:#aaaadd">CallableStatement</span> stmt = conn.<span style="color:#000000">prepareCall</span><span style="color:#000000">(</span>sql<span style="color:#000000">)</span>; <br />                        stmt.<span style="color:#000000">setLong</span><span style="color:#000000">(</span><span style="color:#000000">1</span>, pojo.<span style="color:#000000">getId</span><span style="color:#000000">(</span><span style="color:#000000">)</span>.<span style="color:#000000">longValue</span><span style="color:#000000">(</span><span style="color:#000000">)</span><span style="color:#000000">)</span>; <br />                        stmt.<span style="color:#000000">execute</span><span style="color:#000000">(</span><span style="color:#000000">)</span>; <br /> <span style="color:#000000">}</span> <span style="font-weight:bold;color:#990066">catch</span> <span style="color:#000000">(</span><span style="color:#aaaadd">Exception</span> e<span style="color:#000000">)</span> <span style="color:#000000">{</span> <br /> <span style="font-weight:bold;color:#990066">if</span><span style="color:#000000">(</span>log.<span style="color:#000000">isDebugEnable</span><span style="color:#000000">)</span><span style="color:#000000">{</span> <br />                                log.<span style="color:#000000">debug</span><span style="color:#000000">(</span>&quot;call DAO</font><span style="color:#0000ff"><font face="Courier New">&#8216;s readCountPlusOne() faild, with Exception:&quot;); <br />                                e.printStackTrace(); <br />                        } <br />                    } </p>
<p>                    return null; <br />                } <br />            }); <br />    }</font></span></div>
<p></td>
</tr>
</tbody>
</table>
<p><span>7、然后我们在我们的bussiness中调用readCountPlusOne方法既可实现通过Hibernate调用一个简单的Mysql存储过程。 </p>
<p>有点走马观花，主要是把口舌都放在mysql部分，hibernate部分则用的比较简单，我想把调用方法改为Named Query，这样不会这么丑陋。 <br />抛砖引玉，谢谢。</span></p>
<div></div>
]]></content:encoded>
			<wfw:commentRss>http://www.diamondtin.com/2006/springhibernate%e4%b8%ad%e4%bd%bf%e7%94%a8mysql%e5%ad%98%e5%82%a8%e8%bf%87%e7%a8%8b%e5%88%9d%e6%ad%a5/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

