欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring之ORM模塊代碼詳解

 更新時(shí)間:2017年12月04日 14:55:27   作者:冰河winner  
這篇文章主要介紹了Spring之ORM模塊代碼詳解,具有一定參考價(jià)值,需要的朋友可以了解下。

Spring框架七大模塊簡單介紹

Spring中MVC模塊代碼詳解

ORM模塊對Hibernate、JDO、TopLinkiBatis等ORM框架提供支持

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

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

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下的所有類都會被當(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模塊代碼詳解的全部內(nèi)容,希望大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!

相關(guān)文章

  • 解決idea2020 maven無法自動導(dǎo)包的問題

    解決idea2020 maven無法自動導(dǎo)包的問題

    這篇文章主要介紹了解決idea2020 maven無法自動導(dǎo)包的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • spring動態(tài)控制定時(shí)任務(wù)的實(shí)現(xiàn)

    spring動態(tài)控制定時(shí)任務(wù)的實(shí)現(xiàn)

    在實(shí)際項(xiàng)目中,經(jīng)常需要動態(tài)的控制定時(shí)任務(wù),比如通過接口增加、啟動、停止、刪除定時(shí)任務(wù),本文主要介紹了spring動態(tài)控制定時(shí)任務(wù)的實(shí)現(xiàn),感興趣的可以了解一下
    2024-01-01
  • Java StringBuilder和StringBuffer源碼分析

    Java StringBuilder和StringBuffer源碼分析

    這篇文章主要針對Java中兩個(gè)常用的操作字符串的類 StringBuilder和StringBuffer進(jìn)行源碼分析,感興趣的小伙伴們可以參考一下
    2016-01-01
  • 詳解Java的Hibernate框架中的set映射集與SortedSet映射

    詳解Java的Hibernate框架中的set映射集與SortedSet映射

    這篇文章主要介紹了詳解Java的Hibernate框架中的set映射集與SortedSet映射,Hibernate是Java的SSH三大web開發(fā)框架之一,需要的朋友可以參考下
    2015-12-12
  • java實(shí)現(xiàn)附件預(yù)覽(openoffice+swftools+flexpaper)實(shí)例

    java實(shí)現(xiàn)附件預(yù)覽(openoffice+swftools+flexpaper)實(shí)例

    本篇文章主要介紹了java實(shí)現(xiàn)附件預(yù)覽(openoffice+swftools+flexpaper)實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2016-10-10
  • SpringCloud中的Ribbon負(fù)載均衡器詳細(xì)解析

    SpringCloud中的Ribbon負(fù)載均衡器詳細(xì)解析

    這篇文章主要介紹了SpringCloud中的Ribbon負(fù)載均衡器詳細(xì)解析,Ribbon 是一個(gè)基于 HTTP 和 TCP 的客戶端負(fù)載均衡工具,它基于 Netflix Ribbon 實(shí)現(xiàn),通過封裝可以讓我們輕松地將面向服務(wù)的 REST 模版請求自動轉(zhuǎn)換成客戶端負(fù)載均衡的服務(wù)調(diào)用,需要的朋友可以參考下
    2024-01-01
  • Spring Boot中使用RabbitMQ的示例代碼

    Spring Boot中使用RabbitMQ的示例代碼

    本篇文章主要介紹了Spring Boot中使用RabbitMQ的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-04-04
  • Java中Sentinel框架詳解

    Java中Sentinel框架詳解

    Sentinel是一個(gè)高可用、高擴(kuò)展、高穩(wěn)定性的開源流量控制和熔斷降級框架,可以在分布式系統(tǒng)中實(shí)現(xiàn)實(shí)時(shí)的流量控制,防止系統(tǒng)因流量過大導(dǎo)致系統(tǒng)崩潰和服務(wù)降級,Sentinel面向所有的Java應(yīng)用,本文就給大家詳細(xì)介紹一下Java中Sentinel框架,需要的朋友可以參考下
    2023-06-06
  • Spring AOP實(shí)現(xiàn)權(quán)限檢查的功能

    Spring AOP實(shí)現(xiàn)權(quán)限檢查的功能

    這篇文章主要介紹了Spring AOP實(shí)現(xiàn)權(quán)限檢查的功能,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • Java 數(shù)組差集實(shí)例代碼

    Java 數(shù)組差集實(shí)例代碼

    這篇文章主要介紹了Java 數(shù)組差集實(shí)例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09

最新評論