如何解决StrutsHibernate的整合问题

我们今天要讨论struts Hibernate ,下面通一实例,我们能更深刻的了解struts Hibernate的整合问题。希望本文能对大家了解有所帮助。

把Hibernate用到的包全部放进web应用程序的lib里面,然后定义hibernate.cfg.xml,也就是插件的配置信息,如下

 
 
 
  1. xml version="1.0" encoding="UTF-8"?> 
  2.                                          "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"> 
  3.  
  4.      
  5.          name="show_sql">false property> 
  6.          name="use_outer_join">true property> 
  7.          name="hibernate.dialect">net.sf.hibernate.dialect.MySQLDialect property> 
  8.          name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver property> 
  9.          name="hibernate.connection.url">jdbc:mysql:///test property> 
  10.          name="hibernate.connection.username">root property> 
  11.          name="hibernate.connection.password"> property> 
  12.          name="hibernate.connection.pool.size">20 property> 
  13.          name="session_factory_name">hibernate/session_factory property> 
  14.           
  15.      resource="/homepage/Artical.hbm.xml"/> 
  16.  
  17.      session-factory> 
  18. hibernate-configuration> 
  19.  

信息很清楚的,就是配置数据库等信息,我们利用jndi来查找SessionFactory,下面是插件程序,

 
 
 
  1. HibernatePlugIn.java  
  2.  
  3. import org.apache.struts.action.PlugIn;  
  4. import org.apache.struts.action.ActionServlet;  
  5. import org.apache.struts.config.ModuleConfig;  
  6. import javax.servlet.ServletException;  
  7. import javax.servlet.ServletContext;  
  8. import net.sf.hibernate.SessionFactory;  
  9. import net.sf.hibernate.cfg.Configuration;  
  10. public class HibernatePlugIn  
  11.     implements PlugIn {  
  12.   public void destroy() {  
  13.   }  
  14.   public void init(ActionServlet servlet, ModuleConfig config) throws  
  15.       ServletException {  
  16.     try {  
  17.       ServletContext context = servlet.getServletContext();  
  18.       SessionFactory sf = new Configuration().configure().buildSessionFactory();  
  19.       context.setAttribute("net.sf.hibernate.SessionFactory",sf);  
  20.     }  
  21.     catch (Exception ex) {  
  22.       ex.printStackTrace();  
  23.     }  
  24.   }  
  25. }  
  26.  

相应的,要在struts-config.xml配置插件信息,大概如下,注意路径

 
 
 
  1.  className="homepage.HibernatePlugIn"> 
  2.      property="configFilePath" value="/WEB-INF/classes/hibernate.cfg.xml" /> 
  3.      property="storeInServletContext" value="true" /> 
  4.    plug-in> 

以后就可以利用java.naming.Context,java.naming.InitiaContext来查找

 
 
 
  1. Context ct = new InitialContext();  
  2.       sessions=(SessionFactory) ct.lookup("hibernate/session_factory");  
  3.       session=sessions.openSession();  

当然,还有另外两种整合方式,我认为这种方法更为灵活,也比较容易掌握。以上介绍struts Hibernate。

网页名称:如何解决StrutsHibernate的整合问题
链接地址:http://www.hantingmc.com/qtweb/news18/423968.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联