基于的注解来管理事务@Transactional 此注解打在方法上和类上都可以
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.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">
<!-- 根据配置文件读取信息用于创建数据库连接池 -->
<!-- 创建数据库连接池 -->
<bean id="ds" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
// 每个人的后缀名可能不太一样
<property name="url" value="jdbc:oracle:thin:@localhost:1521/orcl.navicat.com"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 创建SqlSessionFactoryBean用于执行sql语句 -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="ds"></property>
</bean>
<!-- 扫描映射文件创建代理对象 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zt.dao"></property>
</bean>
<!-- 创建事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据源-->
<property name="dataSource" ref="ds"></property>
</bean>
#
<!-- 配置环绕通知--> // 如果是基于子类的话就需要配置这个方法 proxy-target-class
//基于子类指的是 不是通过service接口直接调用的方法
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
</beans>
基于xml管理事务
<!--事务配置-->
<tx:advice transaction-manager="transactionManager" id="interceptor">
<tx:attributes>
<tx:method name="find*" read-only="true" propagation="SUPPORTS"/>
<tx:method name="*" propagation="REQUIRED" read-only="false"></tx:method>
</tx:attributes>
</tx:advice>
<!-- aop指定路径-->
<aop:config proxy-target-class="true">
<!-- 配置指定路径-->
<aop:advisor advice-ref="interceptor" pointcut="execution(* com.zt.impl.*.*(..))"></aop:advisor>
</aop:config>