您的位置:首页 > 编程语言 > Java开发

Spring配置SessionFactory

2012-11-12 21:54 141 查看
1.不用dataSource引入hibernate.cfg.xml

hibernate.cfg.xml放在src目录下。(hibernate的配置文件)

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

2.丢掉hibernate.cfg.xml

1>使用注解:

<bean id="sessionFactory" class="...hibernate3.annotation.AnnotationSessionFactoryBean">
       <property name="dataSource" ref="dataSource" />   
       <property name="annotatedClasses">   
          <list>   
             <value>com.bjsxt.model.User</value>   
             <value>com.bjsxt.model.Log</value>   
          </list>   
       </property>   
      <property name="hibernateProperties">   
         <props>   
            <prop key="hibernate.dialect">   
               org.hibernate.dialect.MySQLDialect   
           </prop>   
           <prop key="hibernate.show_sql">true</prop>   
         </props>   
      </property>   
</bean>

2>不使用注解:

<bean id="sessionFactory"
     class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
	<property name="dataSource" ref="dataSource" />
	<property name="mappingResources">   <!-- mappingResouces属性用来列出全部映射文件 -->
	<list><!-- 以下用来列出Hibernate映射文件 -->
		<value>jsf/web/entity/User.hbm.xml</value>
	</list>
	</property>
 	<!-- 定义Hibernate的SessionFactory的属性 -->
	<property name="hibernateProperties">
		<props>
		    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
		    <prop key="hibernate.hbm2ddl.auto">update</prop>
		    <prop key="hibernate.show_sql">true</prop>
		    <prop key="hibernate.format_sql">true</prop>
		</props>
	</property>
</bean>

3>使用packagesToScan

它Spring 2.5.6的新特性。

<bean id="sessionFactory" class="...hibernate3.annotation.AnnotationSessionFactoryBean">
	 <property name="dataSource" ref="dataSource" />
	 <property name="packagesToScan">
		<list>
		     <value>com.bjsxt.model</value>
	        </list>
	</property>
	<property name="hibernateProperties">
		<props>
		     <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
		     <prop key="hibernate.show_sql">true</prop>
		</props>
	</property>
</bean>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: