Spring整合mybatis--Spring中的事务管理(注解形式)
application.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.bboy.service.impl"/>
<context:component-scan base-package="com.bboy.mapper"/>
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/qinmanage"/>
<property name="username" value="root"/>
<property name="password" value="127003"/>
</bean>
<!--事务的配置-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--将事务管理器交给spring容器进行管理-->
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="typeAliasesPackage" value="com.bboy.pojo"/>
<property name="dataSource" ref="dataSource"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.bboy.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
</beans>
service
AccountService
package com.bboy.service;
public interface AccountService {
public int pay(int id_1,int id_2,int money);
}
AccountServiceImpl
package com.bboy.service.impl;
import com.bboy.mapper.AccountMapper;
import com.bboy.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
/**
* @类描述:
* @作者:秦帅
* @时间:2023/11/24 0024 11:49:08
*/
@Service("accountService")
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountMapper accountMapper ;
public void setAccountMapper(AccountMapper accountMapper) {
this.accountMapper = accountMapper;
}
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT)
@Override
public int pay(int id_1, int id_2, int money) {
//-:转出 id_1
accountMapper.updateId1(id_1, money);
//-停电了
// int res = 1/0 ;
//-:转入 id_2
accountMapper.updateId2(id_2, money);
return 0;
}
}
pojo(Account)
package com.bboy.pojo;
import lombok.Data;
@Data
/**
* @类描述:
* @作者:秦帅
* @时间:2023/11/24 0024 10:45:51
*/
public class Account {
private int id ;
private String name ;
private int money ;
}
AccountMapper
package com.bboy.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
@Mapper
public interface AccountMapper {
//- 转出
@Update("update t_account set money=money-${money} where id=${id}")
public int updateId1(@Param("id") int id, @Param("money") int money);
//- 转入
@Update("update t_account set money=money+${money} where id=${id}")
public int updateId2(@Param("id") int id,@Param("money") int money);
}
demo
package com.bboy.demo;
import com.bboy.service.AccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Demo {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
AccountService accountService = (AccountService) context.getBean("accountService");
accountService.pay(1,2,100);
}
}
标签:11,24,int,Spring,org,public,money,import,id
From: https://www.cnblogs.com/Kbaor/p/17853458.html