`

Spring mvc 和HIbernate结合 通过注解进行开发

 
阅读更多

1.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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 <context:component-scan base-package="com.sinovatech" />
 <!-- 定义数据源的Bean ,给Hibernate的sessionFactory-->
 <bean id="dataSource"
  class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName"
   value="oracle.jdbc.driver.OracleDriver">
  </property>
  <property name="url"
   value="jdbc:oracle:thin:@192.168.1.111:1521:ecom">
  </property>
  <property name="username" value="jxbms"></property>
  <property name="password" value="jxbms"></property>
 </bean>
 
<!-- 定义Hibernate的sessionFactory,通过该Bean,可以获得Hibernate的Session-->
 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  <property name="dataSource">
   <ref bean="dataSource" />
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">
     org.hibernate.dialect.Oracle9Dialect
    </prop>
    <!--设置二级缓冲-->
    <prop key="hibernate.cache.provider_class">
     org.hibernate.cache.EhCacheProvider
    </prop>
    <!--设置二级缓冲,打开查询缓冲-->
    <prop key="hibernate.cache.use_query_cache">true</prop>
    <!--设置显示Hibernate操作的SQL语句-->
    <prop key="hibernate.show_sql">true</prop>
   </props>
  </property>
  <property name="packagesToScan"> 
            <list>
             <value>com.sinovatech.example.model</value>
            </list> 
        </property>

 </bean>

 <bean name="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
  <property name="sessionFactory">
   <ref local="sessionFactory"/>
  </property>
 </bean>
 
 
  <!-- 配置事务管理器 --> 
 <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory">
   <ref local="sessionFactory"/>
  </property>
 </bean>
   
    <!-- 配置事务特性 ,配置add、delete和update开始的方法,事务传播特性为required-->      
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
     <tx:attributes>
      <tx:method name="insert*" propagation="REQUIRED" />
      <tx:method name="delete*" propagation="REQUIRED" />
      <tx:method name="update*" propagation="REQUIRED" />
      <tx:method name="query*" read-only="true"/>

      <tx:method name="get*" read-only="true"/>

      <tx:method name="find*" read-only="true"/>
     </tx:attributes>
    </tx:advice>
   
    <!-- 配置那些类的方法进行事务管理,当前cn.com.jobedu.oa.service包中的子包、类中所有方法需要,还需要参考tx:advice的设置 -->
    <aop:config>
     <aop:pointcut id="allManagerMethod" expression="execution (* com.sinovatech.example.service.*.*(..))"/>
     <aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/>
    </aop:config>       
 
 
 </beans>

 

2.servlet.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:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
 <!--注意加载顺序,并且关于ACTION请求全部放在servlet.xml页面  --> 
 <context:component-scan base-package="com.sinovatech.example.action" >
 </context:component-scan>
 <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
 </bean>
 
 <bean
  id="urlMapping"
  class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
 <bean
  class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
  
 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/WEB-INF/jsp/"/>
  <property name="suffix" value=".jsp"></property>
 </bean>
 
</beans>

 

附件是jar列表和工程项目名称

 

 

  • 大小: 52.2 KB
  • 大小: 40.4 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics