Spring整合Hibernate 一 - 注入SessionFactory


Spring3 整合 Hibernate4 - 注入SessionFactory

版本:

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

要使用Spring3整合Hibernate4需要再添加以下包

1.----  spring-orm-3.2.4.RELEASE.jar
2.----  spring-dao-2.0.7.jar(Spring里沒有提供,需要到網上下載)

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

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 的baen

<?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>

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

使用數據源(DataSource)

如果要使用數據源:

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

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

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.  導入包時注意刪除相同的包!(優先刪除版本低的)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM