溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么解決Struts Hibernate的整合問題

發(fā)布時(shí)間:2021-12-06 09:25:56 來源:億速云 閱讀:111 作者:iii 欄目:編程語(yǔ)言

這篇文章主要介紹“怎么解決Struts Hibernate的整合問題”,在日常操作中,相信很多人在怎么解決Struts Hibernate的整合問題問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”怎么解決Struts Hibernate的整合問題”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!

把Hibernate用到的包全部放進(jìn)web應(yīng)用程序的lib里面,然后定義hibernate.cfg.xml,也就是插件的配置信息,如下

<?xml version="1.0" encoding="UTF-8"?>                                          "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"> <hibernate-configuration>     <session-factory>         <property name="show_sql">falseproperty>         <property name="use_outer_join">trueproperty>         <property name="hibernate.dialect">net.sf.hibernate.dialect.MySQLDialectproperty>         <property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driverproperty>         <property name="hibernate.connection.url">jdbc:mysql:///testproperty>         <property name="hibernate.connection.username">rootproperty>         <property name="hibernate.connection.password">property>         <property name="hibernate.connection.pool.size">20property>         <property name="session_factory_name">hibernate/session_factoryproperty>              <mapping resource="/homepage/Artical.hbm.xml"/>      session-factory> hibernate-configuration>

信息很清楚的,就是配置數(shù)據(jù)庫(kù)等信息,我們利用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();      }    }  }

相應(yīng)的,要在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();


當(dāng)然,還有另外兩種整合方式,我認(rèn)為這種方法更為靈活,也比較容易掌握。

到此,關(guān)于“怎么解決Struts Hibernate的整合問題”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI