Spring整合mybatis
这里介绍了Spring整合mybatisSpring整合mybatis
现在介绍Spring的事务管理
Spring的事务管理
方式1:注解@Transactional
1:开启事务注解
要使用注解,就要开启事务的注解驱动
<tx:annotation-driven/>
2:方法上方加上@Transactional
然后需要在需要事务的方法上方加上@Transactional
方式二:AOP实现事务织入
1:配置声明式事务
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <constructor-arg ref="dataSource" /> </bean>
只要配置了第一步,那么事务就已经生成,默认全部都生效,如果配置了下面的步骤,那么就指定很切到哪里
2:配置事务通知
<tx:advice id="txAdvice" transaction-manager="transactionManager"> <!-- 给哪些方法配置事务 --> <tx:attributes> <!-- 给所有的update方法设置事务,传播特性为REQUIRED。默认也是REQUIRED --> <tx:method name="update" propagation="REQUIRED"/> <!-- 给所有的query方法设置事务,只读m模式,也就是不能增删改 --> <tx:method name="query" read-only="true"/> <!-- 给所有的getStudents方法设置事务 --> <tx:method name="getStudents" /> <!-- 给所有的方法设置事务 --> <tx:method name="*" /> </tx:attributes> </tx:advice>
3:配置事务切入
<aop:config> <aop:pointcut id="txPointcut" expression="execution(* com.lingaolu.impl.*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" /> </aop:config>
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- dataSource:使用Spring的数据源替换Mybatis的配置
我们这里使用Spring提供的JDBC:org.springframework.jdbc.datasource
-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/study?userSSL=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai"/>
<property name="username" value="root"/>
<property name="password" value="password"/>
</bean>
<!-- 注入sqlSessionFactory,官网就是这样介绍的 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 绑定mybatis的配置文件 -->
<property name="configLocation" value="classpath:mybatis.xml"/>
<!-- 注册mapper文件 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
<!-- 还可以注册别名,设置setting等等,完全能替代mybatis的核心配置文件里的额所有配置 -->
</bean>
<!-- 注入sqlSessionFactory,官网就是这样介绍的 -->
<!--<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>-->
<!-- 配置声明式事务 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<constructor-arg ref="dataSource" />
</bean>
<!-- 结合AOP实现事务的织入 -->
<!-- 配置事务通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 给哪些方法配置事务 -->
<tx:attributes>
<!-- 给所有的update方法设置事务,传播特性为REQUIRED。默认也是REQUIRED -->
<tx:method name="update" propagation="REQUIRED"/>
<!-- 给所有的query方法设置事务,只读m模式,也就是不能增删改 -->
<tx:method name="query" read-only="true"/>
<!-- 给所有的getStudents方法设置事务 -->
<tx:method name="getStudents" />
<!-- 给所有的方法设置事务 -->
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<!-- 配置事务窃入 -->
<aop:config>
<aop:pointcut id="txPointcut" expression="execution(* com.lingaolu.impl.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />
</aop:config>
</beans>