Spring整合Hibernate 一 - 注入SessionFactory

Spring3 整合 Hibernate4 - 注入SessionFactory

版本:html

spring-framework-3.2.4.RELEASE
hibernate-release-4.2.5.Final
jdk1.7spring

要使用Spring3整合Hibernate4須要再添加如下包sql

1.----  spring-orm-3.2.4.RELEASE.jar
2.----  spring-dao-2.0.7.jar(Spring裏沒有提供,須要到網上下載)數據庫

在要用到hibernate的類中添加SessionFactory屬性和相應的set方法session

public class StudentDaoImpl implements IStudentDao {
    //SessionFactory 在包org.hibernate內
    private SessionFactory sessionFactory;

    public void save(Student student) {
        Session session = sessionFactory.getCurrentSession();
        session.beginTransaction();
        session.save(student);
        session.getTransaction().commit();
        
        System.out.println("save:"+student.getName());
    }

    //爲SessionFactory添加set方法,Spring才能爲其注入
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

}

在配置文件中添加SessionFactory 的baenoracle

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    <!-- 添加sessionFactory bane ,注意,該類是Spring提供的 -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <!-- 注入Hibernate 配置文件路徑,前面要加上  classpath:-->
        <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
    </bean>


    <bean id="studentDao" class="com.startspring.dao.impl.StudentDaoImpl">
        <!-- 把sessionFactory 注入給studentDao -->
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="studentService" class="com.startspring.service.impl.StudentServiceImpl">
        <property name="studentDao" ref="studentDao" />
    </bean>

    <bean id="start" class="com.startspring.Start">
        <property name="studentService" ref="studentService" />
    </bean>
</beans>

------------------------------------------------------------------------------------------------------------app

使用數據源(DataSource)

若是要使用數據源:this

1.先把數據源相關的包導入到項目。spa

2.在Spring配置文件中添加數據源的bean。hibernate

3.把數據源 bean 注入到sessionFactory的dataSource屬性。

(關於 Spring 注入數據源:http://www.cnblogs.com/likailan/p/3459776.html

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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <!-- 數據源 -->
    <bean id="dataSource" destroy-method="close" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="oracle.jdbc.OracleDriver" />
        <property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:ORCL" />
        <property name="user" value="hib" />
        <property name="password" value="hib" />
    </bean>

    <!-- 添加sessionFactory bane ,注意,該類是Spring提供的 -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <!-- 注入Hibernate 配置文件路徑,前面要加上  classpath:-->
        <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
        <!-- 把數據源 bean 注入到sessionFactory的dataSource屬性 -->
        <property name="dataSource" ref="dataSource"/>
    </bean>


    <bean id="studentDao" class="com.startspring.dao.impl.StudentDaoImpl">
        <!-- 把sessionFactory 注入給studentDao -->
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="studentService" class="com.startspring.service.impl.StudentServiceImpl">
        <property name="studentDao" ref="studentDao" />
    </bean>

    <bean id="start" class="com.startspring.Start">
        <property name="studentService" ref="studentService" />
    </bean>
</beans>

如今數據庫鏈接交由數據源來管理。能夠把 hibernate.cfg.xml 文件中的connection.driver_class、connection.username等屬性去掉了。

--------------------------------------------------------------------------------------------------------------

去掉Hibernate配置文件

經過注入的方式爲SessionFactory注入hibernate的配置信息,從而省去hibernate配置文件

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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <!-- 數據源 -->
    <bean id="dataSource" destroy-method="close" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="oracle.jdbc.OracleDriver" />
        <property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:ORCL" />
        <property name="user" value="hib" />
        <property name="password" value="hib" />
    </bean>

    <!-- 添加sessionFactory bane ,注意,該類是Spring提供的 -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <!-- 把數據源 bean 注入到sessionFactory的dataSource屬性 -->
        <property name="dataSource" ref="dataSource"/>
        
        <!-- hibernate的基本配置,如:方言,是否顯示sql等 -->
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.current_session_context_class">thread</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
            </props>
        </property>
        
        <!-- 引入映射文件,若是有多個能夠繼續添加 <value> -->
        <property name="mappingLocations">
            <list>
                <value>com/startspring/entrty/Student.hbm.xml</value>
            </list>
        </property>
    </bean>


    <bean id="studentDao" class="com.startspring.dao.impl.StudentDaoImpl">
        <!-- 把sessionFactory 注入給studentDao -->
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="studentService" class="com.startspring.service.impl.StudentServiceImpl">
        <property name="studentDao" ref="studentDao" />
    </bean>

    <bean id="start" class="com.startspring.Start">
        <property name="studentService" ref="studentService" />
    </bean>
</beans>

如今能夠把 hibernate.cfg.xml 文件刪除了。

原來Spring的配置文件中<property name="configLocation" value="classpath:hibernate.cfg.xml"/>這行代碼也能夠刪去了。

----------------------------------------------------------------------------------------------------------

Spring2.X版本整合Hibernate

Spring2.x整合Hibernate和Spring3.x版基本一致,但要注意如下幾點:

1.  Spring2.x只支持Hibernate3 ,並不支持Hibernate4

2.  整合 Hibernate3 使用的SessionFactory Bean 是:org.springframework.orm.hibernate3包下的LocalSessionFactoryBean而不是org.springframework.orm.hibernate4.LocalSessionFactoryBean(Spring3整合hibernate3也是)

3.  導入包時注意刪除相同的包!(優先刪除版本低的)

相關文章
相關標籤/搜索