Spring 整合hibernate 以及注意事项

Spring2.X版本整合Hibernate

Spring2.x整合Hibernate和Spring3.x版基本一致,但要注意以下几点:

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

2、从Spring2.x之后开始支持注解

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

4、 导入包时注意删除相同的包!(优先删除版本低的)

 

 

在配置文件中添加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"/>这行代码也可以删去了。

猜你喜欢

转载自heands.iteye.com/blog/2356971