13. ibatis最佳实践

本文介绍如何使用Ibatis进行单元测试,包括配置SqlMapClient、创建测试数据、编写针对映射层和DAO层的测试案例等。同时探讨了Ibatis配置文件的管理策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


1. ibatis中的单元测试





1.1 对映射层进行单元测试

 

1. 测试用数据库实例


http://hsqldb.sourceforge.net/


2. 数据库脚本


3. ibatis配置文件


4. ibatis SqlMapClient单元测试

public class PersonMapTest extends TestCase {
	private SqlMapClient sqlMapClient;

	public void setup() {
		sqlMapClient = SqlMapClientBuilder.build("maps/TestSqlMapConfig.xml");
		runSqlScript("scripts/drop-person-schema.sql");
		runSqlScript("scripts/create-person-schema.sql");
		runSqlScript("scripts/create-person-test-data.sql");
	}

	public void testShouldGetPersonWithIdOfOne() {
		Person person = (Person) sqlMapClient.queryForObject("getPerson", new Integer(1));
		assertNotNull("Expected to find a person.", person);
		assertEquals("Expected person ID to be 1.", new Integer(1), person.getId());
	}
}


1.2 对DAO进行单元测试


1. 利用模拟对象来对DAO进行单元测试



1.3 对DAO的消费层进行单元测试



2. 管理ibatis配置文件



2.1 将其保存在类路径上


2.2 集中放置文件


2.3 主要按返回类型来组织映射文件



3. 命名规范




































评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值