spring hibernate 整合

关键字: spring hibernate 配置
Spring和hibernate已成为目前开发的最常用框架,所以开发人员常常需要去配置他们,通过配置把他们整合在一起。这用配置时常很麻烦,特别对于新手而言,就算是老手也需要查资料或者参照以往项目的资料。这些配制涉及的方面较多包括:数据源,事务,Hibernate
以及J2EE容器的资源等等。为了方便大家,下面我给出了一份常用的配置模版,可以适用于部署在J2EE容器内或容器外(注意其中的注释部分)。
Java代码 复制代码

   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:jee="http://www.springframework.org/schema/jee" 
   7.        xsi:schemaLocation=" 
   8.        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd 
   9.        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd 
  10.        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd 
  11.        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd"> 
  12. <!--    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
  13.        <property name="locations"> 
  14.        <list> 
  15.        <value>hibernate.properties</value> 
  16.         
  17.        </list> 
  18.        </property> 
  19.     </bean>    --> 
  20.  <!-- DAO Configuration --> 
  21.   
  22.  <bean id="managedGroupDAO" class="com.topsec.tsm.framework.ManagedGroupDAOOraImp"> 
  23.   <property name="sessionFactory" ref="sessionFactory"></property> 
  24.  </bean> 
  25.    <bean id="dashBoardDAO" class="com.topsec.tsm.sim.dashboard.ad.DashBoardDAOOraImp"> 
  26.   <property name="sessionFactory" ref="sessionFactory"></property> 
  27.  </bean> 
  28.  <!-- Facade Configuration --> 
  29.  <bean id="com.topsec.tsm.framework.ManagedGroupFacade" class="com.topsec.tsm.framework.ManagedGroupFacadeImp"> 
  30.   <property name="dao" ref="managedGroupDAO"></property> 
  31.  </bean> 
  32.    <bean id="com.topsec.tsm.sim.dashboard.ad.DashBoardFacade" class="com.topsec.tsm.sim.dashboard.ad.DashBoardFacadeImp"> 
  33.   <property name="dao" ref="dashBoardDAO"></property> 
  34.  </bean> 
  35.  <!-- the transaction configuration --> 
  36.   
  37.  <tx:advice id="txAdvice" transaction-manager="txManager"> 
  38.     <!-- the transactional semantics... --> 
  39.      
  40.     <tx:attributes>       
  41.       <tx:method name="*" propagation="REQUIRED"/> 
  42.     </tx:attributes> 
  43.  </tx:advice> 
  44.   
  45.  <!--   <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
  46.     <property name="dataSource" ref="dataSource"/> 
  47.  </bean>--> 
  48.  <bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager"> 
  49.        <property name="transactionManagerName" value="java:/TransactionManager" /> 
  50.  </bean> 
  51.        
  52.  <aop:config> 
  53.     <aop:pointcut id="managedGroupOperation" expression="execution(* *..ManagedGroupFacadeImp.*(..))"/> 
  54.      <aop:pointcut id="dashBoardOperation" expression="execution(* *..DashBoardFacadeImp.*(..))"/> 
  55.     <aop:advisor advice-ref="txAdvice" pointcut-ref="managedGroupOperation"/> 
  56.     <aop:advisor advice-ref="txAdvice" pointcut-ref="dashBoardOperation"/> 
  57.  </aop:config> 
  58.   
  59.  <!-- Data source configuration --> 
  60.   
  61. <!--   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 
  62.     <property name="driverClassName" value="com.microsoft.jdbc.sqlserver.SQLServerDriver"/> 
  63.     <property name="url" value="jdbc:microsoft:sqlserver://192.168.97.93:1433;DatabaseName=dashboard"/> 
  64.     <property name="username" value="sa"/> 
  65.     <property name="password" value="talent"/> 
  66.  </bean>--> 
  67.    <jee:jndi-lookup id="dataSource" jndi-name="java:SIM_DS" /> 
  68.  <!-- hibernate configuration --> 
  69.  <!-- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
  70.     <property name="dataSource" ref="dataSource"/> 
  71.     <property name="mappingResources"> 
  72.       <list> 
  73.         <value>mappings/DashBoard.hbm.xml</value> 
  74.         <value>mappings/Instrument.hbm.xml</value> 
  75.         <value>mappings/ManagedGroup.hbm.xml</value> 
  76.         <value>mappings/Monitor.hbm.xml</value> 
  77.         <value>mappings/Resource.hbm.xml</value> 
  78.       </list> 
  79.     </property> 
  80.     
  81.     <property name="hibernateProperties"> 
  82.       <props> 
  83.         <prop key="hibernate.dialect">${hibernate.dialect}</prop> 
  84.         <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> 
  85.         <prop key="hbm2ddl.auto">${hbm2ddl.auto}</prop> 
  86.         
  87.       </props> 
  88.     </property> 
  89.  </bean> --> 
  90.  <jee:jndi-lookup id="sessionFactory" jndi-name="java:/hibernate/SessionFactory" /> 
  91.   
  92. </beans> 

你可能感兴趣的:(DAO,spring,sql,Hibernate,xml)