【Spring八】Spring与Hibernate整合

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"  >
     
     
     
      < bean  id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
            <property name= "configLocation">
                <value> classpath:hibernate.cfg.xmlvalue >
            property>
      bean  >
     
     
      < bean
            class= "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
            <property name= "locations">
                <value> classpath:jdbc.propertiesvalue >
            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  >
     
      < 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  >
     
     
     
     
      < 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  >
     
     
      < 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);
     }
}






你可能感兴趣的:(Spring,Hibernate,框架,spring,hibernate,数据库,jdbc)