mybatis_04:整合spring

前言

你可以把mybatis整合到spring, 也可以把事务交给spring来管理。

你也可以同时使用mybatis和spring,而不把mybatis整合到spring,mybatis自己管理事务。

mybatis的事务是如何被spring接管的?
事务底层是jdbc事务。
sqlSession最终走的还是jdbc connection.

基本配置

第一步:注入sqlSessionFactory

In MyBatis-Spring, an SqlSessionFactoryBean is used to create an SqlSessionFactory . To configure the factory bean, put the following in the Spring XML configuration file:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
</bean>

第二步:扫描并注入mapper对象

有三种方式,
Using the <mybatis:scan/> element.
Using the annotation @MapperScan
Using a classic Spring xml file and registering the MapperScannerConfigurer

第三种方式:MapperScannerConfigurer

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  <property name="basePackage" value="org.mybatis.spring.sample.mapper" />
</bean>

第三步:事务管理

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="dataSource" />
</bean>


@Transactional
在service层注解,指定业务逻辑是事务的。
可以注解在service class上,也可以注解在方法上。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值