Spring之ORM模块
2015-05-27 11:22
295 查看
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会自动维护这些对象
[java] view
plaincopyprint?
public interface ICatDao{
public void createCat(Cat cat);
public List<Cat> listCats();
public int getCatsCount();
public Cat findCatByName(String name);
}
[java] view
plaincopyprint?
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;
}
}
[html] view
plaincopyprint?
<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配置的实体类,则改为
[html] view
plaincopyprint?
<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层
[java] view
plaincopyprint?
public interface ICatService{
public void createCat(Cat cat);
public List<Cat> listCats();
public int getCatsCount();
}
分层的做法是,程序调用Service层,Service层调用DAO层,DAO层调用Hibernate实现数据访问,原则上不允许跨曾访问。分层使业务层次更加清晰
[java] view
plaincopyprint?
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层配置事务管理
[html] view
plaincopyprint?
<!-- 事务管理器-->
<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><!-- 为所有方法加上事务-->
<propkeypropkey="*">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>
ORM模块依赖于dom4j.jar、antlr.jar等包
在Spring里,Hibernate的资源要交给Spring管理,Hibernate以及其SessionFactory等知识Spring一个特殊的Bean,有Spring负责实例化与销毁。因此DAO层只需要继承HibernateDaoSupport,而不需要与Hibernate的API打交道,不需要开启、关闭Hibernate的Session、Transaction,Spring会自动维护这些对象
[java] view
plaincopyprint?
public interface ICatDao{
public void createCat(Cat cat);
public List<Cat> listCats();
public int getCatsCount();
public Cat findCatByName(String name);
}
[java] view
plaincopyprint?
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;
}
}
[html] view
plaincopyprint?
<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配置的实体类,则改为
[html] view
plaincopyprint?
<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层
[java] view
plaincopyprint?
public interface ICatService{
public void createCat(Cat cat);
public List<Cat> listCats();
public int getCatsCount();
}
分层的做法是,程序调用Service层,Service层调用DAO层,DAO层调用Hibernate实现数据访问,原则上不允许跨曾访问。分层使业务层次更加清晰
[java] view
plaincopyprint?
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层配置事务管理
[html] view
plaincopyprint?
<!-- 事务管理器-->
<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><!-- 为所有方法加上事务-->
<propkeypropkey="*">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模块(结合Hibernate)
- spring的orm模块
- Spring没有对ORM模块提供自己的实现,只对JDBC和其他ORM框架进行了封装。
- Spring之ORM模块代码详解
- Spring之ORM模块
- Spring之ORM模块
- Spring的七大模块
- spring boot创建多模块项目
- Spring详解-----------对ORM的支持
- 使用spring测试模块搭建自动测试平台
- Spring之ORM(spring 与mybatis的4种整合实例)
- Spring orm +Spring data jpa 入门
- Spring IOC模块的简单介绍
- spring boot项目中使用spring-boot-devtools模块进行代码热部署,避免重新启动web项目
- spring boot项目中使用spring-boot-devtools模块进行代码热部署,避免重新启动web项目
- maven搭建springboot多模块项目
- Springboot整合dubbo构建maven多模块项目(三) - 把server分为api(服务接口定义)和server(服务实现)两个子module
- SpringBoot多模块项目
- 【第八章】 对ORM的支持 之 8.2 集成Hibernate3 ——跟我学spring3
- Spring DAO vs Spring ORM vs Spring JDBC