spring整合hibernate的applicationContext.xml文件配置以及web.xml

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring整合hibernate的applicationContext.xml文件配置以及web.xml相关的知识,希望对你有一定的参考价值。

applicationContext.xml文件

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" 

       xmlns:tx="http://www.springframework.org/schema/tx"

       xmlns:aop="http://www.springframework.org/schema/aop"

  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

  xsi:schemaLocation="http://www.springframework.org/schema/beans 

  http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 

  http://www.springframework.org/schema/tx 

  http://www.springframework.org/schema/tx/spring-tx-3.1.xsd

  http://www.springframework.org/schema/aop 

  http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">

  <!-- 配置hibernate连接信息 -->

  <bean id="source" class="org.apache.commons.dbcp.BasicDataSource">

    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>

    <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/>

    <property name="username" value="james"/>

    <property name="password" value="james"/>

  </bean>

  <!-- 配置sessionFactory -->

  <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

    <property name="dataSource" ref="source"/>

    <!-- 配置hibernate其他参数 -->

    <property name="hibernateProperties">

      <props>

        <prop key="hibernate.show_sql">true</prop>

        <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>

        <prop key="javax.persistence.validation.mode">none</prop>

      </props>

    </property>

    <!-- 配置hibernate映射文件,非注解情况下 -->

    <property name="mappingResources">

      <list>

        <value>entity/Comments.hbm.xml</value>

        <value>entity/News.hbm.xml</value>

        <value>entity/User.hbm.xml</value>

        <value>entity/Topic.hbm.xml</value>

      </list>

    </property>

  </bean>

  <!-- 配置dao注入sessionFactory -->

  <bean id="userDao" class="dao.UserDaoImpl">

    <property name="sessionFactory" ref="sessionFactory"/>

  </bean>

  <!-- 向userService注入dao -->

  <bean id="userService" class="service.UserServiceImpl">

    <property name="userDao" ref="userDao"></property>

  </bean>

  <!-- 定义事务管理器  -->

  <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">

    <property name="sessionFactory" ref="sessionFactory"/>

  </bean>

  <!-- 配置事务增强 -->

  <tx:advice id="txAdvice" transaction-manager="txManager">

    <!-- 定义属性,声明事务规则 -->

    <tx:attributes>

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

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

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

      <tx:method name="add*" propagation="REQUIRED"/>

      <tx:method name="del*" propagation="REQUIRED"/>

      <tx:method name="update*" propagation="REQUIRED"/>

      <tx:method name="do*" propagation="REQUIRED"/>

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

    </tx:attributes>

  </tx:advice>

  <aop:config>

    <!-- 配置切入点 -->

    <aop:pointcut expression="execution(* service.*.*(..))" id="serviceMethod"/>

    <!-- 将事物增前和切入点结合 -->

    <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod"/>

  </aop:config>

</beans>

web.xml文件

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.0" 

xmlns="http://java.sun.com/xml/ns/javaee" 

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

  <display-name></display-name>

  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

  <context-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>classpath:applicationContext.xml</param-value>

  </context-param>

  <listener>

    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

  </listener>

  <!-- OpenSessionInViewFilter,配置次功能可以延时加载,即保持会话随事件开启 -->

  <filter>

    <filter-name>OpenSessionInViewFilter</filter-name>

    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>

  </filter>

  <filter-mapping>

    <filter-name>OpenSessionInViewFilter</filter-name>

    <url-pattern>*.action</url-pattern>

  </filter-mapping>

</web-app>



以上是关于spring整合hibernate的applicationContext.xml文件配置以及web.xml的主要内容,如果未能解决你的问题,请参考以下文章

spring整合hibernate

Spring整合Hibernate详细步骤

spring和hibernate的整合

hibernate与spring整合

Spring:Spring整合Hibernate,之后整合Struts2

Spring_Hibernate整合准备