spring整合dbunit
参考demo:G:\学习资料\java\java学习文档\java技术文档\Spring文档\xufei-spring-word\xufei-projo-test =》 springtestdbunit
# 导包
<!--下面是SSM的你懂的-->
<!--第一步应该导入我们的mybatis的包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.4</version>
</dependency>
<!--导入我们的mybatis运行的时候的一些依赖包日志相关的 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--导入的是日志相关的包 -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!--带入cglib代理的包 -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.2.4</version>
</dependency>
<!--引入junit类 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--导入myBatis和Spring整合的中间包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<!--导入我们Spring的相关包 -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.3.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javassist/javassist -->
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.1.GA</version>
</dependency>
<!-- https://mvnrepository.com/artifact/ognl/ognl -->
<dependency>
<groupId>ognl</groupId>
<artifactId>ognl</artifactId>
<version>3.2.14</version>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# 优化基类
public class AbstractDbunitTestCase {
private DatabaseConnection connection;
private File tempFile;
private IDataSet testDataSet; // 测试数据的DataSet对象
public AbstractDbunitTestCase(Connection connection, String testFileName) throws DatabaseUnitException {
this.connection = new DatabaseConnection(connection);
InputStream inputStream = AbstractDbunitTestCase.class.getClassLoader().getResourceAsStream(testFileName);
testDataSet = new FlatXmlDataSet(new FlatXmlProducer(new InputSource(inputStream)));
}
public AbstractDbunitTestCase(String testFileName) throws DatabaseUnitException {
InputStream inputStream = AbstractDbunitTestCase.class.getClassLoader().getResourceAsStream(testFileName);
testDataSet = new FlatXmlDataSet(new FlatXmlProducer(new InputSource(inputStream)));
}
/**
* 设置这个连接池的包裹对象
*
* @param conn
* @throws DatabaseUnitException
*/
protected void setDatabaseConnection(Connection conn) throws DatabaseUnitException {
this.connection = new DatabaseConnection(conn);
}
/**
* 这个就是备份多张表
*
* @param tabNames
*/
protected synchronized void backManyTable(String... tabNames) throws Exception {
QueryDataSet queryDataSet = new QueryDataSet(connection);
for (String tabName : tabNames) {
queryDataSet.addTable(tabName);
}
// 实施备份 使用到临时文件
tempFile = File.createTempFile("dbUnitTest", ".xml");
// 实施备份
if (null != tempFile) {
FlatXmlDataSet.write(queryDataSet, Files.newOutputStream(tempFile.toPath()));
}
}
/**
* 备份一张表
*/
protected void backOneTable(String tabName) throws Exception {
backManyTable(tabName);
}
/**
* 插入测试数据
*/
protected void insertTestData() throws DatabaseUnitException, SQLException {
if (null != connection && null != testDataSet) {
DatabaseOperation.CLEAN_INSERT.execute(connection, testDataSet);
} else {
throw new RuntimeException("参数有误不能插入数据:");
}
}
/**
* 还原表中的数据
*/
protected void resumeTable() throws Exception {
if (null != tempFile) {
IDataSet dataSet = new FlatXmlDataSet(new FlatXmlProducer(new InputSource(Files.newInputStream(tempFile.toPath()))));
DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);
} else {
throw new RuntimeException("临时文件都没有怎么还原....");
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# 准备数据库实体
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Integer uId; // 用户id
private String uName; // 用户名
private String passWord; // 用户密码
private Integer gender; // 用户性别
private String qq; // 用户qq号
private String weixin; // 用户微信号
private Integer workTime; // 用户工作年限
private Integer paymentAmount; // 用户缴费金额
private Date registerTime; // 用户注册时间
private String token; // 用户token用户身份的唯一标识
private Date tokenEndTime; // token过期时间
private Date payInvalidTime; // 用户缴费失效时间
private Integer isDel;// 是否删除用户 如果为0就是删除 1就是不删除
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 准备mapper
public interface UserMapper {
/**
* 通过id找用户
*
* @param uId
* @return
*/
User findUserById(int uId);
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# 准备bean-ibatis的配置文件
<?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:p="http://www.springframework.org/schema/p"
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:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd"
>
<!--数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://127.0.0.1:3369/school"></property>
<property name="username" value="root"/>
<property name="password" value="Curry@xu1#!3"></property>
</bean>
<!--Spring整合mybatis的关键点 在 sqlSessionFactory的创建上-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入dataSource对象-->
<property name="dataSource" ref="dataSource"></property>
<!--配置mapper.xml配置文件所在的位置-->
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean>
<!--这个类就非常重要了-->
<bean id="scannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--注入这个工厂-->
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
<!--扫描的是DAO接口所在的位置-->
<property name="basePackage" value="com.xf.springtestdbunit"></property>
</bean>
<!--配置aop的自动代理-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!--配置spring中包的扫描-->
<context:component-scan base-package="com.xf.springtestdbunit"></context:component-scan>
</beans>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 准备mapper.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">
<!--
1:
nameSpace:这个只能是当前这个实体的mapper接口对应的 全路径
-->
<mapper namespace="com.xf.springtestdbunit.UserMapper">
<!--查询所有的用户数据
id 必须是上面的接口中对应的方法的名字
传递的形参 必须和 接口中对应的参数类型一会
返回值的类型必须和 接口中对应的方法中的 返回值类型一致
注意:Java有可能返回集合 那么这里写 集合中泛型的数据类型
-->
<select id="findUserById" parameterType="int" resultType="com.xf.springtestdbunit.User">
select *
from t_user
where uId = #{value}
</select>
</mapper>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 准备测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:bean-ibatis.xml"})
public class TestUserDAO extends AbstractDbunitTestCase {
@Autowired
private UserMapper userMapper;
@Autowired
private DataSource dataSource;
private User exUser;
public TestUserDAO() throws DatabaseUnitException {
super("testdata.xml");
}
@Before
public void init() throws Exception {
setDatabaseConnection(dataSource.getConnection());
exUser = new User(1, "张三", "1111", 0, "123", "111", 2, 2, null, "aaaaaaaaaaa", null, null, 0);
// backOneTable("t_user");
backManyTable("t_user", "t_role", "t_user_role", "t_permission", "t_role_permission");
insertTestData();
}
@Test
public void testFindUserById() throws SQLException, DatabaseUnitException {
User acUser = userMapper.findUserById(1);
Assert.assertEquals(exUser.getUId(), acUser.getUId());
Assert.assertEquals(exUser.getGender(), acUser.getGender());
Assert.assertEquals(exUser.getIsDel(), acUser.getIsDel());
Assert.assertEquals(exUser.getPaymentAmount(), acUser.getPaymentAmount());
Assert.assertEquals(exUser.getPassWord(), acUser.getPassWord());
Assert.assertEquals(exUser.getPayInvalidTime(), acUser.getPayInvalidTime());
Assert.assertEquals(exUser.getWorkTime(), acUser.getWorkTime());
Assert.assertEquals(exUser.getWeixin(), acUser.getWeixin());
Assert.assertEquals(exUser.getUName(), acUser.getUName());
Assert.assertEquals(exUser.getTokenEndTime(), acUser.getTokenEndTime());
Assert.assertEquals(exUser.getRegisterTime(), acUser.getRegisterTime());
Assert.assertEquals(exUser.getQq(), acUser.getQq());
Assert.assertEquals(exUser.getToken(), acUser.getToken());
}
@After
public void after() throws Exception {
resumeTable();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
问题:测试的都是查询的方法、增删改怎么测试呢?
记住:在进行增删改的时候 是先进行增删改----然后进行查询 相当于还是使用查询来断言
dbunit是DAO层测试的框架
最近更新: 2025/01/22, 13:46:16