Hibernate所需元素:
三要素:实体类,*.hbm.xml,hibernate.cfg.xml
Spring所需元素:
applicationContext.xml
hibernate在操作数据库时,使用sessionFactory.openSession()来增删改查,并通过session来开启事务。
与spring整合时,事务的管理交给了spring,利用面向切面的思想来操作事务。
在spring的配置文件中引入sessionFactory,由于没法直接将sessionFactory注入,因为没有提供构造方法和set方法,获取回忆hibernate操作时,获取sessionFactory通过的是从hibernate.cfg.xml配置文件获取:
SessionFactory
sessionFactory
= configuration.buildSessionFactory();
那么在Spring的配置文件中,该如何注入SessionFactory呢?
通过一个中间类:LocalSessionFactoryBean
详细代码:
1.关于hibernate相关的配置文件:
hibernate操作的实体类为Classes班级实体类:
①Classes.hbm.xml<->Classes.java
②hibernate.cfg.xml
2.Spring配置文件:
applicationContext.xml:
<
beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx= "http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
>
<!--
要为 Hibernate提供SessionFactory,有两种方式获取SessionFactory:
1.根据hibernate.cfg.xml的配置文件来获取,由于在该配置文件中配置了连接数据库的相关信息,
所以这种方式不需要我们获取数据源,自动在内部完成。
2.手动根据properties文件获取数据源,然后再将数据源注入。
由于不能直接获取SessionFactory,因为SessionFactory不具备 di的特征(构造方法和set方法不满足),所以
Spring提供了一个额外的类:LocalSessionFactoryBean,它既有
hibernate
中sessionFactory中的功能
又有spring中DI的特点
-->
<!--方法一 直接根据hibernate的配置文件来获取 -->
<
bean
id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
<property name= "configLocation">
<value> classpath:hibernate.cfg.xml</value >
</property>
</
bean
>
<!-- 方法二 自行获取数据源(先加载properties文件,然后获取数据源),然后将数据源注入 -->
<
bean
class= "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
<property name= "locations">
<value> classpath:jdbc.properties</value >
</property>
</
bean
>
<
bean
id="dataSource" destroy-method="close"
class= "org.apache.commons.dbcp.BasicDataSource" >
<property name= "driverClassName" value="${jdbc.driverClassName}" />
<property name= "url" value ="${jdbc.url}" />
<property name= "username" value="${jdbc.username}" />
<property name= "password" value="${jdbc.password}" />
</
bean
>
<!-- 将获得的数据源注入到sessionFactory中-->
<
bean
id="sessionFactory1" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
<property name= "dataSource" ref ="dataSource"/>
<property name= "mappingResources">
<list>
<value> cn/itheima03/spring/ hibernate/transaction/domain/Classes.hbm.xml </value>
</list>
</property>
<property name= "hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
</value>
</property>
</
bean
>
<!-- ===================================================== -->
<!-- 需要注入sessionFactory -->
<
bean
id="classesDao" class="cn.itheima03.spring.hibernate.transaction.dao.impl.ClassesDaoImpl" >
<property name= "sessionFactory">
<ref bean= "sessionFactory1"/>
</property>
</
bean
>
<
bean
id="classesService" class="cn.itheima03.spring.hibernate.transaction.service.impl.ClassesServiceImpl" >
<property name= "classesDao">
<ref bean= "classesDao"/>
</property>
</
bean
>
<!-- 引入hibernate实现的事务管理,注意使用的sessionFactory要和classDao中注入的是同一个! -->
<
bean
id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" >
<property name= "sessionFactory">
<ref bean= "sessionFactory1"/>
</property>
</
bean
>
<!-- 配置目标类的哪些方法使用什么样的事务 -->
<
tx:advice
id="tx" transaction-manager="transactionManager" >
<tx:attributes>
<tx:method name= "save*" read-only ="false"/>
</tx:attributes>
</
tx:advice
>
<
aop:config
>
<aop:pointcut expression="execution(* cn.itheima03.spring.hibernate.transaction.service.impl.ClassesServiceImpl.*(..))" id ="perform"/>
<aop:advisor advice-ref="tx" pointcut-ref= "perform"/>
</
aop:config
>
</
beans
>
|
其他代码:
//继承
HibernateDaoSupport
public
class ClassesDaoImpl extends HibernateDaoSupport implements ClassesDao{
@Override
public
void saveClasses(Classes classes) {
this.getHibernateTemplate().save(classes);
}
}
====================================
public
class ClassesServiceImpl implements ClassesService{
private
ClassesDao classesDao;
public
ClassesDao getClassesDao() {
return classesDao ;
}
public
void setClassesDao(ClassesDao classesDao) {
this.classesDao = classesDao;
}
public
void saveClasses(Classes classes){
this.classesDao .saveClasses(classes);
}
}
====================================
public
class ClassesServiceTest {
@Test
public
void testSaveClasses(){
ApplicationContext context =
new ClassPathXmlApplicationContext("cn/itheima03/spring/hibernate/transaction/config/applicationContext.xml" );
ClassesService classesService = (ClassesService) context.getBean(
"classesService"
);
Classes classes =
new Classes();
classes.setCname(
"aa");
classesService.saveClasses(classes);
}
}
|