首頁  >  文章  >  Java  >  Spring之ORM模組程式碼詳解

Spring之ORM模組程式碼詳解

小云云
小云云原創
2017-12-05 11:40:502086瀏覽

ORM物件關係映射,是一種程式技術,用於實現物件導向程式語言裡不同類型系統的資料之間的轉換 。從效果上來說,它其實是創建了一個可在程式語言裡使用的--"虛擬物件資料庫"。這篇文章主要介紹了Spring之ORM模組程式碼詳解,具有一定參考價值,需要的朋友可以了解下。

Spring框架七大模組簡單介紹

Spring中MVC模組程式碼詳解

ORM模組對Hibernate、JDO、 TopLinkiBatis等ORM框架提供支援

ORM模組依賴dom4j.jar、antlr.jar等套件

在Spring裡,Hibernate的資源要交給Spring管理,Hibernate以及其SessionFactory等知識Spring一個特殊的Bean,有Spring負責實例化與銷毀。因此DAO層只需要繼承HibernateDaoSupport,而不需要與Hibernate的API打交道,不需要開啟、關閉Hibernate的Session、Transaction,Spring會自動維護這些物件

#
public interface ICatDao{ 
   public void createCat(Cat cat); 
   public List<Cat> listCats(); 
   public int getCatsCount(); 
   public Cat findCatByName(String name); 
}

import org.springframework.orm.hibernate3.support.HibernateDaoSupport; 
public class CatDaoImpl extends HibernateDaoSupportimplements ICatDao{ 
   public void createCat(Cat cat){ 
       this.getHibernateTemplate().persist(cat); 
   } 
   public List<Cat> listCats(){ 
       return this. getHibernateTemplate().find("select cfrom Cat c"); 
   } 
   public int getCatsCount(){ 
       Number n = (Number)this.getSession(true).createQuery("selectcount(c) from Cat c").uniqueResult(); 
       return n.intValue(); 
   } 
   public Cat findCatByName(String name){ 
       List<Cat> catList =this.getHibernateTemplate().find("select c from Cat where c.name = ?",name); 
       if(catList.size()>0) 
          return catList.get(0); 
       return null; 
   } 
  
}

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" destroy-method="destroy"> 
<property name="dataSource" ref="dataSource" /> 
<!-- 该Package下的所有类都会被当做实体类加载--> 
<property name="annotatedPackages" value="classpath:/com/clf/orm" /> 
<property name="anotatedClasses"> 
   <list> 
       <value>com.clf.spring.orm.Cat</value> 
       <value>com.clf.spring.orm.Dog</value> 
   </list> 
<property name="hibernateProperties"> 
   <props> 
       <prop key="hiberante.dialect">org.hibernate.dialect.MySQLDialect</prop> 
       <prop key="hibernate.show_sql">true</prop> 
       <prop key=" hibernate.format_sql">true</prop> 
       <prop key=" hibernate.hbm2ddl.auto">create</prop> 
   </props> 
</property> 
  
<bean id="catDao" class="com.clf.spring.orm.CatDaoImpl"> 
   <property name="sessionFactory" ref=" sessionFactory"/> 
</bean>

#如果使用XML配置的實體類,則改為

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" destroy-method="destroy"> 
<property name="mappingResources"> 
   <list> 
       <value>classpath:/com/clf/orm/Cat.hbm.xml</value> 
   </list> 
</property> 
…… 
</bean>

Spring預設在DAO層新增事務,DAO層的每個方法為一個事務。 Spring+Hibernate程式設計中,習慣的做法實在DAO層上再增加一個Service層,然後把交易配置在Service層

public interface ICatService{ 
   public void createCat(Cat cat); 
   public List<Cat> listCats();  
   public int getCatsCount(); 
}

分層的做法是,程式呼叫Service層,Service層呼叫DAO層,DAO層呼叫Hibernate實作資料訪問,原則上不允許跨曾訪問。分層讓業務層級更清楚

public class CatServiceImpl implements ICatService{ 
   private IDao catDao; 
   public IDao getCatDao(){ 
       return catDao; 
   } 
  
   public void setCatDao(IDao dao){ 
       this.catDao = dao; 
   } 
    
   public void createCat(Cat cat){ 
       catDao.createCat(cat); 
   } 
   public List<Cat> listCats(){ 
       return catDao.listCats(); 
   } 
   public int getCatsCount(){ 
       return catDao.getCatsCount(); 
   } 
  
}

然後再Service層設定事務管理

<!-- 事务管理器--> 
<bean id="hibernateTransactionManager" class=" org.springframework.orm.hibernate3.HibernateTransactionManager 
"> 
   <property name="sessionFactory" ref="sessionFactory"/> 
</bean> 
  
<!-- 事务管理规则--> 
<bean id="hibernateTransactionAttributeSource" class=" org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource"> 
   <property name="properties"> 
       <props><!-- 为所有方法加上事务--> 
          <propkeypropkey="*">PROPGATION_REQUIRED</prop> 
   </property> 
</bean> 
  
<!-- 事务工厂代理类,将Service实现类、事务管理器、事务管理规则组装在一起--> 
<bean id="catService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> 
   <property name="transactionManager" ref=" hibernateTransactionManager"> 
   <property name="target"> 
       <bean class="com.clf.spring.orm.CatServiceImpl" > 
          <property name="catDao" ref="catDao"/> 
       </bean> 
   </property> 
   <property name="transactionAttributeSource" ref=" hibernateTransactionAttributeSource" /> 
</bean>

#然後再Service層設定事務管理

rrreee

以上內容就是Spring之ORM模組程式碼詳解,希望能幫助大家。

相關推薦:

Java框架中spring框架的優點

Java中關於SpringAop的詳解

#### #####對Spring MVC進行簡單介紹######

以上是Spring之ORM模組程式碼詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn