HSQL简介

来源:互联网 发布:广州数控螺纹编程 编辑:程序博客网 时间:2024/04/29 10:57
前言
    在对dao层写测试类的时候,我们需要一个测试数据库,一般我们会是专门建立一个真实的测试数据库,但是有了HSQLDB事情就变得简单了起来。

正题
一、简介:
hsql数据库是一款纯Java编写的免费数据库,许可是BSD-style的协议,如果你是使用Java编程的话,不妨考虑一下使用它,相对其他数据库来说,其体积小,才563kb。仅一个hsqldb.jar文件就包括了数据库引擎,数据库驱动,还有其他用户界面操作等内容。在Java开源世界里,hsql是极为受欢迎的(就Java本身来说),JBoss应用程序服务器默认也提供了这个数据库引擎。由于其体积小的原因,又是纯Java设计,又支持SQL99,SQL2003大部分的标准,所以也是作为商业应用程序展示的一种选择。
 
ps:官网http://hsqldb.org/

二、优点

1.轻巧,只有600多K,运行速度非常快。结合Hibernate数据库无关的特性,非常适合在项目开发的时候使用。

2.作为单元测试数据库。单元测试的时候,启动HSQLDB的file模式,数据不存盘,可以保证测试原子性。

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

4.不需要安装,使用非常方便。

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

6.小型项目作为现场数据库使用,不需要安装Oracle之类的大型DB,减轻了维护成本,并且,HSQLDB非常容易备份。

三、局限性

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

2.作为测试数据库来讲,由于HSQLDB支持标准SQL, 所以一般情况没问题,但是对于某个数据(如MySql)的特殊语法则不兼容,容易报错。

四、作为测试数据库的使用
作为测试数据库的话,主要用于测试与数据有交互的类,即我们平时所讲的dao层。
HSQLDB使用很简单,只需要从官网下载最新版本的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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://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 SQLSET 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有一定的局限性,但是还是不得不说在某些情况下它是一个不错的测试数据库的选择。

3 0