如何解决Struts Hibernate的整合问题
创始人
2024-06-07 15:31:18
0

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

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

  1.  version="1.0" encoding="UTF-8"?> 
  2.                                          "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"> 
  3.  
  4.      
  5.          name="show_sql">falseproperty> 
  6.          name="use_outer_join">trueproperty> 
  7.          name="hibernate.dialect">net.sf.hibernate.dialect.MySQLDialectproperty> 
  8.          name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driverproperty> 
  9.          name="hibernate.connection.url">jdbc:mysql:///testproperty> 
  10.          name="hibernate.connection.username">rootproperty> 
  11.          name="hibernate.connection.password">property> 
  12.          name="hibernate.connection.pool.size">20property> 
  13.          name="session_factory_name">hibernate/session_factoryproperty> 
  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。

【编辑推荐】

  1. Hibernate3.1简单描述
  2. Hibernate save基础简介
  3. 浅析Hibernate 3二级缓存基础
  4. Hibernate流行架构浅析
  5. Hibernate update浅谈

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
着眼MAC地址,解救无法享受D... 在安装了DHCP服务器的局域网环境中,每一台工作站在上网之前,都要先从DHCP服务器那里享受到地址动...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...