您现在的位置是:首页 > 个人日记个人日记

spring-12-事务管理-xml配置

2019-02-03 18:57:46【个人日记】224人已围观

简介对事务的管理,XML配置

事务管理-xml配置

配置spring-config.xml文件

  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:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  6. 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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
  7. <!--扫描注解-->
  8. <context:component-scan base-package="com.huangxin.order"></context:component-scan>
  9. <!--AOP注解生效-->
  10. <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
  11. <!--加载配置文件-->
  12. <context:property-placeholder location="property/jdbc.properties"></context:property-placeholder>
  13. <!--druid连接池-->
  14. <bean id="dateSource" class="com.alibaba.druid.pool.DruidDataSource">
  15. <property name="url" value="${jdbc.url}"></property>
  16. <property name="username" value="${jdbc.username}"></property>
  17. <property name="password" value="${jdbc.password}"></property>
  18. </bean>
  19. <!--SqlSessionFactory工厂交给spring-->
  20. <bean id="sqlSessionFactory" name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  21. <!--将数据源注入SqlSessionFactory-->
  22. <property name="dataSource" ref="dateSource"></property>
  23. <!--引入映射-->
  24. <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
  25. </bean>
  26. <!--让spring生成mapper接口的实现类-->
  27. <!--多个mapper接口-->
  28. <!--MapperScannerConfigurer将扫描所有包下的mapper-->
  29. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  30. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
  31. <!--对哪些包下的类生成mapper代理类-->
  32. <property name="basePackage" value="com.huangxin.order.mapper"></property>
  33. </bean>
  34. <!--事务管理-->
  35. <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  36. <property name="dataSource" ref="dateSource"></property>
  37. </bean>
  38. <!--配置事务增强-->
  39. <!--isolation事务隔离级别-->
  40. <!--注意命名规范-->
  41. <tx:advice id="txAdvice" transaction-manager="txManager">
  42. <tx:attributes>
  43. <tx:method name="delete*" isolation="DEFAULT" propagation="REQUIRED"/>
  44. <tx:method name="add*" isolation="DEFAULT" propagation="REQUIRED"/>
  45. <tx:method name="insert*" isolation="DEFAULT" propagation="REQUIRED"/>
  46. <tx:method name="update*" isolation="DEFAULT" propagation="REQUIRED"/>
  47. </tx:attributes>
  48. </tx:advice>
  49. <!--增强事务-->
  50. <aop:config>
  51. <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.huangxin.order.service.*.*(..))"/>
  52. </aop:config>
  53. </beans>

注意

这样使用集中配置,一旦配好不用再次配置,

但是配置比较麻烦.需要细心

Tags: JavaWeb  

评论区

    2024-04-20 01:15:55

    站长

    没有登录功能是为了方便大家留言,但留言接口现在被恶意攻击,将关闭留言接口,如有疑问,请联系我的QQ 1538933906/同微信


文章评论



给自个选个头像吧!






站点信息

  • 建站时间:   2019-01-31
  • 网站程序:   Tomcat+nginx
  • 文章统计:   44篇文章
  • 标签管理:   标签云
  • 微信公众号:  扫描二维码,联系我