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

<?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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocations">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<bean id="userDao" class="com.dao.impl.UserDaoImpl" >
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="userService" class="com.service.UserService" >
<property name="userDao" ref="userDao"></property>
</bean>
</beans>
[html] view plaincopy
<?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"  
       xsi:schemaLocation="  
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">  
  
   <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
    <property name="configLocations">  
        <value>classpath:hibernate.cfg.xml</value>  
    </property>  
  </bean>  
      
  <bean id="userDao" class="com.dao.impl.UserDaoImpl" >  
    <property name="sessionFactory" ref="sessionFactory"></property>  
  </bean>  
    
  <bean id="userService" class="com.service.UserService" >  
    <property name="userDao" ref="userDao"></property>  
  </bean>  
    
</beans>  



hibernate.cfg.xml配置文件如下:
<?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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
<property name="connection.username">root</property>
<property name="connection.password">***</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">10</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<mapping resource="com/model/User.hbm.xml"/>
<!-- <mapping class="com.model.User"/>-->
</session-factory>
</hibernate-configuration>

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




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

配置的核心就是:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocations">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>





作者:星辰 时间:2016-05-27 浏览 907评论 0 赞 0砸 0 标签: spring
评论
还可以再输入500个字

请您注意

·自觉遵守:爱国、守法、自律、真实、文明的原则
·尊重网上道德,遵守《全国人大常委会关于维护互联网安全的决定》及中华人民共和国其他各项有关法律法规
·严禁发表危害国家安全,破坏民族团结、国家宗教政策和社会稳定,含侮辱、诽谤、教唆、淫秽等内容的作品
·承担一切因您的行为而直接或间接导致的民事或刑事法律责任
·您在NoteShare上发表的作品,NoteShare有权在网站内保留、转载、引用或者删除
·参与本评论即表明您已经阅读并接受上述条款