Spring之ORM模塊代碼詳解

Spring框架七大模塊簡(jiǎn)單介紹

目前成都創(chuàng)新互聯(lián)已為上1000+的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管網(wǎng)站托管、企業(yè)網(wǎng)站設(shè)計(jì)、津市網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。

Spring中MVC模塊代碼詳解

ORM模塊對(duì)Hibernate、JDO、TopLinkiBatis等ORM框架提供支持

ORM模塊依賴于dom4j.jar、antlr.jar等包

在Spring里,Hibernate的資源要交給Spring管理,Hibernate以及其SessionFactory等知識(shí)Spring一個(gè)特殊的Bean,有Spring負(fù)責(zé)實(shí)例化與銷毀。因此DAO層只需要繼承HibernateDaoSupport,而不需要與Hibernate的API打交道,不需要開啟、關(guān)閉Hibernate的Session、Transaction,Spring會(huì)自動(dòng)維護(hù)這些對(duì)象

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" /> 
<!-- 該P(yáng)ackage下的所有類都會(huì)被當(dāng)做實(shí)體類加載--> 
<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配置的實(shí)體類,則改為

<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默認(rèn)在DAO層添加事務(wù),DAO層的每個(gè)方法為一個(gè)事務(wù)。Spring+Hibernate編程中,習(xí)慣的做法實(shí)在DAO層上再添加一個(gè)Service層,然后把事務(wù)配置在Service層

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

分層的做法是,程序調(diào)用Service層,Service層調(diào)用DAO層,DAO層調(diào)用Hibernate實(shí)現(xiàn)數(shù)據(jù)訪問,原則上不允許跨曾訪問。分層使業(yè)務(wù)層次更加清晰

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層配置事務(wù)管理

<!-- 事務(wù)管理器--> 
<bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager 
"> 
   <property name="sessionFactory" ref="sessionFactory"/> 
</bean> 
  
<!-- 事務(wù)管理規(guī)則--> 
<bean id="hibernateTransactionAttributeSource" class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource"> 
   <property name="properties"> 
       <props><!-- 為所有方法加上事務(wù)--> 
          <propkeypropkey="*">PROPGATION_REQUIRED</prop> 
   </property> 
</bean> 
  
<!-- 事務(wù)工廠代理類,將Service實(shí)現(xiàn)類、事務(wù)管理器、事務(wù)管理規(guī)則組裝在一起--> 
<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> 

總結(jié)

以上就是本文關(guān)于Spring之ORM模塊代碼詳解的全部?jī)?nèi)容,希望大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!

網(wǎng)頁(yè)名稱:Spring之ORM模塊代碼詳解
網(wǎng)站路徑:http://muchs.cn/article20/ihidjo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、網(wǎng)頁(yè)設(shè)計(jì)公司、響應(yīng)式網(wǎng)站企業(yè)網(wǎng)站制作、網(wǎng)站收錄、網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司