spring+hibernate 两种整合方式配置文件

来源:互联网 发布:tabletoexcel.js 编辑:程序博客网 时间:2024/06/06 03:52

之前的文章都是讲解springmvc+spring+mybatis 的整合,而很少有springmvc+spring+hibernate 因为工作的需要,最近在使用hibernate 所以下面我们来看看 spring整合hibernate的配置文件,这里只说spring+hibernate 的配置文件而不说springmvc 因为这些是不用变的。

spring整合hibernate 有两种方式 1、注解方式 2、xml方式实现


1、注解方式实现:

  applicationContext.xml配置文件:

[html] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  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.      xmlns:tx="http://www.springframework.org/schema/tx"  
  6.      xmlns:context="http://www.springframework.org/schema/context"  
  7.      xsi:schemaLocation="  
  8.          http://www.springframework.org/schema/beans  
  9.          http://www.springframework.org/schema/beans/spring-beans.xsd  
  10.          http://www.springframework.org/schema/tx  
  11.          http://www.springframework.org/schema/tx/spring-tx.xsd  
  12.          http://www.springframework.org/schema/aop  
  13.          http://www.springframework.org/schema/aop/spring-aop.xsd  
  14.          http://www.springframework.org/schema/context  
  15.          http://www.springframework.org/schema/context/spring-context.xsd">  
  16.     <context:component-scan base-package="com.test" />  
  17.     <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  18.       <property name="locations">  
  19.         <list>  
  20.             <value>classpath:jdbc.properties</value>  
  21.         </list>  
  22.       </property>  
  23.     </bean>  
  24.     <bean id="c3p0DataSource" destroy-method="close"  
  25.         class="com.mchange.v2.c3p0.ComboPooledDataSource">  
  26.         <property name="driverClass" value="${driverClass}" />  
  27.         <property name="jdbcUrl" value="${url}" />  
  28.         <property name="user" value="${user}" />  
  29.         <property name="password" value="${password}" />  
  30.         <property name="initialPoolSize" value="${initialPoolSize}" />  
  31.         <property name="minPoolSize" value="${minPoolSize}" />  
  32.         <property name="maxPoolSize" value="${maxPoolSize}" />  
  33.         <property name="maxIdleTime" value="${maxIdleTime}" />  
  34.     </bean>                   
  35.     <bean id="sessionFactory"  
  36.         class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
  37.         <property name="dataSource" ref="c3p0DataSource" />  
  38.         <property name="packagesToScan">  
  39.             <list>  
  40.                 <value>com.test.bean</value>  
  41.             </list>  
  42.         </property>  
  43.         <property name="hibernateProperties">  
  44.             <props>  
  45.                 <prop key="hibernate.dialect">${dialect}</prop>  
  46.                 <prop key="hibernate.show_sql">${show_sql}</prop>  
  47.                 <prop key="hibernate.format_sql">${format_sql}</prop>  
  48.                 <prop key="hibernate.use_sql_commants">${use_sql_comments}</prop>  
  49.                 <prop key="hibernate.hbm2ddl.auto">${hbm2ddl.auto}</prop>  
  50.             </props>  
  51.         </property>  
  52.     </bean>  
  53.     <bean id="txManager"  
  54.         class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
  55.         <property name="sessionFactory" ref="sessionFactory" />  
  56.     </bean>  
  57.     <tx:advice id="txAdvice" transaction-manager="txManager">  
  58.         <tx:attributes>  
  59.             <tx:method name="get*" read-only="true" />  
  60.             <tx:method name="*" />  
  61.         </tx:attributes>  
  62.     </tx:advice>  
  63.     <aop:config>  
  64.         <aop:pointcut id="bizMethods" expression="execution(* com.test.biz.*.*(..))" />  
  65.         <aop:advisor advice-ref="txAdvice" pointcut-ref="bizMethods" />  
  66.     </aop:config>  
  67. </beans>  
2.xml方式实现
applicationContext.xml配置:

[html] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  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.   xmlns:tx="http://www.springframework.org/schema/tx"  
  6.   xsi:schemaLocation="http://www.springframework.org/schema/beans  
  7.                       http://www.springframework.org/schema/beans/spring-beans.xsd  
  8.                       http://www.springframework.org/schema/tx  
  9.                       http://www.springframework.org/schema/tx/spring-tx.xsd  
  10.                       http://www.springframework.org/schema/aop  
  11.                       http://www.springframework.org/schema/aop/spring-aop.xsd">  
  12.            
  13.     <!-- 让spring 去读取指定路径下的资源文件 -->  
  14.     <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  15.       <property name="locations" value="classpath:jdbc.properties"/>  
  16.     </bean>  
  17.       
  18.     <!-- 配置c3p0连接池 -->  
  19.     <bean id="c3p0Source" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">  
  20.       <property name="driverClass" value="${driverClass}" />  
  21.       <property name="jdbcUrl" value="${url}" />  
  22.       <property name="user" value="${user}" />  
  23.       <property name="password" value="${password}" />  
  24.       <property name="initialPoolSize" value="${initialPoolSize}" />  
  25.       <property name="minPoolSize" value="${minPoolSize}" />  
  26.       <property name="maxPoolSize" value="${maxPoolSize}" />  
  27.       <property name="maxIdleTime" value="${maxIdleTime}" />  
  28.     </bean>  
  29.       
  30.     <!-- 配置SessionFactory -->  
  31.     <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
  32.       <property name="dataSource" ref="c3p0Source" />  
  33.       <property name="mappingResources">  
  34.           <list>  
  35.             <value>/com/cdzg/spring/bean/User.hbm.xml</value>  
  36.           </list>  
  37.       </property>  
  38.       <property name="hibernateProperties">  
  39.         <props>  
  40.                 <prop key="hibernate.dialect">${dialect}</prop>  
  41.                 <prop key="hibernate.hbm2ddl.auto">${hbm2ddl.auto}</prop>  
  42.                 <prop key="hibernate.show_sql">${show_sql}</prop>  
  43.                 <prop key="hibernate.format_sql">${format_sql}</prop>  
  44.                 <prop key="hibernate.use_sql_comments">${use_sql_comments}</prop>  
  45.             </props>  
  46.       </property>  
  47.     </bean>  
  48.       
  49.     <!-- 配置事务管理器 -->  
  50.     <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
  51.       <property name="sessionFactory" ref="sessionFactory" />  
  52.     </bean>  
  53.       
  54.     <!-- 定义事务通知 -->  
  55.     <tx:advice id="txAdvice" transaction-manager="txManager">  
  56.       <tx:attributes>  
  57.         <tx:method name="get*" read-only="true"/>  
  58.         <tx:method name="*"/>  
  59.       </tx:attributes>  
  60.     </tx:advice>  
  61.        
  62.      <!-- 定义事务切面,并应用事务通知 -->      
  63.      <aop:config>  
  64.       <aop:pointcut id="xxxBizImpl" expression="execution(* com.cdzg.spring.biz.*.*(..))"/>  
  65.       <aop:advisor pointcut-ref="xxxBizImpl" advice-ref="txAdvice"/>  
  66.      </aop:config>  
  67.            
  68.     <bean id="userDaoImpl" class="com.cdzg.spring.dao.impl.UserDaoImpl">  
  69.         <property name="sessionFactory" ref="sessionFactory" />  
  70.     </bean>  
  71.     <bean id="userBizImpl" class="com.cdzg.spring.biz.impl.UserBizImpl">  
  72.         <property name="userDao" ref="userDaoImpl" />  
  73.     </bean>  
  74.     <bean id="userAction" class="com.cdzg.spring.web.actions.UserAction">  
  75.         <property name="userBiz" ref="userBizImpl" />  
  76.     </bean>  
  77. </beans>  
两种配置最大的区别就是注解方式不用在写O/R映射配置文件而xml方式实现的要配置O/R映射配置文件


注解的这种方式,直接扫描bean包就可以,剩下的对应关系由框架完成


而xml配置方式要配置O/R 映射文件并在这里指定文件,如果多的话可以使用通配符 "*"


转载地址:http://my.oschina.NET/xiaoen/blog/122439

1 0
原创粉丝点击