2012-07-31 96 views
-1

我正在通過hibernate這幾天然後我正在嘗試開發一個應用程序來集成struts2 + Hibernate + Spring ..我有兩種方法來初始化hibernate會話工廠..一個是當我們加載過濾器調度員的struts2當時,如下圖所示,我們應該加載例如Hibernate的SessionFactory ..集成struts2 + spring +休眠

web.xml中......

<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 
    <welcome-file-list> 
    <welcome-file>index.jsp</welcome-file> 
    </welcome-file-list> 
    <listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <filter> 
    <filter-name>controller</filter-name> 
    <filter-class>mypack.Struts2Dispatcher</filter-class> 
    </filter> 
    <filter-mapping> 
    <filter-name>controller</filter-name> 
    <url-pattern>/*</url-pattern> 
    </filter-mapping></web-app> 

Struts2Dispatcher文件..

import javax.servlet.*; 
import org.apache.struts2.dispatcher.FilterDispatcher; 
import org.hibernate.HibernateException; 

public class Struts2Dispatcher extends FilterDispatcher 
{ 
    @Override 
    public void init(FilterConfig filterConfig) throws ServletException 
    { 
     super.init(filterConfig); 
     try 
     { 
      HibernateUtil.createSessionFactory(); 
      System.out.print("-------application initializing successfully-----"); 
     } 
     catch (HibernateException e) 
     { 
      throw new ServletException(e); 
     } 
    } 
} 

和休眠UTIL ..

package mypack; 

import org.hibernate.SessionFactory; 
import org.hibernate.cfg.Configuration; 
import org.hibernate.Session; 

public class HibernateUtil 
{ 
    private static SessionFactory sessionFactory; 
    public static void createSessionFactory() 
    { 
     sessionFactory = new Configuration().configure().buildSessionFactory(); 
    } 
    public static Session getSession() 
    { 
     return sessionFactory.openSession(); 
    } 
} 

配置文件..

<?xml version='1.0' encoding='UTF-8'?> 
<!DOCTYPE hibernate-configuration PUBLIC 
      "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
      "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 

<!-- Generated by MyEclipse Hibernate Tools.     --> 
<hibernate-configuration> 

    <session-factory> 
     <property name="connection.username">system</property> 
     <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property> 
     <property name="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</property> 
     <property name="connection.password">manager</property> 
     <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> 
     <property name="show_sql">true</property> 
     <mapping resource="Employee.hbm.xml"/> 
    </session-factory> 

</hibernate-configuration> 

映射文件..

<!DOCTYPE hibernate-mapping PUBLIC 
      "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
      "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
      <hibernate-mapping> 
      <class name="mypack.Employee"> 
      <id name="eid" column="emp_id" type="int"> 
      <generator class="increment"/></id> 
      <property name="name" column="emp_name"/> 
      <property name="job"/> 
      <property name="salary" type="int"/> 
      </class> 
      </hibernate-mapping> 

應用context.xml文件

<beans 
    xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> 
<bean id="MyIOCObject" class="mypack.LoginAction"/> 

</beans> 

的struts.xml文件..

<struts>  
<package name="pack" extends="struts-default"> 
<action name="login" class="MyIOCObject" method="insert"> 
<result name="success">/welcome.jsp</result> 
<result name="failure">/relogin.jsp</result> 
</action> 
</package> 
</struts>  

登錄操作Java類

package mypack; 

import org.hibernate.*; 
import org.hibernate.cfg.Configuration; 

public class LoginAction 
{ 
    private String name,job; 
    private int salary; 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 


    public String insert() 
    { 
     try 
     { 
     Session ses=HibernateUtil.getSession(); 
     Employee emp=new Employee(name,job,salary); 
     Transaction tx=ses.beginTransaction(); 
     ses.save(emp); 
     tx.commit(); 
     ses.close(); 
     return "success"; 
     }catch(Exception e) 
     { 
      System.out.println(e); 
      return "failure"; 
     } 
    } 

    public String getJob() { 
     return job; 
    } 

    public void setJob(String job) { 
     this.job = job; 
    } 

    public int getSalary() { 
     return salary; 
    } 

    public void setSalary(int salary) { 
     this.salary = salary; 
    } 

} 

主要員工POJO

package mypack; 

public class Employee 
{ 
     private int eid,salary; 
     private String name,job; 

    public Employee() 
    { 
     super(); 
    } 

    public Employee(String name, String job,int salary) 
    { 
     super(); 
     this.salary = salary; 
     this.name = name; 
     this.job = job; 
    } 

    public int getEid() { 
     return eid; 
    } 
    public void setEid(int eid) { 
     this.eid = eid; 
    } 
    public int getSalary() { 
     return salary; 
    } 
    public void setSalary(int salary) { 
     this.salary = salary; 
    } 
    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 
    public String getJob() { 
     return job; 
    } 
    public void setJob(String job) { 
     this.job = job; 
    } 

} 

主要測試程序..

package mypack; 

import java.util.Scanner; 

import org.hibernate.SessionFactory; 
import org.hibernate.cfg.Configuration; 
import org.hibernate.classic.Session; 

public class TestProgram 
{ 

    public static void main(String[] args) 
    { 
      try 
      { 
       Configuration cfg=new Configuration(); 
       cfg.configure(); 
       SessionFactory sesfac=cfg.buildSessionFactory(); 
       Session ses=sesfac.openSession(); 
       System.out.println("Session created , fetching objects..."); 
       Scanner in=new Scanner(System.in); 
       System.out.println("Enter Employee id :- "); 
       int id=in.nextInt(); 
       Employee e=(Employee)ses.load(Employee.class,id); 
       System.out.println("Following Object is fetched"); 
       System.out.println(e.getEid()+"\t"+e.getName()+"\t"+e.getSalary()+"\t"+e.getJob()); 
       ses.close(); 

      }catch (Exception e) 
      { 
       System.out.println(e); 
      } 
    } 

} 

和我是迪的另一種方法scussing在鏈接another approach to integagrate現在請告訴我哪種方法是最好的,有沒有其他方法更好,然後這兩個..?

請發佈更新的代碼,以便我能理解這個概念,請指教.. !!

+0

@folks請指教,通過在int()方法中自定義篩選器或按照URL中的建議加載hibernate配置,通過證明與數據庫相關的bean與上下文一起加載加載器監聽器.. !! – user1538526 2012-07-31 18:12:21

回答

1

下面是我什麼工作(使用的Struts2,JPA/Hibernate和吉斯):

  • 創建和銷燬在ServletContextListenerEntityManagerFactorySessionFactory;不繼承Struts2的過濾器來做到這一點(也可以考慮使用,而不是舊的較新的StrutsPrepareAndExecuteFilter,棄用FilterDispatcher
  • 創建和銷燬EntityManager S或Session S IN的攔截,使每個請求都有自己單位的-工作;這是一個最佳實踐,因爲這些工作單元不是線程安全的並且不應該在請求之間共享
  • 將攔截器提早定位到堆棧中以確保需要訪問數據庫的任何其他攔截器它;這種採用打開會話中查看格局,這可以確保任何延遲加載的特性是可提取時,你的觀點被渲染(動作已被處理後)

最後,你可能想知道Struts2的,休眠和春天先好一點,然後再把它們全部堆起來。

+0

@ Steven.Thanks請說明如何通過servlet上下文監聽器創建會話工廠,代碼中的一個小例子將使理解更加清晰,並請showhow通過攔截器創建會話,這將是一個很大的幫助。感謝dude – user1538526 2012-07-31 18:35:34

+0

請發佈上述兩個案例的更新代碼將是一個很大的幫助夥計,非常感謝..! – user1538526 2012-07-31 18:39:02