nevergiveupzeng

导航

Spring 集成Hibernate的三种方式

首先把hibernate的配置文件hibernate.cfg.xml放入spring的src目录下,并且为了便于测试导入了一个实体类Student.java以及它的Student.hbm.xml文件

第一种集成方式:首先定义一个MySessionFactory的类

package com.tz.core;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.springframework.stereotype.Component;

public class MysessionFactoryBean {

    public void setConfig(String config) {
        this.config = config;
    }

    public String getConfig() {
        return config;
    }

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
    private String config;
    private SessionFactory sessionFactory;

    public void init() {
        Configuration configuration = new Configuration().configure(config);
        sessionFactory = configuration.buildSessionFactory();
    }
}

在Spring的配置文件中进行IOC注入

<bean id="sessionFactory" class="com.tz.core.MysessionFactoryBean" init-method="init">
         <property name="config" value="hibernate.cfg.xml"></property>
    </bean>

注意要加上init-method="init",否则不会初始化,然后获取Session

package hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

public class TestHibernate {
  @Test
  public void handle(){
      Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
      SessionFactory sessionFactory = configuration.buildSessionFactory();
      Session session = sessionFactory.openSession();
      System.out.print(session);
  }
}

用JUTIL测试打印一个地址则成功了

2第二种方式,

  <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
         <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
   </bean>

获取方式

	 @Autowired
	 private SessionFactory sessionFactory;

  直接注入即可,记得扫包

第三种集成方式,在Spring的配置文件applicationContext中写入.这种方法的一大特色是可以使用SPRING的数据源,hibernate.cfg.xml不导入也可

 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/test"></property>   
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>   
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" >
       <property name="dataSource" ref="dataSource"></property>
    </bean> 
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
       <property name="dataSource" ref="dataSource"></property>
        <property name="mappingLocations">
           <list>
               <value>com/tz/entity/Student.hbm.xml</value>
           </list>
        </property>
        <property name="hibernateProperties">
           <props>
              <prop key ="hibernate.show_sql">true</prop>
              <prop key="hibernate.format_sql">true</prop>
           </props>
        </property>         
    </bean>

获取SessionFactory对象的方式与第二种相同

posted on 2015-03-11 01:43  nevergiveupzeng  阅读(431)  评论(0编辑  收藏  举报