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">
    
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        
        <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
    bean>


    <bean id="studentDao" class="com.startspring.dao.impl.StudentDaoImpl">
        
        <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>

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


    <bean id="studentDao" class="com.startspring.dao.impl.StudentDaoImpl">
        
        <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>

    
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        
        <property name="dataSource" ref="dataSource"/>
        
        
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.show_sql">trueprop>
                <prop key="hibernate.current_session_context_class">threadprop>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialectprop>
            props>
        property>
        
        
        <property name="mappingLocations">
            <list>
                <value>com/startspring/entrty/Student.hbm.xmlvalue>
            list>
        property>
    bean>


    <bean id="studentDao" class="com.startspring.dao.impl.StudentDaoImpl">
        
        <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的配置文件中这行代码也可以删去了。

你可能感兴趣的:(Spring)