当前位置:   article > 正文

Spring之ORM模块_springorm模块

springorm模块

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

 

  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. }
 

 

 

  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. }

 

 

 

  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. <bean id="catDao" class="com.clf.spring.orm.CatDaoImpl">
  19. <property name="sessionFactory" ref=" sessionFactory"/>
  20. </bean>
 

 

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

 

  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层

 

  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实现数据访问,原则上不允许跨曾访问。分层使业务层次更加清晰

 

 

  1. public class CatServiceImpl implements ICatService{
  2. private IDao catDao;
  3. public IDao getCatDao(){
  4. return catDao;
  5. }
  6. public void setCatDao(IDao dao){
  7. this.catDao = dao;
  8. }
  9. public void createCat(Cat cat){
  10. catDao.createCat(cat);
  11. }
  12. public List<Cat> listCats(){
  13. return catDao.listCats();
  14. }
  15. public int getCatsCount(){
  16. return catDao.getCatsCount();
  17. }
  18. }
 

 

然后再Service层配置事务管理

 

  1. <!-- 事务管理器-->
  2. <bean id="hibernateTransactionManager" class=" org.springframework.orm.hibernate3.HibernateTransactionManager
  3. ">
  4. <property name="sessionFactory" ref="sessionFactory"/>
  5. </bean>
  6. <!-- 事务管理规则-->
  7. <bean id="hibernateTransactionAttributeSource" class=" org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
  8. <property name="properties">
  9. <props><!-- 为所有方法加上事务-->
  10. <propkey="*">PROPGATION_REQUIRED</prop>
  11. </property>
  12. </bean>
  13. <!-- 事务工厂代理类,将Service实现类、事务管理器、事务管理规则组装在一起-->
  14. <bean id="catService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
  15. <property name="transactionManager" ref=" hibernateTransactionManager">
  16. <property name="target">
  17. <bean class="com.clf.spring.orm.CatServiceImpl" >
  18. <property name="catDao" ref="catDao"/>
  19. </bean>
  20. </property>
  21. <property name="transactionAttributeSource" ref=" hibernateTransactionAttributeSource" />
  22. </bean>
 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/327851?site
推荐阅读
相关标签
  

闽ICP备14008679号