pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>mybatisaddsub</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.31</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.15</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
mybatis-config.xml
<?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="mapUnderscoreToCamelCase" value="true"/>
</settings>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/dict"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/AccountMapper.xml"></mapper>
</mappers>
</configuration>
AccountMapper.xml
<?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="mapper.AccountMapper">
<resultMap id="account" type="entity.Account">
<id property="acid" column="acid" />
<result property="acname" column="acname" />
<result property="acpassword" column="acpassword" />
<result property="acmoney" column="acmoney" />
</resultMap>
<select id="selectById" parameterType="java.lang.Integer" resultMap="account" >
select * from account where acid = #{accountId}
</select>
<update id="updateById" >
update account set acname = #{acname},acpassword = #{acpassword}, acmoney = #{acmoney}
where acid= #{acid}
</update>
</mapper>
Account.java
package entity;
public class Account {
private Integer acid;
private String acname;
private String acpassword;
private Integer acmoney;
public Integer getAcid() {
return acid;
}
public void setAcid(Integer acid) {
this.acid = acid;
}
public String getAcname() {
return acname;
}
public void setAcname(String acname) {
this.acname = acname;
}
public String getAcpassword() {
return acpassword;
}
public void setAcpassword(String acpassword) {
this.acpassword = acpassword;
}
public Integer getAcmoney() {
return acmoney;
}
public void setAcmoney(Integer acmoney) {
this.acmoney = acmoney;
}
}
AccountMapper.java
package mapper;
import entity.Account;
public interface AccountMapper {
public int updateById(Account account);
public Account selectById(Integer accountId);
}
AccountService.java
package service;
import entity.Account;
public interface AccountService {
public boolean toMoney(Account account1,Account account2,int money);
public Account getById(int id);
}
AccountServiceImpl.java
package service.impl;
import entity.Account;
import mapper.AccountMapper;
import org.apache.ibatis.session.SqlSession;
import service.AccountService;
import utils.DButil;
public class AccountServiceImpl implements AccountService {
@Override
public boolean toMoney(Account account1, Account account2, int money) {
int row1 = 0;
int row2 = 0;
SqlSession sqlSession = null;
try {
sqlSession = DButil.getSqlSession();
AccountMapper mapper = sqlSession.getMapper(AccountMapper.class);
account1.setAcmoney(account1.getAcmoney()-money);
account2.setAcmoney(account2.getAcmoney() +money);
row1 = mapper.updateById(account1);
row2 = mapper.updateById(account2);
sqlSession.commit();
} catch (Exception e) {
sqlSession.rollback();
} finally {
DButil.closeSqlSession(sqlSession);
}
if(row1+row2 >=2)
return true;
else
return false;
}
public Account getById(int id) {
SqlSession sqlSession = DButil.getSqlSession();
AccountMapper mapper = sqlSession.getMapper(AccountMapper.class);
Account account = mapper.selectById(id);
DButil.closeSqlSession(sqlSession);
return account;
}
}
DButil.java
package utils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;
public class DButil {
private static SqlSessionFactory sqlSessionFactory = null;
static {
try {
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession(false);
}
public static void closeSqlSession(SqlSession sqlSession){
if(sqlSession !=null)
sqlSession.close();
}
}
TestAccountService.java
import entity.Account;
import org.junit.Test;
import service.AccountService;
import service.impl.AccountServiceImpl;
public class TestAccountService {
@Test
public void testToMoney(){
AccountService accountService = new AccountServiceImpl();
Account account1 = accountService.getById(1);
Account account2 = accountService.getById(2);
int money1 = account1.getAcmoney();
int money2 = account2.getAcmoney();
System.out.println(money1);
System.out.println(money2);
boolean result = accountService.toMoney(account1,account2,20);
System.out.println(result);
money1 = account1.getAcmoney();
money2 = account2.getAcmoney();
System.out.println(money1);
System.out.println(money2);
}
}