溫馨提示×

溫馨提示×

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

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

spring結(jié)合hibernate示例詳解

發(fā)布時間:2020-09-08 16:50:05 來源:腳本之家 閱讀:140 作者:動力節(jié)點(diǎn) 欄目:編程語言

單純Hibernate程序

1、首先是導(dǎo)入hibernate的jar包。

2、 建立用戶和用戶操作記錄實(shí)體,Log.Java和User.java。代碼如下所示。

Log.java

import java.util.Date; 
public class Log { 
  private int id; 
  //日志的類別.日志一般起到一個不可否認(rèn)性. 
  //操作日志 安全日志 事件日志. 
  private String type; 
  private String detail; 
  private Date time; 
  public int getId() { 
    return id; 
  } 
  public void setId(int id) { 
    this.id = id; 
  } 
  public String getType() { 
    return type; 
  } 
  public void setType(String type) { 
    this.type = type; 
  } 
  public String getDetail() { 
    return detail; 
  } 
  public void setDetail(String detail) { 
    this.detail = detail; 
  } 
  public Date getTime() { 
    return time; 
  } 
  public void setTime(Date time) { 
    this.time = time; 
  } 
} 

User.java

public class User { 
  private int id; 
  private String name; 
  public int getId() { 
    return id; 
  } 
  public void setId(int id) { 
    this.id = id; 
  } 
  public String getName() { 
    return name; 
  } 
  public void setName(String name) { 
    this.name = name; 
  } 
} 

3、 并建立與之對應(yīng)的實(shí)體配置文件,Log.hbm.xml和Use.hbm.xml。代碼如下所示。

<?xml version="1.0"?> 
<!DOCTYPE hibernate-mapping PUBLIC  
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
<hibernate-mapping> 
  <class name="com.bjpowernode.usermgr.domain.User" table="t_user"> 
    <id name="id" > 
      <generator class="native"/> 
    </id> 
    <property name="name" /> 
  </class> 
</hibernate-mapping> 

Log.hbm.xml

<?xml version="1.0"?> 
<!DOCTYPE hibernate-mapping PUBLIC  
  "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
<hibernate-mapping> 
  <class name="com.bjpowernode.usermgr.domain.Log" table="t_log"> 
    <id name="id" > 
      <generator class="native"/> 
    </id> 
    <property name="type" /> 
    <property name="detail" /> 
    <property name="time" /> 
  </class> 
</hibernate-mapping> 

4、Manager層代碼如下所示。

LogManager.java接口

public interface LogManager { 
  //添加日志.方法 
  public void addLog(Log log); 
} 

LogManagerImpl實(shí)現(xiàn)

public class LogManagerImpl implements LogManager { 
  @Override 
  public void addLog(Log log) {  
    HibernateUtils.getSessionFactory().getCurrentSession().save(log); 
     
  } 
}

UserManager接口

public interface UserManager { 
 
  public void addUser(User user); 
} 

UserManagerImpl.java實(shí)現(xiàn)

public class UserManagerImpl implements UserManager { 
  @Override 
  public void addUser(User user) { 
    Session session = null; 
    try { 
      //這個session中是放到threadlocal. 
      session = HibernateUtils.getSessionFactory().getCurrentSession(); 
      session.beginTransaction(); 
      // 網(wǎng)用戶表中添加一條同時網(wǎng)日志中添加一條. 
      session.save(user); 
      Log log = new Log(); 
      log.setType("操作日志"); 
      log.setTime(new Date()); 
      log.setDetail("xxx"); 
      LogManager logManager = new LogManagerImpl(); 
      //添加日志. 
      logManager.addLog(log); 
      session.getTransaction().commit(); 
    } catch (Exception e) { 
      e.printStackTrace(); 
      session.getTransaction().rollback(); 
    }finally{ 
      HibernateUtils.closeSession(session); 
    } 
  } 
} 

5、是通過sessionFactory來創(chuàng)建session,通過session來開啟提交和關(guān)閉回滾事務(wù),我們把session的開啟關(guān)閉封裝到一個工具類中。HibernateUtils.java代碼如下所示。

public class HibernateUtils { 
  private static SessionFactory factory; 
  static { 
    try { 
      //讀取hibernate.cfg.xml文件 
      Configuration cfg = new Configuration().configure(); 
      //建立SessionFactory 
      factory = cfg.buildSessionFactory(); 
    }catch(Exception e) { 
      e.printStackTrace(); 
    } 
  } 
  public static Session getSession() { 
    return factory.openSession(); 
  }  
  public static void closeSession(Session session) { 
    if (session != null) { 
      if (session.isOpen()) { 
        session.close(); 
      } 
    } 
  } 
  public static SessionFactory getSessionFactory() { 
    return factory; 
  } 
} 

6、配置hibernate.cfg.xml文件,包括數(shù)據(jù)庫名稱,數(shù)據(jù)庫關(guān)聯(lián)的表,以及用戶名密碼等。代碼如下所示。

<hibernate-configuration> 
  <session-factory> 
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/spring_hibernate_1</property> 
    <property name="hibernate.connection.username">root</property> 
    <property name="hibernate.connection.password">root</property> 
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 
    <property name="hibernate.show_sql">true</property> 
    <property name="hibernate.hbm2ddl.auto">update</property> 
    <property name="hibernate.current_session_context_class">thread</property> 
    <!--  
    <property name="hibernate.current_session_context_class">jta</property> 
    --> 
    <mapping resource="com/bjpowernode/usermgr/domain/User.hbm.xml"/> 
    <mapping resource="com/bjpowernode/usermgr/domain/Log.hbm.xml"/> 
  </session-factory> 
</hibernate-configuration> 

7、 使用junit進(jìn)行單元測試,代碼如下所示。

import junit.framework.TestCase; 
public class UserManagerImplTest extends TestCase { 
  public void testAddUser() { 
    UserManager userManager = new UserManagerImpl(); 
    User user = new User(); 
    user.setName("張三"); 
    userManager.addUser(user); 
  } 
} 

在上述操作用,對事物的控制邊界在業(yè)務(wù)邏輯層,因?yàn)樵赨serManagerImpl中我們調(diào)用addUser()這一方法的同時要把這一操作寫入到日志中,也就是調(diào)用了addLog()方法,而對于類的方法,一方法一session,一session一事務(wù),那么如果控制事務(wù)的呢?我們要執(zhí)行開啟addUser()事務(wù)同時再開啟addLog()事務(wù)嗎?在這里我們沒有再用以前的openSession方法,選擇用的HibernateUtils.getSessionFactory().getCurrentSession();同時在hibernate.cfg.xml中對getCurrentSession()進(jìn)行配置如下,       <propertyname="hibernate.current_session_context_class">thread</property>表示在當(dāng)前線程中,與當(dāng)前線程綁定這樣執(zhí)行addUser()方法和addLog()方法使用的是同一個事務(wù)。

那OpenSession和getCurrentSession的區(qū)別呢?

1、          openSession必須關(guān)閉,currentSession在事務(wù)結(jié)束后自動關(guān)閉。

2、          openSession沒有和當(dāng)前線程綁定,currentSession和當(dāng)前線程綁定。并且使用currentSession需要在我們的hibernate.cfg.xml文件中進(jìn)行事務(wù)的配置,是使用Jdbc事務(wù)還是JTA事務(wù)。

hibernate和spring結(jié)合使用

我們從上述例子中發(fā)現(xiàn),hibernate的事務(wù)是獨(dú)立于hibernate對數(shù)據(jù)庫的增刪改查的,并且事務(wù)控制在我們的業(yè)務(wù)邏輯層,對于獨(dú)立的東西,像是橫切性問題,自然想到了AOP,實(shí)際上SpringAOP封裝了對事務(wù)的管理,使用SpringAOP我們不再負(fù)責(zé)去開啟和關(guān)閉事務(wù)。

下面用SpringAOP來和hibernate結(jié)合。

當(dāng)然也要導(dǎo)入Spring相關(guān)jar。

對于事務(wù)管理就是一個橫切性問題,把事務(wù)管理模塊化就是我們的aspect,然后再配置文件中進(jìn)行配置,我們可以把事務(wù)單獨(dú)放到一個配置文件中。

1、代碼如下,applicationContext-common.xml文件。

<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:aop="http://www.springframework.org/schema/aop" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd 
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd 
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> 
  <!-- 配置SessionFactoyr --> 
  <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
    <property name="configLocation"> 
      <value>classpath:hibernate.cfg.xml</value> 
    </property> 
  </bean> 
  <!-- 配置事務(wù)管理器 --> 
  <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
    <property name="sessionFactory"> 
      <ref bean="sessionFactory"/> 
    </property> 
  </bean> 
<!-- 哪些類哪些方法使用事務(wù). -->  
<aop:config> 
  <aop:pointcut id="allManagerMethod" expression="execution(* com.bjpowernode.usermgr.manager.*.*(..))"/> 
  <aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/> 
</aop:config> 
<tx:advice id="txAdvice" transaction-manager="transactionManager"> 
  <tx:attributes> 
    <tx:method name="add*" propagation="REQUIRED"/> 
    <tx:method name="del*" propagation="REQUIRED"/> 
    <tx:method name="modify*" propagation="REQUIRED"/> 
    <tx:method name="*" propagation="REQUIRED" read-only="true"/> 
  </tx:attributes> 
</tx:advice> 
</beans> 

首先是配置的是sessionFactory,讓spring拿到hibernate的sessionFactory,以便對hibernate事務(wù)控制,通過sessionFactory產(chǎn)生session再訪問。這樣就把把hibernate的sessionFactory注入到Spring中了。通過<property>標(biāo)簽,告訴SpringHibernate的配置文件在哪里,以便spring可以讀取hibernate的配置文件。
其次是配置事務(wù)管理器,把我們的sessionFactory注入給事務(wù)管理器,讓事務(wù)管理器管理事務(wù)。

其次,到底是哪些類哪些方法,開始執(zhí)行的時候執(zhí)行事務(wù),<aop:pointcutid="allManagerMethod"  

其次,配置AOP,expression="execution(*com.bjpowernode.usermgr.manager.*.*(..))",到底是哪些類交給spring完成事務(wù)管理?我們應(yīng)用在所有的manager包中的所有方法上,manager所有類所有方法全部參與事務(wù)的運(yùn)行。那在什么地方觸發(fā)開啟事務(wù)?

再次,定義一個Advice,配置事務(wù)的傳播特性,例如addUser()中調(diào)用addLog()方法,是在同一個事務(wù)還是不在同一個事務(wù)。以add開頭,del開頭,modify開頭以及其他,我們配置的事務(wù)傳播特性為propagation="REQUIRED",這樣在一個方法中調(diào)用另一個方法他們公共一個線程。

2、讓UserManagerImpl繼承spring提供的對hibernateDao支持類。

HibernateDaoSupport,這樣繼承之后我們就能拿到session,其實(shí)也就是hibernate中的session,只不過spring為我們封裝了。我們可以這樣拿到sesion:This.getSession().save(user);或者使用spring封裝好的對象:

This.getHibernateTemplate().save(user);這樣都封裝到里面了,我們不管理事務(wù)的開啟和關(guān)閉。

之前在我們的UserManagerImpl中使用了LogManagerImpl實(shí)例,這次我們可以使用Spring的IOC容器,把他們之間的依賴關(guān)系注入到Spring中,這樣就看不到實(shí)例,面對接口編程,進(jìn)行了解耦。

接口不變,UserManagerImpl.java代碼如下所示。

public class UserManagerImpl extends HibernateDaoSupport implements UserManager { 
  private LogManager logManager; 
  public void setLogManager(LogManager logManager) 
  { 
    this.logManager = logManager; 
  } 
  @Override 
  public void addUser(User user)throws Exception { 
    //this.getSession().save(user); 
        //或者用. 
        this.getHibernateTemplate().save(user); 
        Log log = new Log(); 
        log.setType("操作日志"); 
        log.setTime(new Date()); 
        log.setDetail("xxx"); 
        //LogManager logManager = new LogManagerImpl(); 
        //添加日志. 
        logManager.addLog(log);        
        //運(yùn)行期的異常,會回滾. 并且是他的子類也會回滾. 
        //throw new RuntimeException(); 
        //throw new Exception(); 
  } 
} 

LogManagerImpl.java 代碼如下所示。

public class LogManagerImpl extends HibernateDaoSupport implements LogManager { 
  @Override 
  public void addLog(Log log) { 
    //getSession().save(log); 
    this.getHibernateTemplate().save(log); 
     
  } 
} 

刪除我們自己建立的HibernateUtils.java類,刪除hibernate.cfg.xml文件中對getCurrentSession()的事務(wù)配置。

3、在配置文件中配置依賴關(guān)系。

applicationContext-beans.xml代碼如下所示。

<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:aop="http://www.springframework.org/schema/aop" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd 
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd 
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> 
  <bean id="userManager" class="com.bjpowernode.usermgr.manager.UserManagerImpl"> 
    <property name="sessionFactory" ref="sessionFactory"/> 
    <property name="logManager" ref="logManager"/> 
  </bean> 
  <bean id="logManager" class="com.bjpowernode.usermgr.manager.LogManagerImpl"> 
    <property name="sessionFactory" ref="sessionFactory"/> 
  </bean> 
</beans> 

在Junit中測試程序代碼如下所示。

public class UserManagerImplTest extends TestCase { 
  public void testAddUser() { 
    BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext-*.xml");  
    UserManager userManager = (UserManager) factory.getBean("userManager"); 
    User user = new User(); 
    user.setName("張三"); 
    try { 
      userManager.addUser(user); 
    } catch (Exception e) { 
      e.printStackTrace(); 
    } 
  } 
} 

顯示結(jié)果如下圖所示。

spring結(jié)合hibernate示例詳解

    這樣就完成了spring和hibernate的結(jié)合,主要是利用SpringAOP對hibernate的事務(wù)進(jìn)行控制和在Manager層之間的調(diào)用用Spring IOC進(jìn)行控制。

總結(jié)

以上所述是小編給大家介紹的spring結(jié)合hibernate示例詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對億速云網(wǎng)站的支持!

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

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

AI