使用maven用spring集成mybatis开启事务aspectJ
- 加入maven依赖
<!--spring的依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${version}</version>
</dependency>
<!--spring的事务依赖-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${version}</version>
</dependency>
<!--spring事务-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${version}</version>
</dependency>
<!--mybatis的依赖-->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${version}</version>
</dependency>
<!--mybatis和spring集成的依赖-->
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${version}</version>
</dependency>
<!--mysql的依赖-->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${version}</version>
</dependency>
<!--阿里公司数据库连接池-->
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${version}</version>
</dependency>
<!--加入分页依赖-->
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>${version}</version>
</dependency>
<!--aspectj依赖-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${version}</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>${version}</filtering>
</resource>
</resources>
</build>
- 配置spring配置文件
- applicationContext.xml
<!--配置properties文件位置-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置数据源 阿里公司的Druid 作用是连接数据库-->
<bean id="myDateSource"
class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<!--set注入-->
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--创建SqlSessionFactory对象-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--连接上连接池数据库-->
<property name="dataSource" ref="myDateSource"/>
<property name="configLocation" value="classpath:mybatis.xml"/>
</bean>
<!--使用SessionFactory创建dao对象-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--指定SqlSessionFactory对象的id-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="${dao包的位置}"/>
</bean>
<!-- 创建service对象
你自己的service包,里面的方法
-->
<bean id="service" class="${}">
<property name="${}" ref="*dao"/>
<property name="${}" ref="*dao"/>
</bean>
<!--声明式事务处理:和源代码完全分离的-->
<!--1、声明事务管理器对象-->
<bean id="myTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="myDateSource"/>
</bean>
<!--2、声明业务方法和它的事务属性(隔离级别,传播行为,超时时间
id:自定义名称,表示<tx:advice>和</tx:advice>之间的配置内容的
transaction-manager:事务管理器对象的id
-->
<tx:advice id="myAdvice" transaction-manager="myTransactionManager">
<!--tx:attributes:配置事务属性-->
<tx:attributes>
<!--tx:method:给具体的方法配置事务属性,method可以有多个,分别给不同的方法设置事务属性
name:方法名称,1)完整的方法名称,不带有包和类
2)方法可以使用通配符,*表示任意字符
propagation:传播行为,枚举值 默认值:REQUIRED
isolation:隔离级别 默认值:DEFAULT
rollback-for:你指定的异常类名,全限定类名,发生异常一定回滚 默认值:RuntimeException以及其子类
-->
<tx:method name="buy" propagation="REQUIRED" isolation="DEFAULT"
rollback-for="java.lang.RuntimeException"/>
<!--使用通配符,指定很多的方法-->
<!--指定增加方法-->
<tx:method name="add*" propagation="REQUIRES_NEW"/>
<!--指定修改方法-->
<tx:method name="modify*" />
<!--删除方法-->
<tx:method name="remove*" />
<!--除以上方法的其他方法,查询方法-->
<tx:method name="*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
<!--配置aop
-->
<aop:config>
<!--
配置切入点表达式:指定哪些包中类,要使用事务
id:切入点表达式的名称,唯一值
expression:切入点表达式,指定哪些类要使用事务,aspectj会创建代理对象
声明自动代理生成器,使用aspectJ框架内部的功能,创建目标对象的代理对象
创建代理对象是在内存中实现的,修改目标对象的内存中的结构,创建为代理对象
所有目标对象就是被修改后的代理对象
aspectj-autoproxy:会把spring容器中的所有目标对象,一次性都生成代理对象
-->
<!--<aop:aspectj-autoproxy />-->
<!--如果你期望目标类有接口,使用cglib代理
proxy-target-class=“true”:告诉框架,要使用cglib动态代理
execution(任意返回值 任意包中的service包中的任意包中的类.任意方法(任意返回值))
-->
<aop:pointcut id="servicePt" expression="execution(* *..service..*.*(..))"/>
<!--配置增强器:关联advice和pointcut
advice-ref:通知,上面tx:advice哪里的配置
pointcut-ref:切入点表达式的id
-->
<aop:advisor advice-ref="myAdvice" pointcut-ref="servicePt"/>
</aop:config>