??xml version="1.0" encoding="utf-8" standalone="yes"?>在线中文字幕视频,九九九九久久久久,激情综合婷婷http://www.aygfsteel.com/szhswl/category/27818.html宋针q的个hI间zh-cnWed, 05 Dec 2007 14:52:17 GMTWed, 05 Dec 2007 14:52:17 GMT60使用模板模式化DAO操作Hibernate http://www.aygfsteel.com/szhswl/articles/165620.html宋针q?/dc:creator>宋针q?/author>Wed, 05 Dec 2007 10:49:00 GMThttp://www.aygfsteel.com/szhswl/articles/165620.htmlhttp://www.aygfsteel.com/szhswl/comments/165620.htmlhttp://www.aygfsteel.com/szhswl/articles/165620.html#Feedback0http://www.aygfsteel.com/szhswl/comments/commentRss/165620.htmlhttp://www.aygfsteel.com/szhswl/services/trackbacks/165620.html   
  getHibernateTemplate().save(user);
  
  q样一句话在我们没有Spring的时候就必须使用如下的代码才能完成:
  
  Session session = HibernateUtil.getSession();
  Transaction tx = session.beginTransaction();
  session.save(user);
  tx.commit();
  HibernateUtil.colseSession();
  q里q省M异常处理Q同时用了HibernateUtilcL化从SessionFactory获取SessionQ以及关闭Session{处理?br />   
  但是我们在用Hibernate的时候不一定会使用SpringQ所以我们可以模仿Spring的处理方式,做一个Hibernate的模板,使用模板模式来简化我们的开发,其主要的目的是Z化开发,使代码达到最大话的重用?br />   
  1Q我们现来实C个Hibernate模板Q?/strong>
  
 1  package kick.hibernate;
 2  
 3  import net.sf.hibernate.HibernateException;
 4  import net.sf.hibernate.Session;
 5  import net.sf.hibernate.Transaction;
 6  
 7  public class HibernateTemplate{
 8  public static Object run(HibernateCallback callback) throws HibernateException{
 9  Session session = null;
10  Transaction tx = null;
11  try {
12  session = HibernateSessionutil.currentSession();
13  tx = session.beginTransaction();
14  Object result = callback.execute(session);
15  tx.commit();
16  session.flush();
17  return result;
18  }
 catch (HibernateException e) {
19  tx.rollback();
20  return null;
21  }
 finally {
22  HibernateSessionutil.closeSession();
23  }

24  }

25
  q里cd单,是使用一个实现HibernateCallBack接口的一个回掉类Q在调用的时候根据具体的需求实现HibernateCallBackcR?br />   
  2Q回掉接口HibernateCallBackQ?/strong>
1  package kick.hibernate;
2  
3  import net.sf.hibernate.HibernateException;
4  import net.sf.hibernate.Session;
5  
6  public interface HibernateCallBack {
7  Object execute(Session session)throws HibernateException;
8  }

  
  好了Q到此ؓ止我们就可以使用q个模板了,可以用如下的方式使用Q?br />   HibernateTemplate.run(new HibernateCallback() {
  public Object execute(Session session) throws HibernateException {
  session.save(user);
  return null;
  }
  });
  
  看看Q是不是省去了很多代码?^_^
  
  不过q还没有辑ֈ想Spring里面那样单,不要着急,“面包会有?#8221;呵呵Q我们会辑ֈ的?br />   
  3Q实现我们自qHibernateSupportc:
  
  从上面的代码可以看出Q我们要自己实现HibernateCallback接口Q而每ơ我们实现的时候又重复代码了。因此我们再抽象Q讲q些实现攑ֈ我们的HibernateSupportc里面去。看看我们上面的代码q道我们实现HibernateCallback接口的目的就是ؓ了调用session.save()ҎQ即session的方法。代码如下:
  
 1  package kick.hibernate;
 2  
 3  import java.io.Serializable;
 4  
 5  import net.sf.hibernate.HibernateException;
 6  import net.sf.hibernate.Session;
 7  
 8  public class HibernateSupport{
 9  
10  public Object save(final Object object) throws HibernateException{
11  return HibernateTemplate.run(new HibernateCallBack(){
12  
13  public Object execute(Session session) throws HibernateException {
14  session.save(object);
15  return null;
16  }

17  
18  }
);
19  }

20  public Object save(final Object object,final Serializable id) throws HibernateException{
21  return HibernateTemplate.run(new HibernateCallBack(){
22  
23  public Object execute() throws HibernateException {
24  session.save(object,id);
25  return null;
26  }

27  
28  }
);
29  }

30  
31  public Object saveOrUpdate(final Object object) throws HibernateException{
32  return HibernateTemplate.run(new HibernateCallBack(){
33  
34  public Object execute(Session session) throws HibernateException {
35  session.saveOrUpdate(object);
36  return null;
37  }

38  
39  }
);
40  }

41  ……………………………………………………………………………………
42  ……………………………………………………………………………………
43  ……………………………………………………………………………………
44  

  调用一些其他的session的方法?br />   
  }
  
  4Q抽象RootDaoQ?/strong>
  
  该类为抽象类Q在实现自己的DAOcȝ时候承该cR该cȝ有一个HibernateSupport的对象,在子cM使用getHibernateTemplate()Ҏ可以得到该对象Q然后调用它对应的方法。实C码如下:
  
 1  package kick.hibernate.dao;
 2  
 3  import net.sf.hibernate.Session;
 4  import kick.hibernate.HibernateTemplateImpl;
 5  
 6  public abstract class RootDao {
 7  private HibernateSupport temp = null;
 8  
 9  /**
10  * @return Returns the temp.
11  */

12  public HibernateTemplateImpl getHibernateTemplate(Session session) {
13  return new HibernateSupport();
14  }

15  }

  
  5Q用例子:
  
  定义一个自qDAOc,实现代码如下Q?br />   
  public class UserDaoImpl extends RootDao implements UserDaoInterface{
  public void saveUser(User user) throws KickException {
  getHibernateTemplate().saveOrUpdate(user);
  }
  ……………………………………………………………………………………
  实现其他的方?br />   ……………………………………………………………………………………
  }

]]>
优化hibernate性能的几点徏?/title><link>http://www.aygfsteel.com/szhswl/articles/165525.html</link><dc:creator>宋针q?/dc:creator><author>宋针q?/author><pubDate>Wed, 05 Dec 2007 07:30:00 GMT</pubDate><guid>http://www.aygfsteel.com/szhswl/articles/165525.html</guid><wfw:comment>http://www.aygfsteel.com/szhswl/comments/165525.html</wfw:comment><comments>http://www.aygfsteel.com/szhswl/articles/165525.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/szhswl/comments/commentRss/165525.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/szhswl/services/trackbacks/165525.html</trackback:ping><description><![CDATA[1、针对oracle数据库而言QFetch Size 是设定JDBC的Statementd数据的时候每ơ从数据库中取出的记录条敎ͼ一般设|ؓ30?0?00。Oracle数据库的JDBC驱动默认的Fetch Size=15Q设|Fetch Size讄为:30?0Q性能会有明显提升Q如果l增大,出100Q性能提升不明显,反而会消耗内存?br /> <br />   卛_hibernate配制文g中进行配Ӟ<br /> <br /> <table bordercolor="#cccccc" width="90%" align="center" bgcolor="#e3e3e3" border="1"> <tbody> <tr> <td>1 Qproperty name="hibernateProperties"Q?br /> 2 QpropsQ?br /> 3 Qprop key="hibernate.dialect"Qorg.hibernate.dialect.Oracle9DialectQ?propQ?br /> 4 Qprop key="hibernate.show_sql"QfalseQ?propQ?br /> 5 Q?-- Create/update the database tables automatically when the JVM starts up<br /> 6 Qprop key="hibernate.hbm2ddl.auto"QupdateQ?propQ?--Q?br /> 7 Q?-- Turn batching off for better error messages under PostgreSQL <br /> 8 Qprop key="hibernate.jdbc.batch_size"Q?00Q?propQ?--Q?br /> 9 Qprop key="hibernate.jdbc.batch_size"Q?0Q?propQ?br /> 10 Q?propsQ?br /> 11 Q?propertyQFetch Size讄大Q读数据库的ơ数少Q速度快QFetch Size小Q读数据库的ơ数多Q速度慢?/td> </tr> </tbody> </table> <br />   2、如果是大的系l,生成htm文g。加快页面提升速度?br /> <br />   3、不要把所有的责Q推在hibernate上,对代码进行重构,减少Ҏ据库的操作,量避免在数据库查询时用in操作Q以及避免递归查询操作Q代码质量、系l设计的合理性决定系l性能的高低?br /> <br />   4?对大数据量查询时Q慎用list()或者iterator()q回查询l果Q?<br /> <br />   Q?Q? 使用List()q回l果ӞHibernate会所有查询结果初始化为持久化对象Q结果集较大Ӟ会占用很多的处理旉?<br /> <br />   Q?Q? 而用iterator()q回l果Ӟ在每ơ调用iterator.next()q回对象q用对象时QHibernate才调用查询将对应的对象初始化Q对于大数据量时Q每调用一ơ查询都会花费较多的旉。当l果集较大,但是含有较大量相同的数据Q或者结果集不是全部都会使用Ӟ使用iterator()才有优势?br /> <br />   5、在一对多、多对一的关pMQ用gq加载机Ӟ会不少的对象在使用时方会初始化Q这样可使得节省内存I间以及减少数据库的负荷Q而且若PO中的集合没有被用时Q就可减互数据库的交互从而减处理时间?<br /> <br />   6、对含有兌的POQ持久化对象Q时Q若default-cascade="all"或?“save-update”Q新增POӞh意对PO中的集合的赋值操作,因ؓ有可能得多执行一ơupdate操作?<br /> <br />   7?对于大数据量新增、修攏V删除操作或者是对大数据量的查询Q与数据库的交互ơ数是决定处理时间的最重要因素Q减交互的ơ数是提升效率的最好途径Q所以在开发过E中Q请show_sql讄为trueQ深入了解Hibernate的处理过E,试不同的方式,可以使得效率提升。尽可能Ҏ个页面的昄Q对数据库的操作减少?00----150条以内。越越好? <img src ="http://www.aygfsteel.com/szhswl/aggbug/165525.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/szhswl/" target="_blank">宋针q?/a> 2007-12-05 15:30 <a href="http://www.aygfsteel.com/szhswl/articles/165525.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss> <footer> <div class="friendship-link"> <a href="http://www.aygfsteel.com/" title="狠狠久久亚洲欧美专区_中文字幕亚洲综合久久202_国产精品亚洲第五区在线_日本免费网站视频">狠狠久久亚洲欧美专区_中文字幕亚洲综合久久202_国产精品亚洲第五区在线_日本免费网站视频</a> </div> </footer> վ֩ģ壺 <a href="http://" target="_blank">ͨ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">Ӣ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ͷ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">潭</a>| <a href="http://" target="_blank">Ȫ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ƽ</a>| <a href="http://" target="_blank">Դ</a>| <a href="http://" target="_blank">ȫ</a>| <a href="http://" target="_blank">żҿ</a>| <a href="http://" target="_blank">ն</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ˮ</a>| <a href="http://" target="_blank">׿</a>| <a href="http://" target="_blank">Ϫ</a>| <a href="http://" target="_blank">ɽ</a>| <a href="http://" target="_blank">ɽ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">Į</a>| <a href="http://" target="_blank">ͩ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ʯ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">差</a>| <a href="http://" target="_blank">̳</a>| <a href="http://" target="_blank">Ϸ</a>| <script> (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> </body>