struts和hibernate整合的一種方式(hibernate作為struts外掛程式)

來源:互聯網
上載者:User

把hibernate用到的包全部放進web應用程式的lib裡面,然後定義hibernate.cfg.xml,也就是外掛程式的配置資訊,如下

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"                                         "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"><hibernate-configuration>    <session-factory>        <property name="show_sql">false</property>        <property name="use_outer_join">true</property>        <property name="hibernate.dialect">net.sf.hibernate.dialect.MySQLDialect</property>        <property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>        <property name="hibernate.connection.url">jdbc:mysql:///test</property>        <property name="hibernate.connection.username">root</property>        <property name="hibernate.connection.password"></property>        <property name="hibernate.connection.pool.size">20</property>        <property name="session_factory_name">hibernate/session_factory</property>        <!-- Mapping files -->    <mapping resource="/homepage/Artical.hbm.xml"/>

    </session-factory></hibernate-configuration>資訊很清楚的,就是設定資料庫等資訊,我們利用jndi來尋找SessionFactory,下面是外掛程式程式,HibernatePlugIn.java

import org.apache.struts.action.PlugIn;import org.apache.struts.action.ActionServlet;import org.apache.struts.config.ModuleConfig;import javax.servlet.ServletException;import javax.servlet.ServletContext;import net.sf.hibernate.SessionFactory;import net.sf.hibernate.cfg.Configuration;public class HibernatePlugIn    implements PlugIn {  public void destroy() {  }  public void init(ActionServlet servlet, ModuleConfig config) throws      ServletException {    try {      ServletContext context = servlet.getServletContext();      SessionFactory sf = new Configuration().configure().buildSessionFactory();      context.setAttribute("net.sf.hibernate.SessionFactory",sf);    }    catch (Exception ex) {      ex.printStackTrace();    }  }}相應的,要在struts-config.xml配置外掛程式資訊,大概如下,注意路徑

 <plug-in className="homepage.HibernatePlugIn">    <set-property property="configFilePath" value="/WEB-INF/classes/hibernate.cfg.xml" />    <set-property property="storeInServletContext" value="true" />  </plug-in>

以後就可以利用java.naming.Context,java.naming.InitiaContext來尋找

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

當然,還有另外兩種整合方式,我認為這種方法更為靈活,也比較容易掌握


相關文章

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

Starter Package

SSD Cloud server and data transfer for only $2.50 a month

Get Started >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。