您现在的位置是:首页 > 个人日记个人日记
spring-13-事务管理-注解
2019-02-03 18:59:18【个人日记】351人已围观
简介事务管理注解实现
事务管理-注解
这种方式比较简单
配置spring-config.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"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"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"><!--扫描注解--><context:component-scan base-package="com.huangxin.order"></context:component-scan><!--AOP注解生效--><aop:aspectj-autoproxy></aop:aspectj-autoproxy><!--加载配置文件--><context:property-placeholder location="property/jdbc.properties"></context:property-placeholder><!--druid连接池--><bean id="dateSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="url" value="${jdbc.url}"></property><property name="username" value="${jdbc.username}"></property><property name="password" value="${jdbc.password}"></property></bean><!--SqlSessionFactory工厂交给spring--><bean id="sqlSessionFactory" name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!--将数据源注入SqlSessionFactory--><property name="dataSource" ref="dateSource"></property><!--引入映射--><property name="mapperLocations" value="classpath:mapper/*.xml"></property></bean><!--让spring生成mapper接口的实现类--><!--多个mapper接口--><!--MapperScannerConfigurer将扫描所有包下的mapper--><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property><!--对哪些包下的类生成mapper代理类--><property name="basePackage" value="com.huangxin.order.mapper"></property></bean><!--事务管理--><bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dateSource"></property></bean><!--将事务管理器交给spring管理--><!--使用注解方式--><!--如果使用注解则不使用xml配置--><tx:annotation-driven transaction-manager="txManager"/></beans>
注意
这种方式虽然比较简单,但是容易忘记,配置比较分散
记得在每一个需要管理的事务方法上加上@Transactional注解
Tags: JavaWeb
上一篇: mybatis-07-单表一对多查询
相关文章
随机图文
评论区
2025-11-09 06:25:03
站长
没有登录功能是为了方便大家留言,但留言接口现在被恶意攻击,将关闭留言接口,如有疑问,请联系我的QQ 1538933906/同微信

2019-02-05 11:24:52
测试方法