Spring整合Hibernate配置(使用hibernate.cfg.xml文件)

Spring整合Hibernate配置(使用hibernate.cfg.xml文件)

分类: spring   3487人阅读  评论(4)  收藏  举报

本文主要记录spring整合hibernate的时候,直接使用hibernate.cfg.xml文件注入SessionFactory,applicationContext.xml文件配置如下:

[html]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:aop="http://www.springframework.org/schema/aop"  
  5.        xsi:schemaLocation="  
  6. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  7. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">  
  8.   
  9.    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  10.     <property name="configLocations">  
  11.         <value>classpath:hibernate.cfg.xml</value>  
  12.     </property>  
  13.   </bean>  
  14.       
  15.   <bean id="userDao" class="com.dao.impl.UserDaoImpl" >  
  16.     <property name="sessionFactory" ref="sessionFactory"></property>  
  17.   </bean>  
  18.     
  19.   <bean id="userService" class="com.service.UserService" >  
  20.     <property name="userDao" ref="userDao"></property>  
  21.   </bean>  
  22.     
  23. </beans>  

hibernate.cfg.xml配置文件如下:

[html]  view plain copy
  1. <?xml version='1.0' encoding='utf-8'?>  
  2. <!DOCTYPE hibernate-configuration PUBLIC  
  3.         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  4.         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  5.   
  6. <hibernate-configuration>  
  7.   
  8.     <session-factory>  
  9.   
  10.         <!-- Database connection settings -->  
  11.         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>  
  12.         <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>  
  13.         <property name="connection.username">root</property>  
  14.         <property name="connection.password">***</property>  
  15.   
  16.         <!-- JDBC connection pool (use the built-in) -->  
  17.         <property name="connection.pool_size">10</property>  
  18.   
  19.         <!-- SQL dialect -->  
  20.         <property name="dialect">org.hibernate.dialect.MySQLDialect</property>  
  21.   
  22.         <!-- Enable Hibernate's automatic session context management -->  
  23.         <property name="current_session_context_class">thread</property>  
  24.   
  25.         <!-- Disable the second-level cache  -->  
  26.         <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>  
  27.   
  28.         <!-- Echo all executed SQL to stdout -->  
  29.         <property name="show_sql">true</property>  
  30.         <property name="format_sql">true</property>  
  31.   
  32.         <!-- Drop and re-create the database schema on startup -->  
  33.         <property name="hbm2ddl.auto">update</property>  
  34.   
  35.         <mapping resource="com/model/User.hbm.xml"/>  
  36. <!--        <mapping class="com.model.User"/>-->  
  37.   
  38.     </session-factory>  
  39.   
  40. </hibernate-configuration>  


由于hibernate.cfg.xml文件和applicationContext文件独立开来了,这样更便于hibernate自己独立配置管理,而spring所起的作用只是注入SessionFactory,这样思路更明确,而不是在applicationContext.xml中既要配置Datasource,又要配置SessionFactory,很麻烦,分工明确思路清晰

配置的核心就是:

[html]  view plain copy
  1. <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  2.     <property name="configLocations">  
  3.         <value>classpath:hibernate.cfg.xml</value>  
  4.     </property>  
  5.  </bean>  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值