HSQL簡介,hsqldb

來源:互聯網
上載者:User

HSQL簡介,hsqldb
前言    在對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有一定的局限性,但是還是不得不說在某些情況下它是一個不錯的測試資料庫的選擇。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.