前言
    在对dao层写測试类的时候,我们须要一个測试数据库,一般我们会是专门建立一个真实的測试数据库,可是有了HSQLDB事情就变得简单了起来。

正题
一、简单介绍:
hsql数据库是一款纯Java编写的免费数据库。许但是BSD-style的协议,假设你是使用Java编程的话。最好还是考虑一下使用它。相对其它数据库来说,其体积小,才563kb。

仅一个hsqldb.jar文件就包含了数据库引擎。数据库驱动。还有其它用户界面操作等内容。在Java开源世界里。hsql是极为受欢迎的(就Java本身来说),JBoss应用程序server默认也提供了这个数据库引擎。因为其体积小的原因。又是纯Java设计。又支持SQL99。SQL2003大部分的标准。所以也是作为商业应用程序展示的一种选择。

 

二、长处

1.轻巧,仅仅有600多K,执行速度很快。

结合Hibernate数据库无关的特性。很适合在项目开发的时候使用。


2.作为单元測试数据库。

单元測试的时候,启动HSQLDB的file模式,数据不存盘,能够保证測试原子性。


3.来回复制。随身携带。

4.不须要安装。使用很方便。

5.稳定。并且支持的数据量足够大。

6.小型项目作为现场数据库使用,不须要安装Oracle之类的大型DB。减轻了维护成本,而且,HSQLDB很easy备份。

三、局限性

1.HSQLDB并非一个正式的数据库产品。假设用来做为商业应用数据库或者说开发时的数据库。不太妥当。这点在HSQLDB的官方文档里也提到了。

2.作为測试数据库来讲,因为HSQLDB支持标准SQL, 所以普通情况没问题,可是对于某个数据(如MySql)的特殊语法则不兼容,easy报错。

四、作为測试数据库的使用
作为測试数据库的话。主要用于測试与数据有交互的类。即我们平时所讲的dao层。
HSQLDB使用非常easy,仅仅须要从官网下载最新版本号的zip包然后解压放到lib文件夹下就可以(当然maven项目须要加入到pom.xml中进行引用)。然后须要一个配置文件 两个sql文件和一个測试类就可以。

測试类:
package com.demandforce.dao;

import java.util.Collection;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.Assert; import com.demandforce.model.TextMessageTemplate; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:context/hyperSqlContext.xml"})
public class TextMessageTemplateRetrievalTest { @Autowired
private TextMessageTemplateDao textMessageTemplateDao; @Test
public void testSelectByBusinessCategory() {
Collection<TextMessageTemplate> tmts = textMessageTemplateDao.selectByBusinessCategory( 203, 0);
Assert.notNull(tmts);
Assert.isTrue(tmts.size() == 2);
}
}


hyperSqlContext.xml
<?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:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">
<jdbc:embedded-database id="dataSource" type="HSQL">
<jdbc:script location="classpath:sql/setup.sql" />
<jdbc:script location="classpath:sql/schema-create.sql" />
<jdbc:script location="classpath:sql/data-insert.sql" />
</jdbc:embedded-database>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean name="baseDao" abstract="true">
<property name="dataSource" ref="dataSource" />
</bean> <bean name="textMessageTemplateDao" class="com.demandforce.dao.TextMessageTemplateDaoImpl" parent="baseDao" />
</beans>


setup.sql
--
-- MySQL compatibility mode for Hyper SQL SET DATABASE SQL SYNTAX MYS TRUE;


schema-create.sql

DROP TABLE IF EXISTS TextMessageTemplate;

CREATE TABLE TextMessageTemplate (
ID INT NOT NULL AUTO_INCREMENT,
BusinessID INT NOT NULL,
Type INT NOT NULL,
Template varchar(1000),
CreatedDate datetime DEFAULT NULL,
LastModifiedDate timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
CreatedUserId INT DEFAULT 0,
LastModifiedUserId INT DEFAULT 0,
Delivery INT DEFAULT 0,
DeliveryWindow INT DEFAULT 1,
BusinessCalendar INT NOT NULL DEFAULT 0,
DeliveryTime datetime DEFAULT NULL,
CardCount INT DEFAULT 0,
Segment INT DEFAULT 0,
FrontImage varchar(255) DEFAULT NULL,
IncludeItems INT DEFAULT 1,
IncludeReview INT DEFAULT 1,
IncludeCoupon INT DEFAULT 1,
services varchar(5000) DEFAULT NULL,
Industry INT DEFAULT NULL,
PRIMARY KEY (ID)
);


data-insert.sql
INSERT INTO TextMessageTemplate (BusinessID, Type, Template, Industry)
VALUES ('0', '203', 'Template1', null); INSERT INTO TextMessageTemplate (BusinessID, Type, Template, Industry)
VALUES ('0', '204', 'Template2', null);


小结:
尽管HSQLDB有一定的局限性,可是还是不得不说在某些情况下它是一个不错的測试数据库的选择。

版权声明:本文博主原创文章,博客,未经同意不得转载。

最新文章

  1. iOS--使用UIImageView进行GIF动图播放
  2. Android5.0如何正确启用isLoggable(二) 理分析
  3. ES6 基础版迭代器
  4. sellect、poll、epoll
  5. 通过org.springframework.web.filter.CharacterEncodingFilter定义Spring web请求的编码
  6. Hibernaate事务管理
  7. 天坑 之 java web servlet+jsp项目 配置后 404 (MyEclipse转eclipse)
  8. C++基础梳理--Class、Struct、Union
  9. 关于mysql查询区分大小写
  10. C语言之运算符和条件结构
  11. 使用Git将项目托管到码云及从码云导入项目到本地
  12. 观察者模式 Observer 发布订阅模式 源 监听 行为型 设计模式(二十三)
  13. java 开发工具记录
  14. C语言程序猿必会的内存四区及经典面试题解析
  15. python day18 作业 Django 开发主机管理系统
  16. 小程序sitemap配置
  17. Codeforces 1005 F - Berland and the Shortest Paths
  18. 17. Letter Combinations of a Phone Number (backtracking)
  19. 494. Target Sum - Unsolved
  20. Hibernate 分页 查询

热门文章

  1. boost 库编译选项
  2. echart报表插件使用笔记(二)--按月统计
  3. linux下U盘状态检测
  4. 28、从零写UVC驱动之实现设置属性
  5. [机器学习] Coursera ML笔记 - 逻辑回归(Logistic Regression)
  6. Hbase常见异常 分类: B7_HBASE 2015-02-02 16:16 412人阅读 评论(0) 收藏
  7. 【32.22%】【codeforces 602B】Approximating a Constant Range
  8. c++ 成员函数指针
  9. 小强的HTML5移动开发之路(51)——jquerymobile中改善页面访问速度
  10. 账号权限问题导致 masterha_check_repl 检查失败