首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

spring中运用内存数据库(Embedded database)

2013-01-18 
spring中使用内存数据库(Embedded database)内存数据库(Embedded database或in-momery database)具有配置

spring中使用内存数据库(Embedded database)
内存数据库(Embedded database或in-momery database)具有配置简单、启动速度快、尤其是其可测试性等优点,使其成为开发过程中非常有用的轻量级数据库。在spring中支持HSQL、H2和Derby三种数据库。

下面看一下具体使用方法:
1.需要在applicationContext.xml中添加如下:

    <jdbc:embedded-database id="dataSource">        <jdbc:script location="classpath:schema.sql"/>        <jdbc:script location="classpath:test-data.sql"/>    </jdbc:embedded-database>

向applicationContext.xml添加这段配置时,注意不要忘了添加上jdbc的命名空间,类路径下schema.sql中创建了项目使用的数据库表和一些约束条件等,test-data.sql中想数据库表插入了一些数据。有了该内存数据库,就不再需要那些driverClassName和url等配置了。另外还要注意,该内存数据库默认是HSQL数据库,如果要使用其他的两个数据库,修改embedded-database标签的type属性值即可。

2.下面来看一个示例,该示例使用的derby数据库。

spring配置文件:applicationContext.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:aop="http://www.springframework.org/schema/aop"    xmlns:p="http://www.springframework.org/schema/p"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:tx="http://www.springframework.org/schema/tx"    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/aop     http://www.springframework.org/schema/aop/spring-aop-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    http://www.springframework.org/schema/tx    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">    <!-- derby创建用户名和密码参考:http://www.joyzhong.com/archives/643 -->    <!-- 使用内存数据库时,该段配置不再需要    <bean        id="dataSource"        type="DERBY">        <jdbc:script location="classpath:schema.sql"/>        <jdbc:script location="classpath:test-data.sql"/>    </jdbc:embedded-database>        <bean        id="sessionFactory"        name="code">create table sec_user(   username varchar(100) primary key,   password varchar(255)); create table sec_role( rolename varchar(100) primary key, prompt varchar(255));create table sec_role_user( username varchar(100) not null, rolename varchar(100) not null, constraint ru_id primary key(username,rolename));alter table sec_role_user add constraint fk_user foreign key(username) references sec_user(username);alter table sec_role_user add constraint fk_role foreign key(rolename) references sec_role(rolename);


向数据库表中插入数据:test-data.sql
insert into sec_user values('admin','admin');  insert into sec_user values('test','test');insert into sec_role values('ROLE_USER','common user privilege');insert into sec_role values('ROLE_ADMIN','administrator privilege');insert into sec_role_user values('test','ROLE_USER');insert into sec_role_user values('admin','ROLE_USER');insert into sec_role_user values('admin','ROLE_ADMIN');


官方文档:http://static.springsource.org/spring-framework/docs/3.0.0.M4/reference/html/ch12s08.html

热点排行