Spring之ORM模組__Spring

來源:互聯網
上載者:User

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><!--  為所有方法加上事務-->                    <propkey="*">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>


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.