本例演示转账操作
- 准备数据库表t_act(账户表)

- 创建maven模块,引入依赖
| <dependencies> |
| <dependency> |
| <groupId>org.springframework</groupId> |
| <artifactId>spring-jdbc</artifactId> |
| <version>6.1.8</version> |
| </dependency> |
| |
| <dependency> |
| <groupId>mysql</groupId> |
| <artifactId>mysql-connector-java</artifactId> |
| <version>8.0.29</version> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.mybatis</groupId> |
| <artifactId>mybatis</artifactId> |
| <version>3.5.16</version> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.mybatis</groupId> |
| <artifactId>mybatis-spring</artifactId> |
| <version>3.0.3</version> |
| |
| </dependency> |
| |
| <dependency> |
| <groupId>com.alibaba</groupId> |
| <artifactId>druid</artifactId> |
| <version>1.1.17</version> |
| </dependency> |
| <dependency> |
| <groupId>org.springframework</groupId> |
| <artifactId>spring-context</artifactId> |
| <version>6.1.7</version> |
| </dependency> |
| <dependency> |
| <groupId>junit</groupId> |
| <artifactId>junit</artifactId> |
| <version>4.12</version> |
| <scope>test</scope> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.apache.logging.log4j</groupId> |
| <artifactId>log4j-core</artifactId> |
| <version>2.7</version> |
| </dependency> |
| </dependencies> |
| package com.powernode.bank.pojo; |
| |
| public class Account { |
| |
| private String actno; |
| private Double balance; |
| |
| |
| |
| |
| public String getActno() { |
| return actno; |
| } |
| |
| public void setActno(String actno) { |
| this.actno = actno; |
| } |
| |
| public Double getBalance() { |
| return balance; |
| } |
| |
| public void setBalance(Double balance) { |
| this.balance = balance; |
| } |
| |
| |
| |
| public Account(String actno, Double balance) { |
| this.actno = actno; |
| this.balance = balance; |
| } |
| |
| @Override |
| public String toString() { |
| return "Account{" + |
| "actno='" + actno + '\'' + |
| ", balance=" + balance + |
| '}'; |
| } |
| } |
| package com.powernode.bank.mapper; |
| |
| import com.powernode.bank.pojo.Account; |
| |
| import java.util.List; |
| |
| public interface AccountMapper { |
| |
| int insert(Account account); |
| int deleteByActno(String actno); |
| int update(Account account); |
| Account selectByActno(String actno); |
| List<Account> selectAll(); |
| } |
- 编写Mapper配置文件,该配置文件需要和Mapper接口在同一个目录或在resource目录下创建和Mapper接口相同目录结构
| <?xml version="1.0" encoding="UTF-8" ?> |
| <!DOCTYPE mapper |
| PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" |
| "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
| |
| <mapper namespace="com.powernode.bank.mapper.AccountMapper"> |
| <insert id="insert"> |
| insert into t_act values(#{actno}, #{balance}) |
| </insert> |
| |
| |
| <delete id="deleteByActno"> |
| delete from t_act where actno = #{actno} |
| </delete> |
| |
| <update id="update"> |
| update t_act set balance = #{balance} where actno = #{actno} |
| </update> |
| |
| <select id="selectByActno" resultType="com.powernode.bank.pojo.Account"> |
| select * from t_act where actno = #{actno} |
| </select> |
| <select id="selectAll" resultType="com.powernode.bank.pojo.Account"> |
| select * from t_act |
| </select> |
| </mapper> |
| package com.powernode.bank.service; |
| |
| import com.powernode.bank.pojo.Account; |
| |
| import java.util.List; |
| |
| public interface AccountService { |
| |
| int save(Account act); |
| |
| int deleteByActno(String actno); |
| |
| int modify(Account account); |
| |
| Account getByActno(String actno); |
| |
| List<Account> getAll(); |
| |
| void transfer(String fromActno, String toActno, double money); |
| } |
| package com.powernode.bank.service.impl; |
| |
| import com.powernode.bank.mapper.AccountMapper; |
| import com.powernode.bank.pojo.Account; |
| import com.powernode.bank.service.AccountService; |
| import org.springframework.beans.factory.annotation.Autowired; |
| import org.springframework.stereotype.Service; |
| import org.springframework.transaction.annotation.Transactional; |
| |
| import java.util.List; |
| |
| @Transactional |
| @Service |
| public class AccountServiceImpl implements AccountService { |
| |
| @Autowired |
| private AccountMapper accountMapper; |
| |
| @Override |
| public int save(Account act) { |
| return accountMapper.insert(act); |
| } |
| |
| @Override |
| public int deleteByActno(String actno) { |
| return accountMapper.deleteByActno(actno); |
| } |
| |
| @Override |
| public int modify(Account account) { |
| return accountMapper.update(account); |
| } |
| |
| @Override |
| public Account getByActno(String actno) { |
| return accountMapper.selectByActno(actno); |
| } |
| |
| @Override |
| public List<Account> getAll() { |
| return accountMapper.selectAll(); |
| } |
| |
| @Override |
| public void transfer(String fromActno, String toActno, double money) { |
| Account fromAccount = accountMapper.selectByActno(fromActno); |
| if (fromAccount.getBalance()< money){ |
| throw new RuntimeException("余额不足"); |
| }; |
| Account toAct = accountMapper.selectByActno(toActno); |
| fromAccount.setBalance(fromAccount.getBalance()-money); |
| toAct.setBalance(toAct.getBalance()+money); |
| int count = accountMapper.update(fromAccount); |
| count += accountMapper.update(toAct); |
| if (count != 2){ |
| throw new RuntimeException("转账失败"); |
| } |
| } |
| } |
| jdbc.driver=com.mysql.cj.jdbc.Driver |
| jdbc.url=jdbc:mysql://192.168.1.41:3306/test |
| jdbc.username=root |
| jdbc.password=********* |
| <?xml version="1.0" encoding="UTF-8" ?> |
| <!DOCTYPE configuration |
| PUBLIC "-//mybatis.org//DTD Config 3.0//EN" |
| "http://mybatis.org/dtd/mybatis-3-config.dtd"> |
| <configuration> |
| <settings> |
| <setting name="logImpl" value="STDOUT_LOGGING"/> |
| </settings> |
| </configuration> |
| <?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" |
| xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> |
| |
| |
| <context:component-scan base-package="com.powernode.bank"/> |
| |
| |
| <context:property-placeholder location="jdbc.properties"/> |
| |
| |
| <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> |
| <property name="driverClassName" value="${jdbc.driver}"/> |
| <property name="url" value="${jdbc.url}"/> |
| <property name="username" value="${jdbc.username}"/> |
| <property name="password" value="${jdbc.password}"/> |
| </bean> |
| |
| |
| <bean class="org.mybatis.spring.SqlSessionFactoryBean"> |
| |
| <property name="dataSource" ref="dataSource"/> |
| |
| <property name="configLocation" value="mybatis-config.xml"/> |
| |
| <property name="typeAliasesPackage" value="com.powernode.bank.pojo"/> |
| </bean> |
| |
| |
| <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> |
| <property name="basePackage" value="com.powernode.bank.mapper"/> |
| </bean> |
| |
| |
| <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> |
| <property name="dataSource" ref="dataSource"/> |
| </bean> |
| |
| |
| <tx:annotation-driven transaction-manager="txManager"/> |
| </beans> |
| public class SMTest { |
| @Test |
| public void testSM(){ |
| ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("spring.xml"); |
| AccountService accountServiceImpl = classPathXmlApplicationContext.getBean("accountServiceImpl", AccountService.class); |
| try{ |
| accountServiceImpl.transfer("act-001","act-002", 10000); |
| System.out.println("转账成功"); |
| }catch(Exception e){ |
| e.printStackTrace(); |
| } |
| |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)