728x90
반응형

Spring에서 내장 HSQL DB의 DataSource를 설정하는 기본 방식은 다음과 같다.

1
2
3
4
5
6
<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
    <property name="driverClassName" value="org.hsqldb.jdbcDriver" />
    <property name="url" value="jdbc\:hsqldb\:mem\:spring-playground" />
    <property name="username" value="sa" />
    <property name="password" value="" />
</bean>

 

하지만 <jdbc:embedded-database> 태그를 사용하면 아래와 같이 좀더 쉽게 설정할 수 있다.

type을 지정하지 않으면 기본적으로 HSQL 이 사용되고, H2, Derby 를 사용할 수도 있다.

1
<jdbc:embedded-database id="dataSource" type="HSQL"/>

 

여기에 다음과 같이 property 를 추가로 설정한다면, 

Schema 를 생성한다던지 Data 를 Insert 하는 등의 추가 작업도 지정할 수 있다. 

1
2
3
4
<jdbc:embedded-database id="dataSource">
    <jdbc:script location="classpath:schema.sql" />
    <jdbc:script location="classpath:data.sql" />
</jdbc:embedded-database>

 

지금까지의 내용으로 Test Case 를 작성해 보겠다.

Maven Depdendencies

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
<properties>
    <org.springframework-version>4.0.6.RELEASE</org.springframework-version>
    <hsqldb-version>1.8.0.10</hsqldb-version>
    <junit.version>4.10</junit.version>
    <org.hamcrest.version>1.1</org.hamcrest.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
     
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
 
    <dependency>
        <groupId>hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
        <version>${hsqldb-version}</version>
    </dependency>
     
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
     
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>
     
    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-all</artifactId>
        <version>${org.hamcrest.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>

 

applicationContext.xml 설정

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
<?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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:oxm="http://www.springframework.org/schema/oxm"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd">
 
    <context:component-scan base-package="com.happyhouse.rednics" />
 
    <jdbc:embedded-database id="dataSource" type="HSQL">
        <jdbc:script location="classpath:schema.sql" />
        <jdbc:script location="classpath:data.sql" />
    </jdbc:embedded-database>
 
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource" />
    </bean>
</beans>

 

schema.sql 작성

1
2
3
4
5
6
7
8
9
10
create schema happyhouse AUTHORIZATION DBA;
set schema happyhouse;
 
drop table tbl_user if exists;
 
CREATE TABLE tbl_user (
  id varchar(40) NOT NULL,
  username varchar(45) NOT NULL,
  password varchar(45) NOT NULL
);

 

data.sql 작성

1
insert into tbl_user values('rednics', 'Shin Kwan Young', '1234');

 

Test Case 작성

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
import java.util.List;
import java.util.Map;
 
import javax.sql.DataSource;
 
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/applicationContext.xml")
public class HsqlTest {
     
    @Autowired
    private JdbcTemplate jdbcTemplate;
     
    @Test
    public void test() {
        List<Map<String, Object>> userList = jdbcTemplate.queryForList("select * from happyhouse.tbl_user");
        for(Map<String, Object> user : userList) {
            System.out.println(user.get("id"));
            System.out.println(user.get("username"));
            System.out.println(user.get("password"));
        }
    }
 
}

 

실행결과

1
2
3
rednics
Shin Kwan Young
1234

 

 

728x90
반응형

'SQL > HSQLDB' 카테고리의 다른 글

HSQL 관리 툴  (0) 2015.03.04
Spring + MyBatis + Embedded Database(HSQL) 예제  (0) 2015.03.04
블로그 이미지

nineDeveloper

안녕하세요 현직 개발자 입니다 ~ 빠르게 변화하는 세상에 뒤쳐지지 않도록 우리모두 열심히 공부합시다 ~! 개발공부는 넘나 재미있는 것~!

,