在开发项目中,事务处理是重中之重,那么在应用SSM框架的项目中,事务该怎么配置? 是一个一个接口的敲上@Transactional 注解? 固然不是,Spring的AOP处理,很好的帮咱们解决了这个问题; 咱们就来看看Spring中怎么配置全局事务:java
<?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:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <context:component-scan base-package="com.xt.shop.base.service.impl" /> <context:annotation-config /> <tx:annotation-driven /> <!-- 读取JDBC的配置--> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations" value="classpath:jdbc.properties"/> </bean> <!-- 配置数据源,从上面配置文件读取--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <!-- MyBatis_Plus配置: --> <bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 配置扫描实体的包路径 --> <property name="typeAliasesPackage" value="com.xt.shop.base.entity"/> <!-- 配置扫描 MAPPER XML的位置 --> <property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"/> <!-- 配置 MyBatis配置文件的位置 --> <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/> <property name="globalConfig" ref="globalConfig"/> <!-- 配置插件 --> <property name="plugins"> <array> <!-- 分页插件配置 --> <bean id="paginationInterceptor" class="com.baomidou.mybatisplus.plugins.PaginationInterceptor"> <property name="dialectType" value="mysql"/> <property name="optimizeType" value="aliDruid" /> </bean> </array> </property> </bean> <!-- MP 全局配置注入 --> <bean id="globalConfig" class="com.baomidou.mybatisplus.entity.GlobalConfiguration"> <property name="idType" value="0"/> <property name="dbColumnUnderline" value="true"/> </bean> <!-- 配置扫描MAPPER接口的包路径 --> <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.xt.shop.base.mapper"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> <!-- 事务处理 --> <bean id= "transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 事务注解:开启注解支持--> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> <!-- 全局AOP事物,除get,list,select,query开头的方法外,都处在事物当中 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="select*" propagation="REQUIRED" read-only="true" /> <tx:method name="get*" propagation="REQUIRED" read-only="true" /> <tx:method name="list*" propagation="REQUIRED" read-only="true" /> <tx:method name="query*" propagation="REQUIRED" read-only="true" /> <tx:method name="*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception"/> </tx:attributes> </tx:advice> <!-- 配置事务切面 到Service层 --> <aop:config expose-proxy="true" proxy-target-class="true" > <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.xt.shop.base.service..*.*(..))"/> </aop:config> </beans>
因为习惯了使用MyBatis-Plus,因此我这里的使用MyBatis-Plus,不过MyBatis的配置方式都是相同的;mysql
事务处理之这块须要注意的是:spring
<!-- 配置事务切面 到Service层 --> <aop:config expose-proxy="true" proxy-target-class="true" > <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.xt.shop.base.service..*.*(..))"/> </aop:config>
我当时就是由于 service 后面少写了一个点,就致使事务回滚一直不生效; 这一块主要应用了Mybatis动态代理的方式来锁定须要处理事务的接口,若是路径有误,那根本就代理不了,事务也不会生效;sql
还有, 事务层是配置在代码的service层,因此,业务只有写到service层才会生效,而在controller层处理业务是不会发生事务的; 因此咱们在写项目的时候,最好全部的业务都放在service中处理,养成良好的编码习惯;mybatis