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会自动维护这些对象
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模块的更多相关文章
- spring的orm模块
spring整合hibernate 1.hibernate使用注解. daoImpl需要继承HibernateDaoSupport对象,针对给对象的getHibernateTemplate()进行hi ...
- 1.Spring——七大主要模块
Spring有七大功能模块,分别是Spring Core,AOP,ORM,DAO,MVC,WEB,Content. 下面分别简单介绍: 1.Spring Core Core模块是Spring的核心类库 ...
- spring的核心模块有哪些?
Spring的七个核心模块,供大家参考,具体内容如下 1.Spring core:核心容器 核心容器提供spring框架的基本功能.Spring以bean的方式组织和管理Java应用中的各个组件及其关 ...
- Spring之WEB模块
Spring的WEB模块用于整合Web框架,例如Struts 1.Struts 2.JSF等 整合Struts 1 继承方式 Spring框架提供了ActionSupport类支持Struts 1的A ...
- [02] Spring主要功能模块概述
1.Spring主要功能模块 1.1 Core Container Spring的核心容器模块,其中包括: Beans Core Context SpEL Beans和Core模块,是框架的基础部 ...
- 一头扎进Spring之---------Spring七大核心模块
Spring七大核心模块 核心容器(Spring Core) 核心容器提供Spring框架的基本功能.Spring以bean的方式组织和管理Java应用中的各个组件及其关系.Spring使用BeanF ...
- Spring中各个模块
Spring中个模块介绍 核心模块 提供了对Bean的配置.管理.创建等以及IOC/DI.如果只需要提供IOC/DI功能,只需要引入Beans和Core两个jar包 Core 包含Spring框架 ...
- 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架构模式,可 ...
- Spring第七篇【Spring的JDBC模块】
前言 上一篇Spring博文主要讲解了如何使用Spring来实现AOP编程,本博文主要讲解Spring的对JDBC的支持- 对于JDBC而言,我们肯定不会陌生,我们在初学的时候肯定写过非常非常多的JD ...
随机推荐
- [C#]200 行代码使用 C# 实现区块链
文章原文来自:Code your own blockchain in less than 200 lines of Go!,原始文章是通过 Go 语言来实现自己的区块链的,这里我们参照该文章来使用 C ...
- Python网络爬虫笔记(二):链接爬虫和下载限速
(一)代码1(link_crawler()和get_links()实现链接爬虫) import urllib.request as ure import re import urllib.parse ...
- JavaScript之Promise对象
含义 Promise 是异步编程的一种解决方案,比传统的解决方案--回调函数和事件--更合理和更强大.它由社区最早提出和实现,ES6 将其写进了语言标准,统一了用法,原生提供了 Promise 对象. ...
- php文件包含漏洞(input与filter)
php://input php://input可以读取没有处理过的POST数据.相较于$HTTP_RAW_POST_DATA而言,它给内存带来的压力较小,并且不需要特殊的php.ini设置.php:/ ...
- [Codeforces 864E]Fire
Description Polycarp is in really serious trouble — his house is on fire! It's time to save the most ...
- 51 nod 1495 中国好区间
1495 中国好区间 基准时间限制:0.7 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 阿尔法在玩一个游戏,阿尔法给出了一个长度为n的序列,他认为,一段好的区间,它的长度是& ...
- codeforces round #419 A. Karen and Morning
Karen is getting ready for a new school day! It is currently hh:mm, given in a 24-hour format. As yo ...
- [bzoj4822][Cqoi2017]老C的任务&[bzoj1935][Shoi2007]Tree 园丁的烦恼
来自FallDream的博客,未经允许,请勿转载,谢谢. 老 C 是个程序员. 最近老 C 从老板那里接到了一个任务——给城市中的手机基站写个管理系统.作为经验丰富的程序员,老 C 轻松地完成 ...
- ubuntu Linux下C语言open函数打开或创建文件与read,write函数详细讲解
open(打开文件) 相关函数 read,write,fcntl,close,link,stat,umask,unlink,fopen 表头文件 #include<sys/types.h> ...
- 笔记8 AOP练习2
场景描述: 一张唱片有好多磁道,假设每个磁道只有一首歌,现在需要记录每首歌的播放次数,然后输出. 主要业务:歌曲播放 辅助功能:记录播放次数(切面) 1.创建唱片接口,CompactDiscs.jav ...