Spring之ORM模块

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会自动维护这些对象

[java]  view plain copy print ?
  1. public interface ICatDao{  
  2.       public void createCat(Cat cat);  
  3.       public List<Cat> listCats();  
  4.       public int getCatsCount();  
  5.       public Cat findCatByName(String name);  
  6. }  
 

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;  
  2. public class CatDaoImpl extends HibernateDaoSupportimplements ICatDao{  
  3.       public void createCat(Cat cat){  
  4.              this.getHibernateTemplate().persist(cat);  
  5.       }  
  6.       public List<Cat> listCats(){  
  7.              return this. getHibernateTemplate().find("select cfrom Cat c");  
  8.       }  
  9.       public int getCatsCount(){  
  10.              Number n = (Number)this.getSession(true).createQuery("selectcount(c) from Cat c").uniqueResult();  
  11.              return n.intValue();  
  12.       }  
  13.       public Cat findCatByName(String name){  
  14.              List<Cat> catList =this.getHibernateTemplate().find("select c from Cat where c.name = ?",name);  
  15.              if(catList.size()>0)  
  16.                     return catList.get(0);  
  17.              return null;  
  18.       }  
  19.    
  20. }  

 

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" destroy-method="destroy">  
  2. <property name="dataSource" ref="dataSource" />  
  3. <!--  该Package下的所有类都会被当做实体类加载-->  
  4. <property name="annotatedPackages" value="classpath:/com/clf/orm" />  
  5. <property name="anotatedClasses">  
  6.       <list>  
  7.              <value>com.clf.spring.orm.Cat</value>  
  8.              <value>com.clf.spring.orm.Dog</value>  
  9.       </list>  
  10. <property name="hibernateProperties">  
  11.       <props>  
  12.              <prop key="hiberante.dialect">org.hibernate.dialect.MySQLDialect</prop>  
  13.              <prop key="hibernate.show_sql">true</prop>  
  14.              <prop key=" hibernate.format_sql">true</prop>  
  15.              <prop key=" hibernate.hbm2ddl.auto">create</prop>  
  16.       </props>  
  17. </property>  
  18.    
  19. <bean id="catDao" class="com.clf.spring.orm.CatDaoImpl">  
  20.       <property name="sessionFactory" ref=" sessionFactory"/>  
  21. </bean>  

 

如果使用XML配置的实体类,则改为

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" destroy-method="destroy">  
  2. <property name="mappingResources">  
  3.       <list>  
  4.              <value>classpath:/com/clf/orm/Cat.hbm.xml</value>  
  5.       </list>  
  6. </property>  
  7. ……  
  8. </bean>  
 

Spring默认在DAO层添加事务,DAO层的每个方法为一个事务。Spring+Hibernate编程中,习惯的做法实在DAO层上再添加一个Service层,然后把事务配置在Service层

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. public interface ICatService{  
  2.       public void createCat(Cat cat);  
  3.       public List<Cat> listCats();   
  4.       public int getCatsCount();  
  5. }  

分层的做法是,程序调用Service层,Service层调用DAO层,DAO层调用Hibernate实现数据访问,原则上不允许跨曾访问。分层使业务层次更加清晰

 

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. public class CatServiceImpl implements ICatService{  
  2.       private IDao catDao;  
  3.       public IDao getCatDao(){  
  4.              return catDao;  
  5.       }  
  6.    
  7.       public void setCatDao(IDao dao){  
  8.              this.catDao = dao;  
  9.       }  
  10.        
  11.       public void createCat(Cat cat){  
  12.              catDao.createCat(cat);  
  13.       }  
  14.       public List<Cat> listCats(){  
  15.              return catDao.listCats();  
  16.       }  
  17.       public int getCatsCount(){  
  18.              return catDao.getCatsCount();  
  19.       }  
  20.    
  21. }  

 

然后再Service层配置事务管理

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <!--  事务管理器-->  
  2. <bean id="hibernateTransactionManager" class=" org.springframework.orm.hibernate3.HibernateTransactionManager  
  3. ">  
  4.       <property name="sessionFactory" ref="sessionFactory"/>  
  5. </bean>  
  6.    
  7. <!--  事务管理规则-->  
  8. <bean id="hibernateTransactionAttributeSource" class=" org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">  
  9.       <property name="properties">  
  10.              <props><!--  为所有方法加上事务-->  
  11.                     <propkeypropkey="*">PROPGATION_REQUIRED</prop>  
  12.       </property>  
  13. </bean>  
  14.    
  15. <!--  事务工厂代理类,将Service实现类、事务管理器、事务管理规则组装在一起-->  
  16. <bean id="catService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">  
  17.       <property name="transactionManager" ref=" hibernateTransactionManager">  
  18.       <property name="target">  
  19.              <bean class="com.clf.spring.orm.CatServiceImpl" >  
  20.                     <property name="catDao" ref="catDao"/>  
  21.              </bean>  
  22.       </property>  
  23.       <property name="transactionAttributeSource" ref=" hibernateTransactionAttributeSource" />  
  24. </bean>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值