Spring+SpringMvc+MyBatis整合+事务+单元测试案例 第二章
github源码(day56-ssm-transaction)https://github.com/1196557363/ideaMavenProject
对一些知识点不懂可以参考 SSM整合篇】一. Spring+SpringMvc+MyBatis简单案例
该案例接上章【SSM整合篇】三. SSM整合+事务+单元测试案例 第一章(共四章)
5. 事务的配置
5.1 定义I…Service及其实现类ServiceImpl(实现类就不用加I前缀了)
5.1.1 IDeptService及DeptServiceImpl
定义IDeptService接口
package com.wpj.service;
import org.springframework.stereotype.*;
/**
* ClassName: IDeptService
* Description:
*
* @author JieKaMi
* @version 1.0
* @date: 2020\1\8 0008 19:46
* @since JDK 1.8
*/
public interface IDeptService {
}
定义DeptServiceImpl实现类并注入Dao (实现类记得加@Service)
package com.wpj.service.impl;
import com.wpj.dao.*;
import com.wpj.service.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.*;
/**
* ClassName: DeptServiceImpl
* Description:
*
* @author JieKaMi
* @version 1.0
* @date: 2020\1\8 0008 20:00
* @since JDK 1.8
*/
@Service
public class DeptServiceImpl implements IDeptService {
@Autowired
private IDeptDao iDeptDao;
}
5.1.1 IEmpService及EmpServiceImpl
定义IEmpService接口
package com.wpj.service;
/**
* ClassName: IEmpService
* Description:
*
* @author JieKaMi
* @version 1.0
* @date: 2020\1\8 0008 19:47
* @since JDK 1.8
*/
public interface IEmpService {
}
定义DeptServiceImpl实现类并注入Dao (实现类记得加@Service)
package com.wpj.service.impl;
import com.wpj.dao.*;
import com.wpj.service.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.*;
/**
* ClassName: EmpServiceImpl
* Description:
*
* @author JieKaMi
* @version 1.0
* @date: 2020\1\8 0008 20:03
* @since JDK 1.8
*/
@Service
public class EmpServiceImpl implements IEmpService {
@Autowired
private IEmpDao iEmpDao;
}
5.2 扫描注解和配置事务
扫描注解放在加载jdbc.properties上面
<?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:aop="http://www.springframework.org/schema/aop"
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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 4.扫描注解 use-default-filters="false" 关闭默认机制 默认true -->
<context:component-scan base-package="com.wpj" use-default-filters="false">
<!-- 4.1 只扫描Serivce -->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>
<!-- 1. 加载jdbc.properties -->
<!-- 2. 创建数据源 -->
<!-- 3. Spring跟MyBatis整合 -->
<!-- 5. 事务的配置 -->
<!-- 5.1 事务管理器 负责事务的提交(需要连接数据库)-->
<bean id="tx" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 5.1.1 设置数据源 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 5.2 事务的策略 id:唯一标识 transaction-manager:事务管理器 -->
<tx:advice id="txAdvice" transaction-manager="tx">
<tx:attributes>
<!--
name:方法名称,支持通配符
isolation:事务隔离级别
propagation:传播级别
read-only:是否只读
-->
<!-- 写操作 -->
<tx:method name="add*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/>
<tx:method name="update*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/>
<tx:method name="delete*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/>
<!-- 读操作 -->
<tx:method name="get*" isolation="DEFAULT" propagation="REQUIRED" read-only="true"/>
<tx:method name="query*" isolation="DEFAULT" propagation="REQUIRED" read-only="true"/>
<tx:method name="find*" isolation="DEFAULT" propagation="REQUIRED" read-only="true"/>
<tx:method name="select*" isolation="DEFAULT" propagation="REQUIRED" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 5.3 事务AOP的配置 -->
<aop:config>
<!-- advice-ref: 指向事务的策略 -->
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.wpj.service.*.*(..))" />
</aop:config>
</beans>
5.3 Test (只测试Service是否注入成功)
import com.wpj.service.*;
import org.apache.ibatis.session.*;
import org.junit.*;
import org.junit.runner.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.test.context.*;
import org.springframework.test.context.junit4.*;
import javax.sql.*;
/**
* ClassName: SSMTest
* Description:
*
* @author JieKaMi
* @version 1.0
* @date: 2020\1\8 0008 17:51
* @since JDK 1.8
*/
@RunWith(value= SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = "classpath:spring-context.xml")
public class SSMTest {
@Autowired
private IEmpService iEmpService;
@Test
public void testIEmpService(){
System.out.println(iEmpService);
}
}
5.4 结果
如果报错了 或者为null 就要检查一下前面哪里写错了 再往下面走。
可能出错的地方
- impl没有加@Service
- dao注入没有加@Autowired
- 配置文件包扫描写错了
- 如果还不行的话把事务暂时去掉也可以
6. 测试Spring和MyBatis整合的环境 (以Emp为例)
6.1 创建dao层的代理
<!-- 6. 创建dao层的代理 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 6.1 Mapper接口的包名 -->
<property name="basePackage" value="com.wpj.dao" />
<!-- 6.2 sqlSessionFactoryBeanName-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
6.2 dao添加方法
public interface IEmpDao {
/**
* 获取所有的Emp
* @return
*/
List<Emp> getAllEmp();
}
6.3 mapper定义对应sql映射
<?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.wpj.dao.IEmpDao">
<!-- 配属性和字段的映射,将来写级联查询会用到-->
<resultMap id="empMap" type="emp">
<id property="empId" column="emp_id"/>
<result property="empName" column="emp_name" />
<result property="job" column="job" />
<result property="superId" column="superId" />
<result property="deptNo" column="deptNo" />
</resultMap>
<select id="getAllEmp" resultMap="empMap">
select * from emp
</select>
</mapper>
6.4 serivce中定义对应dao的方法
public interface IEmpService {
/**
* 获取所有的Emp
* @return
*/
List<Emp> getAllEmp();
}
6.5 serviceImpl中通过注入的dao调用方法
@Service
public class EmpServiceImpl implements IEmpService {
@Autowired
private IEmpDao iEmpDao;
public List<Emp> getAllEmp() {
return iEmpDao.getAllEmp();
}
}
6.6 Test
import com.wpj.bean.*;
import com.wpj.service.*;
import org.apache.ibatis.session.*;
import org.junit.*;
import org.junit.runner.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.test.context.*;
import org.springframework.test.context.junit4.*;
import javax.sql.*;
import java.util.*;
@RunWith(value= SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = "classpath:spring-context.xml")
public class SSMTest {
@Autowired
private IEmpService iEmpService;
@Test
public void testGetAllEmp() {
List<Emp> empList = iEmpService.getAllEmp();
for (Emp emp : empList) {
System.out.println(emp);
}
}
}
6.7 结果
还是那句话,如果报错了 或者为null 就要检查一下前面哪里写错了 再往下面走。
可能出错的地方
- 配置dao层的mapper配置和sqlSessionFactoryBeanName
- dao的方法名和配置文件的sql映射的id没对应
- resultMap 属性和字段映射错了
未完待续。。。。
- 测试事务是否配置成功 【SSM整合篇】三. SSM整合+事务+单元测试案例 第三章 (共四章)