21-Spring與Hibernate結合使用

目錄:java

  1. Spring與hibernate結合關鍵
  2. 具體實現步驟:
    1. 導入Hibernate,Spring相關jar包
    2. UserDao中使用spring的原始代碼
    3. 與spring整合,由spring建立SessionFactory對象

 

 

1)Spring與hibernate結合關鍵mysql

  • 將單例的SessionFactory對象注入Spring的IOC容器
  • 使用Spring來實現聲明式事務管理

2)具體實現步驟:web

  1. 導入Hibernate,Spring相關jar包
    1. hibernate相關jar包
    2. spring core相關jar包
    3. spring aop相關jar包
    4. spring orm相關jar包:
      • spring-jdbc-3.2.5.RELEASE.jar
      • spring-orm-3.2.5.RELEASE.jar
      • spring-tx-3.2.5.RELEASE.jar
  2. UserDao中不使用spring的原始代碼
    public void save(User user){
            //不使用spring的原始代碼
            Configuration config = new Configuration();
            config.configure();
            SessionFactory sf = config.buildSessionFactory();
            Session session = sf.getCurrentSession();
            session.beginTransaction();
            session.save(user);
            session.getTransaction().commit();
        }
  3. 與spring整合,由spring建立SessionFactory對象:
    • 在Spring中,咱們要經過Spring的某一個Hibernate Session工廠bean來獲取Hibernate SessionFactory。Spring提供了多種Session工廠bean供選擇,大體分爲爲:
      • org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean
      • org.springframework.orm.hibernate5.LocalSessionFactoryBean
      • org.springframework.orm.hibernate5.LocalSessionFactoryBuilder
    1. 方式一:直接加載hibernate.cfg.xml,建立SessionFactory對象
    2. 方式二:鏈接池交給spring管理,其餘配置仍是寫到hibernate.cfg.xml中
      <bean id="sessionFactory"
              class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
              <property name="dataSource" ref="dataSource"></property>
              <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
          </bean>
    3. 方式3:(推薦) 全部的配置都在spring中完成
      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
          xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
          xmlns:aop="http://www.springframework.org/schema/aop"
          xsi:schemaLocation="http://www.springframework.org/schema/beans 
                              http://www.springframework.org/schema/beans/spring-beans.xsd
                              http://www.springframework.org/schema/context 
                              http://www.springframework.org/schema/context/spring-context-4.3.xsd
                              http://www.springframework.org/schema/aop
                              http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
                              http://www.springframework.org/schema/tx 
                              http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
          
              <!-- 示例化鏈接池 -->
          <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
              <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
              <property name="url" value="jdbc:mysql://localhost:3306/javaweb"></property>
              <property name="username" value="root"></property>
              <property name="password" value=""></property>
              <property name="initialSize" value="6"></property>
              <property name="maxActive" value="2"></property>
          </bean>
          
          <!-- spring建立SessionFactory對象 -->
          <bean id="sessionFactory"
              class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
      
              <property name="dataSource" ref="dataSource"></property>
              <property name="hibernateProperties">
                  <props>
                      <prop key="hibernate.dialect">org.hibernate.dialect.MySQL55Dialect</prop>
                          <prop key="hibernate.show_sql">true</prop>
                          <prop key="hibernate.hbm2ddl.auto">update</prop>
                      </props>
              </property>
      
              <property name="mappingLocations">
                      <list>
                          <value>classpath:claire/entity/User.hbm.xml</value>
                      </list>
              </property>
          </bean>
          
          <bean id="userDao" class="claire.dao.UserDao">
              <property name="sessionFactory" ref="sessionFactory"></property>
          </bean>
          <bean id="userService" class="claire.service.UserService">
              <property name="userDao" ref="userDao"></property>
          </bean>
      
          <!-- spring增長對事務支持 -->
          <bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
              <property name="sessionFactory" ref="sessionFactory"></property>
          </bean>
          <tx:advice id="txAdvice" transaction-manager="txManager">
              <tx:attributes>
                  <tx:method name="*" read-only="false"/>
              </tx:attributes>
          </tx:advice>
          <!-- 配置切面 -->
          <aop:config>
              <aop:advisor advice-ref="txAdvice" pointcut="execution(* claire..*Service.*(..))"/>
          </aop:config>
      </beans>

      spring整合後的代碼:spring

      package claire.dao;
      
      import org.hibernate.SessionFactory;
      import claire.entity.User;
      
      public class UserDao {
          
          //接收spring 的Session對象
          private SessionFactory sessionFactory;
          
          public void save(User user){
              sessionFactory.getCurrentSession().save(user);
          }
      
          public void setSessionFactory(SessionFactory sessionFactory) {
              this.sessionFactory = sessionFactory;
          }
          
      }
      UserDao
  4. 以後能夠選擇兩種方式操做:
    1. 直接在dao中使用sessionFactory對象操做數據庫sql

          public void save(User user){
              sessionFactory.getCurrentSession().save(user);
          }

       

    2.  使用Spring提供的 HibernateTemplate 工具類操做數據庫(5.x已經棄用了)
              HibernateTemplate hibernateTemplate = new HibernateTemplate(sessionFactory);
              hibernateTemplate.save(user);

       

      )
相關文章
相關標籤/搜索