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 ...
随机推荐
- ABP领域层知识回顾之---工作单元
1. 前言 在上一篇博文中(http://www.cnblogs.com/xiyin/p/6842958.html) 我们讲到了ABP领域层的仓储.这边博文我们来讲 工作单元.个人觉得比较重要.文 ...
- [WC 2006]水管局长数据加强版
Description SC省MY市有着庞大的地下水管网络,嘟嘟是MY市的水管局长(就是管水管的啦),嘟嘟作为水管局长的工作就是:每天供水公司可能要将一定量的水从x处送往y处,嘟嘟需要为供水公司找到一 ...
- [HNOI 2001]矩阵乘积
Description Input Output Sample Input 1 2 3 4 2 3 1 1 3 1 4 5 2 2 1 3 1 2 1 2 2 2 1 1 3 1 2 3 2 4 1 ...
- [BZOJ]1018 堵塞的交通(SHOI2008)
一道有点神的线段树. Description 有一天,由于某种穿越现象作用,你来到了传说中的小人国.小人国的布局非常奇特,整个国家的交通系统可以被看成是一个2行C列的矩形网格,网格上的每个点代表一个城 ...
- bzoj 4008: [HNOI2015]亚瑟王
Description 小 K 不慎被 LL 邪教洗脑了,洗脑程度深到他甚至想要从亚瑟王邪教中脱坑. 他决定,在脱坑之前,最后再来打一盘亚瑟王.既然是最后一战,就一定要打得漂 亮.众所周知,亚瑟王是一 ...
- 一个使用 Python 的人工智能聊天机器人框架
一个Python 的 AI Chatbot框架 建立一个聊天室可以听起来很棒,但它是完全可行的. IKY是一个内置于Python中的AI动力对话对话界面. 使用IKY,很容易创建自然语言会话场景,无需 ...
- [Luogu 1516] 青蛙的约会
Description 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要的事 ...
- Yii2 获取URL的一些方法
1. 获取url中的host信息: 例如:http://www.nongxiange.com/product/2.html Yii::$app->request->getHostInfo( ...
- JPA 的 CascadeType 属性 和 FetchType属性 和 各种映射关系
代码地址:https://gitee.com/a247292980/lgp20151222 CascadeType CascadeType.PERSIST级联新增(又称级联保存): CascadeTy ...
- JVM内部原理
这篇文章详细描述了Java虚拟机的内在结构.下面这张图来自<The Java Virtual Machine Specification Java SE 7 Edition>,它展示了一个 ...