spring和hibernate配置文件整合

本文介绍Spring整合Hibernate的具体配置方法,包括使用Spring配置文件代替hibernate.cfg.xml进行数据库连接配置,以及如何设置Hibernate属性和映射类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

为了操作方便,我们经常在spring整合hibernate的WEB项目中省去了用hibernate.cfg.xml的麻烦,将hibernate的信息直接配置在 

Spring配置文件中 

下面的都是针对Spring整合Hibernate(注解方式的hibernate)来说的 

hibernate.cfg.xml和applicationContext.xml原始配置组合方式: 

hibernate.cfg.xml 




Xml代码 
<SPAN style="FONT-SIZE: large"><?xml version="1.0" encoding="utf-8" ?>  
<!DOCTYPE hibernate-configuration PUBLIC   
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"   
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  
<hibernate-configuration>  
    <session-factory>  
        <!--   
            这个只在DBScriptExport.java的new AnnotationConfiguration().configure()或者   
            new Configuration().configure();这里面由于生成数据库表,或者通过   
            config.buildSessionFactory都需要用到下面这四行关于数据库的配置.实际项目中,   
            如果不适用到硬编码做这两件事,而是通过和Spring配合,那么这四行可以不写   
        -->  
        <!--   
            <property name="hibernate.current_session_context_class">thread</property>  
            <property name="connection.driver_class">com.mysql.jdbc.Driver</property>  
            <property name="connection.url">jdbc:mysql:///test</property>  
            <property name="connection.username">root</property>    
            <property name="connection.password">root</property>  
        -->  
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>  
        <property name="show_sql">true</property>  
        <property name="format_sql">true</property>  
        <!-- mapping类声明-->  
        <mapping class="com.javacrazyer.domain.User" />  
    </session-factory>  
</hibernate-configuration></SPAN>  

<?xml version="1.0" encoding="utf-8" ?> 
<!DOCTYPE hibernate-configuration PUBLIC 
"-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 

<hibernate-configuration> 
<session-factory> 
<!-- 
这个只在DBScriptExport.java的new AnnotationConfiguration().configure()或者 
new Configuration().configure();这里面由于生成数据库表,或者通过 
config.buildSessionFactory都需要用到下面这四行关于数据库的配置.实际项目中, 
如果不适用到硬编码做这两件事,而是通过和Spring配合,那么这四行可以不写 
--> 
<!-- 
<property name="hibernate.current_session_context_class">thread</property> 
<property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
<property name="connection.url">jdbc:mysql:///test</property> 
<property name="connection.username">root</property> 
<property name="connection.password">root</property> 
--> 
<property name="dialect">org.hibernate.dialect.MySQLDialect</property> 
<property name="show_sql">true</property> 
<property name="format_sql">true</property> 
<!-- mapping类声明--> 
<mapping class="com.javacrazyer.domain.User" /> 
</session-factory> 
</hibernate-configuration> 




上面注释其实也没讲清楚,其实就是如果单独的hibernate项目来说,如果涉及到数据库操的任何操作代码,那么你必须配那段被注 

释掉的数据库连接信息,如果是整合Spring的话,那么就不用配置了,Spring提供了对hibernate支持的SessionFactory可以再注入 

后获取SESSION,然后你就可以任意去实现数据库操作了 

applicationContext.xml 


Xml代码 
<SPAN style="FONT-SIZE: large"><?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xsi:schemaLocation="   
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans- 

2.5.xsd   
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  


            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  
    <!-- 使用C3P0的连接池技术 -->  
    <bean id="dataSource" destroy-method="close"  
        class="com.mchange.v2.c3p0.ComboPooledDataSource">  
        <property name="driverClass" value="com.mysql.jdbc.Driver" />  
        <property name="jdbcUrl" value="jdbc:mysql:///test" />  
        <property name="user" value="root" />  
        <property name="password" value="root" />  
        <!-- 指定连接数据库连接池的最大连接数 -->  
        <property name="maxPoolSize" value="20" />  
        <!-- 指定连接数据库连接池的最小连接数 -->  
        <property name="minPoolSize" value="1" />  
        <!-- 指定连接数据库连接池的初始化连接数 -->  
        <property name="initialPoolSize" value="1" />  
        <!-- 指定连接数据库连接池的连接的最大空闲时间 -->  
        <property name="maxIdleTime" value="20" />  
    </bean>  
    <bean id="sessionFactory"  
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">  
        <!--指定hibernate配置文件的位置-->  
        <property name="configLocation" value="classpath:hibernate.cfg.xml" />  
        <!--   
        那么这里给sessionFactory注入dataSource的解释:其实就是相当于单独的hibernate项目中使用   
        config.buildSessionFactory()这样手动构建SessionFactory一样,必须提供有关数据库的参数配   
        置信息也就是在hibernate.cfg.xm中 配置的数据库信息,这样使用session才能关联上数据库   
        -->  
        <property name="dataSource" ref="dataSource" />  
    </bean>  
  
    <!--如果不配置SessionFactory,配置Spring自带的JdbcTemplate也挺好,然后注入到需要用到的类中去-->  
    <!--   
        <bean id="jdbcTemplate"  
        class="org.springframework.jdbc.core.JdbcTemplate">  
        <description>Config jdbcTemplate</description> <property  
        name="dataSource" ref="dataSource" /> </bean>  
    -->  
  
    <!--   
        下面是Hibernate的事务管理器 ,如果是单独spring框架的项目中配置spring事务,那么就没有上边   
        的sessionFactory而只会有dataSource,那么下面属性就不是sesionFactory而是DataSource了   
    -->  
    <bean id="transactionManager"  
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
        <property name="sessionFactory" ref="sessionFactory" />  
    </bean>  
  
    <!-- 启用注解方式的声明式事务支持 -->  
    <tx:annotation-driven transaction-manager="transactionManager" />  
  
    <!-- =============================================== -->  
    <bean id="userDao" class="com.javacrazyer.dao.UserDaoHibernateImpl">  
        <property name="sessionFactory" ref="sessionFactory" />  
    </bean>  
  
    <bean id="sf" class="com.javacrazyer.service.ServiceFacade">  
        <property name="userDao" ref="userDao" />  
    </bean>  
</beans></SPAN>  

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:tx="http://www.springframework.org/schema/tx" 
xsi:schemaLocation=" 
http://www.springframework.org/schema/beans 

http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring 

-aop-2.5.xsd 
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring- 

tx-2.5.xsd"> 

<!-- 使用C3P0的连接池技术 --> 
<bean id="dataSource" destroy-method="close" 
class="com.mchange.v2.c3p0.ComboPooledDataSource"> 
<property name="driverClass" value="com.mysql.jdbc.Driver" /> 
<property name="jdbcUrl" value="jdbc:mysql:///test" /> 
<property name="user" value="root" /> 
<property name="password" value="root" /> 
<!-- 指定连接数据库连接池的最大连接数 --> 
<property name="maxPoolSize" value="20" /> 
<!-- 指定连接数据库连接池的最小连接数 --> 
<property name="minPoolSize" value="1" /> 
<!-- 指定连接数据库连接池的初始化连接数 --> 
<property name="initialPoolSize" value="1" /> 
<!-- 指定连接数据库连接池的连接的最大空闲时间 --> 
<property name="maxIdleTime" value="20" /> 
</bean> 
<bean id="sessionFactory" 
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
<!--指定hibernate配置文件的位置--> 
<property name="configLocation" value="classpath:hibernate.cfg.xml" /> 
<!-- 
那么这里给sessionFactory注入dataSource的解释:其实就是相当于单独的hibernate项目中使用 
config.buildSessionFactory()这样手动构建SessionFactory一样,必须提供有关数据库的参数配 
置信息也就是在hibernate.cfg.xm中 配置的数据库信息,这样使用session才能关联上数据库 
--> 
<property name="dataSource" ref="dataSource" /> 
</bean> 

    <!--如果不配置SessionFactory,配置Spring自带的JdbcTemplate也挺好,然后注入到需要用到的类中去--> 
<!-- 
<bean id="jdbcTemplate" 
class="org.springframework.jdbc.core.JdbcTemplate"> 
<description>Config jdbcTemplate</description> <property 
name="dataSource" ref="dataSource" /> </bean> 
--> 

<!-- 
下面是Hibernate的事务管理器 ,如果是单独spring框架的项目中配置spring事务,那么就没有上边 
的sessionFactory而只会有dataSource,那么下面属性就不是sesionFactory而是DataSource了 
--> 
<bean id="transactionManager" 
class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
<property name="sessionFactory" ref="sessionFactory" /> 
</bean> 

<!-- 启用注解方式的声明式事务支持 --> 
<tx:annotation-driven transaction-manager="transactionManager" /> 

<!-- =============================================== --> 
<bean id="userDao" class="com.javacrazyer.dao.UserDaoHibernateImpl"> 
<property name="sessionFactory" ref="sessionFactory" /> 
</bean> 

<bean id="sf" class="com.javacrazyer.service.ServiceFacade"> 
<property name="userDao" ref="userDao" /> 
</bean> 
</beans>  
  上面关于SessionFactory注释说过了,如果不需要hibernate有关数据库的操作的话,那么就不要结合hibernate框架而是通过 

Spring自带的JdbcTemplate来进行数据库操作 




不要hibernate.cfg.xml,将hibernate基本信息配置在applicatonContext.xm中 




applicationContext.xml 





Xml代码 
<SPAN style="FONT-SIZE: large"><?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xsi:schemaLocation="   
            http://www.springframework.org/schema/beans    
            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
            http://www.springframework.org/schema/aop    
            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
            http://www.springframework.org/schema/tx    
            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
    <!-- 使用C3P0的连接池技术 -->  
    <bean id="dataSource" destroy-method="close"  
        class="com.mchange.v2.c3p0.ComboPooledDataSource">  
        <property name="driverClass" value="com.mysql.jdbc.Driver" />  
        <property name="jdbcUrl" value="jdbc:mysql:///test" />  
        <property name="user" value="root" />  
        <property name="password" value="root" />  
        <!-- 指定连接数据库连接池的最大连接数 -->  
        <property name="maxPoolSize" value="20" />  
        <!-- 指定连接数据库连接池的最小连接数 -->  
        <property name="minPoolSize" value="1" />  
        <!-- 指定连接数据库连接池的初始化连接数 -->  
        <property name="initialPoolSize" value="1" />  
        <!-- 指定连接数据库连接池的连接的最大空闲时间 -->  
        <property name="maxIdleTime" value="20" />  
    </bean>  
  
    <bean id="sessionFactory"  
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">  
        <property name="dataSource">  
            <ref bean="dataSource" />  
        </property>  
        <property name="hibernateProperties">  
            <props>  
                <prop key="dialect">  
                    org.hibernate.dialect.MySQLDialect   
                </prop>  
                <prop key="format_sql">true</prop>  
                <prop key="show_sql">true</prop>  
                <prop key="cache.use_query_cache">true</prop>  
                <prop key="cache.provider_class">  
                    org.hibernate.cache.HashtableCacheProvider   
                </prop>  
                <prop key="cache.provider_class">  
                    org.hibernate.cache.EhCacheProvider   
              </prop>  
            </props>  
        </property>  
  
           <!-- 针对xml而非注解方式的资源配置方式一 -->  
             <!-- <property name="mappingResources"> <list>  
            <value>com/javacrazyer/domain/user.hbm.xml</value> </list>  
            </property> -->  
            <!-- 针对xml而非注解方式的资源配置方式二 -->  
             <!--<property name="mappingLocations"> <list> <value>  
            classpath:com/javacrazyer/domain/user.hbm.xml </value> </list>  
            </property>-->  
           
        <property name="annotatedClasses">  
            <list>  
                <value>com.javacrazyer.domain.User</value>  
            </list>  
        </property>  
    </bean>  
  
       
    <!--   
        下面是Hibernate的事务管理器 ,如果是单独spring框架的项目中配置spring事务,那么就没有上边   
        的sessionFactory而只会有dataSource,那么下面属性就不是sesionFactory而是DataSource了   
    -->  
    <bean id="transactionManager"  
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
        <property name="sessionFactory" ref="sessionFactory" />  
    </bean>  
  
    <!-- 启用注解方式的声明式事务支持 -->  
    <tx:annotation-driven transaction-manager="transactionManager" />  
  
    <!-- =============================================== -->  
    <bean id="userDao" class="com.javacrazyer.dao.UserDaoHibernateImpl">  
        <property name="sessionFactory" ref="sessionFactory" />  
    </bean>  
  
    <bean id="sf" class="com.javacrazyer.service.ServiceFacade">  
        <property name="userDao" ref="userDao" />  
    </bean>  
  
</beans></SPAN>  

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:tx="http://www.springframework.org/schema/tx" 
xsi:schemaLocation=" 
http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
http://www.springframework.org/schema/aop 
            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 
<!-- 使用C3P0的连接池技术 --> 
<bean id="dataSource" destroy-method="close" 
class="com.mchange.v2.c3p0.ComboPooledDataSource"> 
<property name="driverClass" value="com.mysql.jdbc.Driver" /> 
<property name="jdbcUrl" value="jdbc:mysql:///test" /> 
<property name="user" value="root" /> 
<property name="password" value="root" /> 
<!-- 指定连接数据库连接池的最大连接数 --> 
<property name="maxPoolSize" value="20" /> 
<!-- 指定连接数据库连接池的最小连接数 --> 
<property name="minPoolSize" value="1" /> 
<!-- 指定连接数据库连接池的初始化连接数 --> 
<property name="initialPoolSize" value="1" /> 
<!-- 指定连接数据库连接池的连接的最大空闲时间 --> 
<property name="maxIdleTime" value="20" /> 
</bean> 

<bean id="sessionFactory" 
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
<property name="dataSource"> 
<ref bean="dataSource" /> 
</property> 
<property name="hibernateProperties"> 
<props> 
<prop key="dialect"> 
org.hibernate.dialect.MySQLDialect 
                </prop> 
<prop key="format_sql">true</prop> 
<prop key="show_sql">true</prop> 
<prop key="cache.use_query_cache">true</prop> 
<prop key="cache.provider_class"> 
org.hibernate.cache.HashtableCacheProvider 
                </prop> 
<prop key="cache.provider_class"> 
org.hibernate.cache.EhCacheProvider 
              </prop> 
</props> 
</property> 

   <!-- 针对xml而非注解方式的资源配置方式一 --> 
<!-- <property name="mappingResources"> <list> 
<value>com/javacrazyer/domain/user.hbm.xml</value> </list> 
</property> --> 
<!-- 针对xml而非注解方式的资源配置方式二 --> 
<!--<property name="mappingLocations"> <list> <value> 
classpath:com/javacrazyer/domain/user.hbm.xml </value> </list> 
</property>--> 

<property name="annotatedClasses"> 
<list> 
<value>com.javacrazyer.domain.User</value> 
</list> 
</property> 
</bean> 


<!-- 
下面是Hibernate的事务管理器 ,如果是单独spring框架的项目中配置spring事务,那么就没有上边 
的sessionFactory而只会有dataSource,那么下面属性就不是sesionFactory而是DataSource了 
--> 
<bean id="transactionManager" 
class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
<property name="sessionFactory" ref="sessionFactory" /> 
</bean> 

<!-- 启用注解方式的声明式事务支持 --> 
<tx:annotation-driven transaction-manager="transactionManager" /> 

<!-- =============================================== --> 
<bean id="userDao" class="com.javacrazyer.dao.UserDaoHibernateImpl"> 
<property name="sessionFactory" ref="sessionFactory" /> 
</bean> 

<bean id="sf" class="com.javacrazyer.service.ServiceFacade"> 
<property name="userDao" ref="userDao" /> 
</bean> 

</beans> 注意与第一种方式不同的是applicatonContext.xml中只有sessionFactory发生了变化,数据源依旧注入到里面去,原来 

该配置hibernate.cfg.xml文件位置的配置现在换成了,众多行与第一种方式中hibernate.cfg.xml中类似的信息配置。 

这样来说呢,更加省事些,当然我指的是SH结合的情况下,如果是hibernate单独的项目,必然用到new 

AnnotationConfiguration.config()就必然需要默认查找src下的hibernate.cfg.xml 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值