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] Design TinyURL 设计精简URL地址

    Note: For the coding companion problem, please see: Encode and Decode TinyURL. How would you design ...

  2. js在光标处插入内容

    //场景一 简易的页面可以这样写var range = window.getSelection().getRangeAt(0);range.insertNode(document.createText ...

  3. Discuz!另一处SSRF无须登陆无须条件

    漏洞来源:http://wooyun.jozxing.cc/static/bugs/wooyun-2015-0151179.html 看看poc:http://phpstudy.com/Discuz_ ...

  4. windows版本的phantomjs-2.1.1-windows安装

    windows版本的phantomjs-2.1.1-windows安装 1.下载 链接:http://pan.baidu.com/s/1dEUl6dN 密码:oij8 2.安装 下载好之后解压放到某个 ...

  5. 计蒜客NOIP2017提高组模拟赛(五)day1-机智的 AmyZhi

    传送门 很水的题目啦QAQ #include<cstdio> #include<cstdlib> #include<algorithm> #include<c ...

  6. 【USACO Mar08】 奶牛跑步 A-star k短路

    Description Bessie准备用从牛棚跑到池塘的方法来锻炼. 但是因为她懒,她只准备沿着下坡的路跑到池塘,然后走回牛棚. Bessie也不想跑得太远,所以她想走最短的路经. 农场上一共有M( ...

  7. bzoj3038上帝造题的七分钟2

    3038: 上帝造题的七分钟2 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 1679  Solved: 713[Submit][Status][Dis ...

  8. redis启动失败

    redis.conf 设置的daemonize yes后台运行,使用redis-server redis.conf之后没有任何反应,以为启动成功 使用 ps -ef|grep redis 查看redi ...

  9. Linux系统之TroubleShooting(故障排除)(转)

    尽管Linux系统非常强大,稳定,但是我们在使用过程当中,如果人为操作不当,仍然会影响系统,甚至可能使得系统无法开机,无法运行服务等等各种问题.那么这篇博文就总结一下一些常见的故障排除方法,但是不可能 ...

  10. 背景重复样式background-repeat

    一.background-repeat属性 在CSS中,使用background-repeat属性可以设置背景图像是否平铺,并且可以设置如何平铺. 语法: background-repeat:取值; ...