内存数据库(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"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>org.apache.derby.jdbc.EmbeddedDriver</value>
</property>
<property name="url">
<value>jdbc:derby:f:/zwh/db/secdb</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>root</value>
</property>
</bean>
-->
<!-- 使用内存数据库 -->
<jdbc:embedded-database id="dataSource" type="DERBY">
<jdbc:script location="classpath:schema.sql"/>
<jdbc:script location="classpath:test-data.sql"/>
</jdbc:embedded-database>
<bean
id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.DerbyDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>zwh.spring.security.po.User</value>
<value>zwh.spring.security.po.Role</value>
</list>
</property>
</bean>
<!-- 使用annotation的方式配置Spring Bean -->
<context:component-scan base-package="zwh.spring.security"></context:component-scan>
</beans>
创建数据库表脚本:schema.sql
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
分享到:
相关推荐
$ git clone https://github.com/mkyong/spring-embedded-database $ mvn jetty:run 访问http://localhost:8080/spring-mvc-db/ ### 3。 将此项目导入Eclipse IDE $ mvn eclipse:eclipse 通过现有项目将Eclipse...
与Spring TestContext框架自动集成完全支持上下文缓存与Flyway数据库迁移工具无缝集成只需将@FlywayTest批注放置在测试类或方法上优化嵌入式数据库的初始化和清理数据库模板用于减少加载时间使用轻量级的捆绑来减小...
Spring嵌入式数据库 作为构建此项目的一部分,实现了以下内容: 创建一个springboot项目 开发和发布宁静的Web服务 使用嵌入式数据库创建和管理数据源 使用的技术 Maven的3.0 Springboot-2.2.4 Spring5.3.5 ...
C#使用Embedded Firebird数据库示例 Firebird 数据库 内嵌数据库 .net data provider for firebird
数据库算法源代码,一本关于uCOS-II的经典教程,特别适合嵌入式系统开发的人员
在此应用中,我使用H2内存数据库进行演示 Application.properties spring.datasource.url=jdbc:h2:mem:TEST;DB_CLOSE_DELAY=-1; spring.datasource.username=sa spring.datasource.password= spring.datasource....
这基本上是使用 Hiberante ORM 演示 Spring 3.x Embedded 数据库功能。 这里我使用了 H2 类型的 inMemory 数据库(Spring 支持 H2、HSQL、Derby) 技术栈。 Spring 3.2.10 和事务。 Spring 嵌入式数据库,H2 类型...
数据库原理3.04 Embedded SQL
简单笔记记录REST应用 ### 1。 使用的技术 Maven的3.0 春天4.1.6.RELEASE HSQLDB 2.3.2 ... 访问http://localhost:8080/spring-mvc-embedded-db-1.0-SNAPSHOT/api/notes ### 6。 其他问题: 您的note-searc
在介绍SQL Server Mobile的基本概念和开发运行环境的基础上,详绅论述安装和配置SQL Server CE Mobile开发环境的步骤和方法,幵通过对一个数据库应用程序(AddressBook)开发过程的详绅讲解使读者完整理解和掌握SQL ...
JSP 和 Servlet 演示 这是一个使用 JSP 和 servlet 以及内存中的 H2 嵌入式数据库的基本购物车应用程序。 如果有兴趣,请参阅我网站上的,我在其中简要讨论了该应用程序。
IAR.Embedded.Workbench是一种很使用的编译器,编译效率高,编译后生成的文件小,附件编译器的中文教程,适合初学者使用!
Chapter 1 Software Engineering of Embedded and Real-Time Systems Chapter 2 Embedded Systems Hardware/Software Co-Development Chapter 3 Software Modeling for Embedded Systems Chapter 4 Software Design ...
Spring 3.x 应用程序描述了基于 AOP 注释、Spring 测试框架和 Spring 嵌入式数据库特性。 它还提供了有关 Lombok 和 Logback 框架的想法。 ##关键点: AppConfig.java @PropertySource ,一种用于添加属性文件...
8.1.4 使用游标的SQL语句 8.1.5 动态SQL 8.1.6 小结 8.1.1 嵌入式SQL的处理过程 主语言 嵌入式SQL(Embedded SQL,ESQL)是将SQL 语句嵌入程序设计语言中,被嵌入的程序设计语言, 如C、C++、Java,称为宿主语言,...
对embedded matlab function的使用中文说明
IAR Embedded Workbench for ARM中文用户指南.PDF
提供内存数据库的规则(同时支持和 )。 它与所有已知的JDBC访问库兼容,例如 , , , 或普通的JDBC。 为什么? 因为您要测试由代码执行SQL代码而不与实际的DB服务器集成 无需运行数据库服务器并使其可用 您正在...
Embedded-mongo-spring 提供了一种在 Spring TestContext Framework 中使用 fgo 的方法。注解神奇之处在于实现了 TestExecutionListener 的 FongoTestExecutionListener 类(默认侦听器)。 这个监听器找到@...