spring2.5笔记 - 第十课 Spring整合Hiberante3

来源:互联网 发布:悠悠球淘宝 编辑:程序博客网 时间:2024/06/05 13:22

http://sishuok.com/forum/blogPost/list/3720.html

 

Spring整合hibernate3重点就是需要初始化SessionFactory这个bean,需要在Spring的配置文件中进行配置,实现实例如下:

Spring配置hibernate3的SessionFactory

实现上xml方式的配置文件与annotation注解方式的区别,只是在配置时所使用的bean不一样,而且配置实体类所使用的属性也不一样(xml:mappingResources;annotation:annotatedClasses),如下:

(一) xml形式的SessionFactory

java代码:
查看复制到剪贴板打印
  1. <!-- hibernater的xml方式 LocalSessionFactoryBean是处理xml方式的bean
  2. 再利用mappingResources来配置实体类(model)映射文件
  3. 再利用hibernateProperties来配置相关属性-->
  4. <bean id="mySessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  5. <property name="dataSource" ref="dataSoure"/>
  6. <property name="mappingResources">
  7. <list>
  8. <value>User.hbm.xml</value>
  9. </list>
  10. </property>
  11. <property name="hibernateProperties">
  12. <props>
  13. <prop key="hibernate.dialect">org.hibernate.dialect.MysqlDialect</prop>
  14. <prop key="show_sql">true</prop>
  15. <prop key="format_sql">true</prop>
  16. </props>
  17. </property>
<!-- hibernater的xml方式 LocalSessionFactoryBean是处理xml方式的bean再利用mappingResources来配置实体类(model)映射文件再利用hibernateProperties来配置相关属性--><bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">    <property name="dataSource" ref="dataSoure"/>    <property name="mappingResources">      <list>        <value>User.hbm.xml</value>      </list>    </property>    <property name="hibernateProperties">    <props>    <prop key="hibernate.dialect">org.hibernate.dialect.MysqlDialect</prop>    <prop key="show_sql">true</prop>    <prop key="format_sql">true</prop>    </props></property>


java代码:
查看复制到剪贴板打印
  1. (二) annotation注解方式的SessionFactory
  2. <!-- hibernater的xml方式 AnnotationSessionFactoryBean是处理annotation方式的bean
  3. 再利用annotatedClasses来配置实体类(model)是否进行了注解
  4. 再利用hibernateProperties来配置相关属性-->
  5. <bean id="mySessionFactory"class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  6. <property name="dataSource" ref="dataSource"/>
  7. <property name="annotatedClasses">
  8. <list>
  9. <value>com.wjt276.model.User</value>
  10. </list>
  11. </property>
  12. <property name="hibernateProperties">
  13. <props>
  14. <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
  15. <prop key="hibernate.show_sql">true</prop>
  16. <prop key="hibernate.format_sql">true</prop>
  17. </props>
  18. </property>
  19. 注意:配置SessionFactory要求前面已经配置了一个数据源的bean了,这里的dataSource引用了那个bean,同时,在dao层中的dataSource成员属性要求使用自动装配功能,进行自动注入。
(二)annotation注解方式的SessionFactory     <!-- hibernater的xml方式 AnnotationSessionFactoryBean是处理annotation方式的bean再利用annotatedClasses来配置实体类(model)是否进行了注解再利用hibernateProperties来配置相关属性-->    <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">    <property name="dataSource" ref="dataSource"/>    <property name="annotatedClasses">      <list>        <value>com.wjt276.model.User</value>      </list>    </property>    <property name="hibernateProperties">    <props>    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>    <prop key="hibernate.show_sql">true</prop>    <prop key="hibernate.format_sql">true</prop>    </props>    </property>注意:配置SessionFactory要求前面已经配置了一个数据源的bean了,这里的dataSource引用了那个bean,同时,在dao层中的dataSource成员属性要求使用自动装配功能,进行自动注入。