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>

Spring之ORM模块的更多相关文章

  1. spring的orm模块

    spring整合hibernate 1.hibernate使用注解. daoImpl需要继承HibernateDaoSupport对象,针对给对象的getHibernateTemplate()进行hi ...

  2. 1.Spring——七大主要模块

    Spring有七大功能模块,分别是Spring Core,AOP,ORM,DAO,MVC,WEB,Content. 下面分别简单介绍: 1.Spring Core Core模块是Spring的核心类库 ...

  3. spring的核心模块有哪些?

    Spring的七个核心模块,供大家参考,具体内容如下 1.Spring core:核心容器 核心容器提供spring框架的基本功能.Spring以bean的方式组织和管理Java应用中的各个组件及其关 ...

  4. Spring之WEB模块

    Spring的WEB模块用于整合Web框架,例如Struts 1.Struts 2.JSF等 整合Struts 1 继承方式 Spring框架提供了ActionSupport类支持Struts 1的A ...

  5. [02] Spring主要功能模块概述

    1.Spring主要功能模块   1.1 Core Container Spring的核心容器模块,其中包括: Beans Core Context SpEL Beans和Core模块,是框架的基础部 ...

  6. 一头扎进Spring之---------Spring七大核心模块

    Spring七大核心模块 核心容器(Spring Core) 核心容器提供Spring框架的基本功能.Spring以bean的方式组织和管理Java应用中的各个组件及其关系.Spring使用BeanF ...

  7. Spring中各个模块

    Spring中个模块介绍 核心模块 ​ 提供了对Bean的配置.管理.创建等以及IOC/DI.如果只需要提供IOC/DI功能,只需要引入Beans和Core两个jar包 Core 包含Spring框架 ...

  8. Spring Framework------>version4.3.5.RELAESE----->Reference Documentation学习心得----->Spring Framework中的spring web MVC模块

    spring framework中的spring web MVC模块 1.概述 spring web mvc是spring框架中的一个模块 spring web mvc实现了web的MVC架构模式,可 ...

  9. Spring第七篇【Spring的JDBC模块】

    前言 上一篇Spring博文主要讲解了如何使用Spring来实现AOP编程,本博文主要讲解Spring的对JDBC的支持- 对于JDBC而言,我们肯定不会陌生,我们在初学的时候肯定写过非常非常多的JD ...

随机推荐

  1. [LeetCode] Top K Frequent Words 前K个高频词

    Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted b ...

  2. 微信小程序开发•模块化

    微信小程序的MINA框架,其实是许多前端开发技术的组合.这篇文章中,我们来简单地讨论一下模块化. 1.模块化标准 玩前端的同学大部分都知道模块化的几个标准,CommonJs / AMD / CMD.这 ...

  3. jQuery滚动指定位置

    $(document).ready(function() { $("#scroll").click(function() { $('html, body').animate({ s ...

  4. bzoj 2436: [Noi2011]Noi嘉年华

    Description NOI2011 在吉林大学开始啦!为了迎接来自全国各地最优秀的信息学选手,吉林大学决定举办两场盛大的 NOI 嘉年华活动,分在两个不同的地点举办.每个嘉年华可能包含很多个活动, ...

  5. 【bzoj4011 hnoi2015】落忆枫音

    题目描述 「恒逸,你相信灵魂的存在吗?」 郭恒逸和姚枫茜漫步在枫音乡的街道上.望着漫天飞舞的红枫,枫茜突然问出这样一个问题. 「相信吧.不然我们是什么,一团肉吗?要不是有灵魂......我们也不可能再 ...

  6. Orz

    OR: 说实话,感觉Virtual Judge挺好使的,至少到现在,Uva都没注册成功过QAQ,估计是校园网的问题 不得不说现在课越来越多,而且对于我们这种学校ACM才开展两年的来说,时间真的好有限, ...

  7. Nginx+uWSGI+Django环境配置

    通常项目会部署在虚拟环境,虚拟环境的使用可以参考这里,点击前往 当然你也可以直接部署,这里不多说. 一.安装uWSGI 1.通过pip安装 pip install uwsgi 这里只说明了一种安装方式 ...

  8. Qone 正式开源,使 javascript 支持 .NET LINQ

    Qone 下一代 Web 查询语言,使 javascript 支持 LINQ Github: https://github.com/dntzhang/qone 缘由 最近刚好修改了腾讯文档 Excel ...

  9. jquery easyui datagrid设置行样式 不可删除某行

    rowStyler: function (index,row) { if (parseInt(row.ksrs) > 0) { return 'color:red'; } }, onLoadSu ...

  10. Python学习---字符串处理

    This world is but a canvas to our imagination. 世界只是我们的想象的画布. ----Apri 22 ''' 题目内容: "Pig Latin&q ...