Spring整合MyBatis是指将Spring框架和MyBatis持久化框架结合起来使用,实现数据访问层和业务逻辑层的整合。Spring是一个轻量级的Java开发框架,提供了面向切面编程(AOP)、依赖注入(DI)等功能,可以简化企业级应用程序的开发。而MyBatis是一种持久化框架,通过将数据库操作与Java对象的映射进行配置,实现了数据库操作的简化。
本文中将使用到整合提供的事务管理机制,可以统一管理数据库操作的事务,确保数据的一致性和完整性。
项目案例主要的流程为:通过service层调用dao层,然后通过dao层调用mybatis进行数据库,最后使用Demo测试类。
事务管理模块:通过模拟两个账号的转账功能,实现事务管理的功能。
项目核心的部分包含:1)项目相关依赖 2)mybatis核心配置文件 3)spring核心配置文件 4)service层内容 5)mapper层内容 6)Demo测试类内容
具体内容如下:
1. pom.xml 依赖
<dependencies>
<!--spring基础依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.20</version>
</dependency>
<!--spring连接jdbc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.20</version>
</dependency>
<!--事务依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.3.20</version>
</dependency>
<!--切面编程框架-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.7</version>
</dependency>
<!--mybatis框架-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.4</version>
</dependency>
<!--mybatis与spring整合-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<!--mysql数据库依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>
<!--dbcp连接池-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.11.0</version>
</dependency>
<!--lombok依赖-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
</dependency>
</dependencies>
2. spring核心配置文件:applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--扫描注解包-->
<context:component-scan base-package="com.laoma.mapper"/>
<context:component-scan base-package="com.laoma.service.impl"/>
<!--数据源-->
<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/student_db"/>
<property name="username" value="root"/>
<property name="password" value="12345678"/>
</bean>
<!--SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--数据源-->
<property name="dataSource" ref="dataSource"/>
<!--实体类别名配置-->
<property name="typeAliasesPackage" value="com.laoma.pojo"/>
</bean>
<!--事务的配置-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--将事务管理器交给spring容器进行管理-->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 配置Mapper,自动扫描Mapper接口,并为其注入SqlSessionFactory -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--指定mapper接口的位置-->
<property name="basePackage" value="com.laoma.mapper"/>
<!--指定sqlSessionFactory的名字-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
</beans>
3. 实体类:Account
@Data
public class Account {
private int id;
private String name;
private int money;
}
4. Service 层:AccountService
public interface AccountService {
public int pay(int sourceId, int dstId,int money);
}
5. Service层:AccountServiceImpl
@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 sourceId, int dstId,int money) {
//-:转出 sourceId
accountMapper.updateId1(sourceId,money);
//-转账中错先异常(模拟停电现象)
//int res = 1/0;
//-:转入 dstId
accountMapper.updateId2(dstId,money);
return 0;
}
}
6. 持久层:AccountMapper
@Mapper
public interface AccountMapper {
//-转出
@Update("update t_account set money=money-${money} where id=${id}")
public int updateId1(@Param("id") int sourceId, @Param("money") int money);
//-转入
@Update("update t_account set money=money+${money} where id=${id}")
public int updateId2(@Param("id") int dstId,@Param("money") int money);
}
7. 测试类:Demo
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);
}
}
8. 补充:数据库中:account 表设计
到此,整个操作整理完毕!