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

spring-13-事务管理-注解

2019-02-03 18:59:18【个人日记】283人已围观

简介事务管理注解实现

事务管理-注解

这种方式比较简单
配置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. <!--将事务管理器交给spring管理-->
  39. <!--使用注解方式-->
  40. <!--如果使用注解则不使用xml配置-->
  41. <tx:annotation-driven transaction-manager="txManager"/>
  42. </beans>

注意

这种方式虽然比较简单,但是容易忘记,配置比较分散

记得在每一个需要管理的事务方法上加上@Transactional注解

Tags: JavaWeb  

评论区

    2024-04-16 16:52:53

    站长

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

    2019-02-05 11:24:52

    jj

    测试方法


文章评论



给自个选个头像吧!






站点信息

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