配置文件pom文件
<?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>Spring-Mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.1.11</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>6.1.11</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.14</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.17.1</version>
</dependency>
</dependencies>
</project>
Logegr.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>
<typeAliases>
<typeAlias type="com.project.pojo.Bank" alias="Bank"/>
</typeAliases>
</configuration>
jdbc.properties配置文件
jdbc.url=jdbc:mysql://localhost:3306/studentdb?useSSL=false&serverTimezone=UTC
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.username=root
jdbc.password=123456
Springmxl
<?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"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
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
https://www.springframework.org/schema/tx/spring-tx.xsd
http://mybatis.org/schema/mybatis-spring
http://mybatis.org/schema/mybatis-spring.xsd">
<context:component-scan base-package="com.project.Service"/>
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="${jdbc.url}"/>
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:Mybatis-config.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.project.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
</beans>
log4j2配置设置
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
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.project.mapper.AccountMapper">
<resultMap id="userResultMap" type="com.project.pojo.Bank">
<result property="actNo" column="act_no"/>
<result property="balance" column="balance"/>
</resultMap>
<insert id="insert">
INSERT INTO studentdb.act (act.act_on, balance) VALUES (#{actNo}, #{balance})
</insert>
<update id="update">
UPDATE studentdb.act SET balance = #{balance} WHERE act.act_on = #{actNo}
</update>
<delete id="delete">
DELETE FROM studentdb.act WHERE act.act_on = #{actNo}
</delete>
<select id="selectByID" resultMap="userResultMap">
SELECT * FROM studentdb.act WHERE act.act_on = #{actNo}
</select>
<select id="selectAll" resultMap="userResultMap">
SELECT * FROM studentdb.act
</select>
</mapper>
Mapper接口
package com.project.mapper;
import com.project.pojo.Bank;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
public interface AccountMapper {
int insert(Bank bank);
int delete(String act);
int update(Bank bank);
Bank selectByID(String id);
List<Bank> selectAll();
}
实体类(empty)
package com.project.pojo;
public class Bank {
private String actNo;
private Integer balance;
public Bank(String number, int i) {
this.actNo=number;
this.balance=i;
}
@Override
public String toString() {
return "bank{" +
"actNo='" + actNo + '\'' +
", balance=" + balance +
'}';
}
public String getActNo() {
return actNo;
}
public void setActNo(String actNo) {
this.actNo = actNo;
}
public Integer getBalance() {
return balance;
}
public void setBalance(Integer balance) {
this.balance = balance;
}
}
业务实现类
package com.project.Service.impl;
import com.project.Service.BankService;
import com.project.mapper.AccountMapper;
import com.project.pojo.Bank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service("bankService")
@Transactional
public class BankServiceImpl implements BankService {
@Autowired
private AccountMapper accountMapper;
@Override
public int save(Bank bank) {
return accountMapper.insert(bank);
}
@Override
public int deleteByID(String act) {
return accountMapper.delete(act);
}
@Override
public int modify(Bank bank) {
return accountMapper.update(bank);
}
@Override
public Bank getByActno(String id) {
return accountMapper.selectByID(id);
}
@Override
public List<Bank> getActnoAll() {
return accountMapper.selectAll();
}
@Override
public void transfer(String fromAct, String toAct, Integer money) {
Bank bank = accountMapper.selectByID(fromAct);
if (bank.getBalance()<money) {
throw new RuntimeException("余额不足");
}
Bank bank1 = accountMapper.selectByID(toAct);
bank.setBalance(bank.getBalance()-money);
int update = accountMapper.update(bank);
if (1==1)
{
throw new RuntimeException("模拟异常!!");
}
bank1.setBalance(bank1.getBalance()+money);
update+=accountMapper.update(bank1);
if (update!=2) {
throw new RuntimeException("转账失败!!!");
}
}
}
业务接口
package com.project.Service;
import com.project.pojo.Bank;
import java.util.List;
public interface BankService {
int save(Bank bank);
int deleteByID(String act);
int modify(Bank bank);
Bank getByActno(String id);
List<Bank> getActnoAll();
public void transfer(String fromAct,String toAct,Integer money);
}
测试类
package com.project.test;
import com.project.Service.BankService;
import com.project.Service.impl.BankServiceImpl;
import com.project.pojo.Bank;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MybatisTest {
@Test
public void test1()
{
ApplicationContext context = (ApplicationContext) new ClassPathXmlApplicationContext("Spring.xml");
BankService bankService = (BankService) context.getBean("bankService", BankService.class);
bankService.transfer("01","05",1000);
}
}