温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-07-04 22:09:17 来源:网络 阅读:1449 作者:封起De日子 栏目:开发技术

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>



向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI