mybatis+spring事务管理

本文介绍如何在MyBatis结合Spring框架中实现注解式事务管理。主要内容包括配置数据源、创建SqlSessionFactory及TransactionManager,并启用事务注解驱动。文章强调了数据源一致性的重要性及注解的有效范围。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

开启mybatis+spring注解式事物管理

在mybatis的配置文件中mybatis.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:p="http://www.springframework.org/schema/p"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xmlns:util="http://www.springframework.org/schema/util"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<!-- BoneCP配置 <bean id="jndisource1" class="org.springframework.jndi.JndiObjectFactoryBean"> 
<property name="jndiName" > <value>hxsmsdb</value> </property> </bean> -->


<!-- BoneCP配置 -->
<bean id="jndisource" class="com.jolbox.bonecp.BoneCPDataSource"
destroy-method="close">
<property name="driverClass" value="${oracle.jdbc.driverClassName}" />
<property name="jdbcUrl" value="${oracle.jdbc.url}" />
<property name="username" value="${oracle.jdbc.username}" />
<property name="password" value="${oracle.jdbc.password}" />
<!-- 检查数据库连接池中空闲连接的间隔时间,单位是分,默认值:240,如果要取消则设置为0 -->
<property name="idleConnectionTestPeriodInMinutes" value="6" />
<!-- 连接池中未使用的链接最大存活时间,单位是分,默认值:60,如果要永远存活设置为0 -->
<property name="idleMaxAgeInMinutes" value="2" />
<!-- 每个分区最大的连接数 -->
<property name="maxConnectionsPerPartition" value="3" />
<!-- 每个分区最小的连接数 -->
<property name="minConnectionsPerPartition" value="1" />
<!-- 分区数 ,默认值2,最小1,推荐3-4,视应用而定 -->
<property name="partitionCount" value="3" />
<!-- 每次去拿数据库连接的时候一次性要拿几个,默认值:2 -->
<property name="acquireIncrement" value="5" />
<!-- 缓存prepared statements的大小,默认值:0 -->
<property name="statementsCacheSize" value="100" />
<!-- 每个分区释放链接助理进程的数量,默认值:3,除非你的一个数据库连接的时间内做了很多工作,不然过多的助理进程会影响你的性能 -->
<property name="releaseHelperThreads" value="3" />
</bean>

  <!---->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg>
<ref bean="jndisource" />
</constructor-arg>
</bean>


<!-- define the SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="jndisource" />
<property name="configLocation" value="classpath:/config/ibatis/mybatis-config.xml" />
<property name="typeAliasesPackage" value="com.yld" />
<property name="mapperLocations" value="classpath:/config/sqlmap/*Mapper.xml" />
</bean>


<!-- mybatis接口, 可以使用分号或逗号 作为分隔符设置多于一个的包路径 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.yld.publicservice.springweb.dao" />
</bean>

<!-- 数据连接事务 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="jndisource" />
</bean>


<!-- 连接事务的注解配置 -->
<tx:annotation-driven transaction-manager="transactionManager"
proxy-target-class="true" />



</beans>

红色的部分是需要加入的

*1、需要注意的有sqlSessionFactory和transactionManager的数据源一定要相同,

*2、而且注解在方法上面默认捕捉的是runtimeexception,同时这个注解只有在外部调用的第一个方法上面才有效,

例如:类A中的testa调用类B中的testb,testb调用本类中的testsunb,该注解@Transactional只有放在testb上面才有效,如果放在testsunb方法上面是没有效果的,同时@Transactional这个值针对public方法有效,其他也是没有效果的。

这只是我自粗浅的理解,菜鸟mark一下。








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值