DataBase
show create table account
CREATE TABLE `account` (
`actno` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`balance` int(11) DEFAULT NULL,
PRIMARY KEY (`actno`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
pom.xml
4.0.0
com.wsd
spring-mybatis
1.0-SNAPSHOT
jar
org.springframework
spring-context
6.0.6
org.springframework
spring-jdbc
6.0.6
mysql
mysql-connector-java
8.0.30
org.mybatis
mybatis
3.5.11
org.mybatis
mybatis-spring
3.0.1
com.alibaba
druid
1.2.13
junit
junit
4.13.2
test
org.apache.maven.plugins
maven-compiler-plugin
3.1
17
pojo (Plain Old Java Object)
package com.wsd.bank.pojo;
/**
* @program: spring_learn
* @description:
* @author: Mr.Wang
* @create: 2023-06-11 06:41
**/
public class Account {
private String actno;
private Double balance;
@Override
public String toString() {
return "Account{" +
"actno='" + actno + '\'' +
", balance=" + balance +
'}';
}
public Account() {
}
public Account(String actno, Double balance) {
this.actno = actno;
this.balance = 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;
}
}
mapper interface
package com.wsd.bank.mapper;
import com.wsd.bank.pojo.Account;
import java.util.List;
/**
* @program: spring_learn
* @description:
* @author: Mr.Wang
* @create: 2023-06-11 07:21
**/
public interface AccountMapper {
/**
* 保存账户
* @param account
* @return
*/
int insert(Account account);
/**
* 根据账号删除账户
* @param actno
* @return
*/
int deleteByActno(String actno);
/**
* 修改账户
* @param account
* @return
*/
int update(Account account);
/**
* 根据账号查询账户
* @param actno
* @return
*/
Account selectByActno(String actno);
/**
* 获取所有账户
* @return
*/
List selectAll();
}
mapper.xml 与mapper接口同名且同目录下
mapper
insert into account values(#{actno}, #{balance})
delete from account where actno = #{actno}
update account set balance = #{balance} where actno = #{actno}
service接口
package com.wsd.bank.service;
import com.wsd.bank.pojo.Account;
import java.util.List;
/**
* @program: spring_learn
* @description:
* @author: Mr.Wang
* @create: 2023-06-11 10:40
**/
public interface AccountService {
/**
* 开户
* @param act
* @return
*/
int save(Account act);
/**
* 根据账号销户
* @param actno
* @return
*/
int deleteByActno(String actno);
/**
* 修改账户
* @param act
* @return
*/
int update(Account act);
/**
* 根据账号获取账户
* @param actno
* @return
*/
Account getByActno(String actno);
/**
* 获取所有账户
* @return
*/
List getAll();
/**
* 转账
* @param fromActno
* @param toActno
* @param money
*/
void transfer(String fromActno, String toActno, double money);
}
service接口实现类
package com.wsd.bank.service.impl;
import com.wsd.bank.mapper.AccountMapper;
import com.wsd.bank.pojo.Account;
import com.wsd.bank.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @program: spring_learn
* @description:
* @author: Mr.Wang
* @create: 2023-06-11 10:46
**/
@Service("accountService")
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 update(Account act) {
return accountMapper.update(act);
}
@Override
public Account getByActno(String actno) {
return accountMapper.selectByActno(actno);
}
@Override
public List getAll() {
return accountMapper.selectAll();
}
@Override
public void transfer(String fromActno, String toActno, double money) {
Account fromAct = accountMapper.selectByActno(fromActno);
if (fromAct.getBalance() < money) {
throw new RuntimeException("余额不足");
}
Account toAct = accountMapper.selectByActno(toActno);
fromAct.setBalance(fromAct.getBalance() - money);
toAct.setBalance(toAct.getBalance() + money);
int count = accountMapper.update(fromAct);
count += accountMapper.update(toAct);
if (count != 2) {
throw new RuntimeException("转账失败");
}
}
}
jdbc.properties配置文件
放在类的根路径下
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/myspring
jdbc.username=root
jdbc.password=root
mybatis-config.xml配置文件 放在类的根路径下,只开启日志,其他配置到spring.xml中。
spring.xml配置文件
test:
package com.wsd;
import com.wsd.bank.service.AccountService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @program: spring_learn
* @description:
* @author: Mr.Wang
* @create: 2023-06-11 13:58
**/
public class springmybatistest {
@Test
public void testSM(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
AccountService accountService = applicationContext.getBean("accountService", AccountService.class);
try {
accountService.transfer("001", "002", 10000.0);
System.out.println("转账成功");
} catch (Exception e) {
e.printStackTrace();
System.out.println("转账失败");
}
}
}
Before testing
After testing
"C:\Program Files\Java\jdk-17\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 -Didea.launcher.port=53095 "-Didea.launcher.bin.path=C:\Minecloud\IDEA_2019\IntelliJ IDEA 2019.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Minecloud\IDEA_2019\IntelliJ IDEA 2019.1\lib\idea_rt.jar;C:\Minecloud\IDEA_2019\IntelliJ IDEA 2019.1\plugins\junit\lib\junit-rt.jar;C:\Minecloud\IDEA_2019\IntelliJ IDEA 2019.1\plugins\junit\lib\junit5-rt.jar;C:\Minecloud\IDEA_workspace\spring_learn\springmybatis\target\test-classes;C:\Minecloud\IDEA_workspace\spring_learn\springmybatis\target\classes;C:\Minecloud\maven_3.9\apache-maven-3.9.0\repo\org\springframework\spring-context\6.0.6\spring-context-6.0.6.jar;C:\Minecloud\maven_3.9\apache-maven-3.9.0\repo\org\springframework\spring-aop\6.0.6\spring-aop-6.0.6.jar;C:\Minecloud\maven_3.9\apache-maven-3.9.0\repo\org\springframework\spring-beans\6.0.6\spring-beans-6.0.6.jar;C:\Minecloud\maven_3.9\apache-maven-3.9.0\repo\org\springframework\spring-core\6.0.6\spring-core-6.0.6.jar;C:\Minecloud\maven_3.9\apache-maven-3.9.0\repo\org\springframework\spring-jcl\6.0.6\spring-jcl-6.0.6.jar;C:\Minecloud\maven_3.9\apache-maven-3.9.0\repo\org\springframework\spring-expression\6.0.6\spring-expression-6.0.6.jar;C:\Minecloud\maven_3.9\apache-maven-3.9.0\repo\org\springframework\spring-jdbc\6.0.6\spring-jdbc-6.0.6.jar;C:\Minecloud\maven_3.9\apache-maven-3.9.0\repo\org\springframework\spring-tx\6.0.6\spring-tx-6.0.6.jar;C:\Minecloud\maven_3.9\apache-maven-3.9.0\repo\mysql\mysql-connector-java\8.0.30\mysql-connector-java-8.0.30.jar;C:\Minecloud\maven_3.9\apache-maven-3.9.0\repo\com\google\protobuf\protobuf-java\3.19.4\protobuf-java-3.19.4.jar;C:\Minecloud\maven_3.9\apache-maven-3.9.0\repo\org\mybatis\mybatis\3.5.11\mybatis-3.5.11.jar;C:\Minecloud\maven_3.9\apache-maven-3.9.0\repo\org\mybatis\mybatis-spring\3.0.1\mybatis-spring-3.0.1.jar;C:\Minecloud\maven_3.9\apache-maven-3.9.0\repo\com\alibaba\druid\1.2.13\druid-1.2.13.jar;C:\Minecloud\maven_3.9\apache-maven-3.9.0\repo\junit\junit\4.13.2\junit-4.13.2.jar;C:\Minecloud\maven_3.9\apache-maven-3.9.0\repo\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar" com.intellij.rt.execution.application.AppMainV2 com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 com.wsd.springmybatistest,testSM
Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@45efc20d] was not registered for synchronization because synchronization is not active
6月 11, 2023 2:17:03 下午 com.alibaba.druid.support.logging.JakartaCommonsLoggingImpl info
信息: {dataSource-1} inited
JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@32057e6] will not be managed by Spring
==> Preparing: select * from account where actno = ?
==> Parameters: 001(String)
<== Columns: actno, balance
<== Row: 001, 50000
<== Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@45efc20d]
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@62f87c44] was not registered for synchronization because synchronization is not active
JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@32057e6] will not be managed by Spring
==> Preparing: select * from account where actno = ?
==> Parameters: 002(String)
<== Columns: actno, balance
<== Row: 002, 0
<== Total: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@62f87c44]
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@525d79f0] was not registered for synchronization because synchronization is not active
JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@32057e6] will not be managed by Spring
==> Preparing: update account set balance = ? where actno = ?
==> Parameters: 40000.0(Double), 001(String)
<== Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@525d79f0]
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@327120c8] was not registered for synchronization because synchronization is not active
JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@32057e6] will not be managed by Spring
==> Preparing: update account set balance = ? where actno = ?
==> Parameters: 10000.0(Double), 002(String)
<== Updates: 1
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@327120c8]
转账成功Process finished with exit code 0