现在的位置: 首页 > 综合 > 正文

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

2015年04月19日 ⁄ 综合 ⁄ 共 4833字 ⁄ 字号 评论关闭

分类:
spring

2177人阅读
评论(0)
收藏
举报

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

  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. <bean
    id
    ="sessionFactory"
    class
    ="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  9. <property
    name
    ="configLocations">
  10. <value>classpath:hibernate.cfg.xml</value>
  11. </property>
  12. </bean>
  13. <bean
    id
    ="userDao"
    class
    ="com.dao.impl.UserDaoImpl"
    >
  14. <property
    name
    ="sessionFactory"
    ref
    ="sessionFactory"></property>
  15. </bean>
  16. <bean
    id
    ="userService"
    class
    ="com.service.UserService"
    >
  17. <property
    name
    ="userDao"
    ref
    ="userDao"></property>
  18. </bean>
  19. </beans>


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

  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. <hibernate-configuration>
  6. <session-factory>
  7. <!-- Database connection settings -->
  8. <property
    name
    ="connection.driver_class">com.mysql.jdbc.Driver</property>
  9. <property
    name
    ="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
  10. <property
    name
    ="connection.username">root</property>
  11. <property
    name
    ="connection.password">***</property>
  12. <!-- JDBC connection pool (use the built-in) -->
  13. <property
    name
    ="connection.pool_size">10</property>
  14. <!-- SQL dialect -->
  15. <property
    name
    ="dialect">org.hibernate.dialect.MySQLDialect</property>
  16. <!-- Enable Hibernate's automatic session context management -->
  17. <property
    name
    ="current_session_context_class">thread</property>
  18. <!-- Disable the second-level cache -->
  19. <property
    name
    ="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
  20. <!-- Echo all executed SQL to stdout -->
  21. <property
    name
    ="show_sql">true</property>
  22. <property
    name
    ="format_sql">true</property>
  23. <!-- Drop and re-create the database schema on startup -->
  24. <property
    name
    ="hbm2ddl.auto">update</property>
  25. <mapping
    resource
    ="com/model/User.hbm.xml"/>
  26. <!-- <mapping class="com.model.User"/>-->
  27. </session-factory>
  28. </hibernate-configuration>

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

配置的核心就是:

  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>

抱歉!评论已关闭.