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

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

Xml代码

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

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

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

spring配置文件:applicationContext.xml

Xml代码

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans
  3. xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:p="http://www.springframework.org/schema/p"
  7. xmlns:context="http://www.springframework.org/schema/context"
  8. xmlns:tx="http://www.springframework.org/schema/tx"
  9. xmlns:jdbc="http://www.springframework.org/schema/jdbc"
  10. xsi:schemaLocation="http://www.springframework.org/schema/beans
  11. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  12. http://www.springframework.org/schema/aop
  13. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  14. http://www.springframework.org/schema/context
  15. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  16. http://www.springframework.org/schema/jdbc
  17. http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
  18. http://www.springframework.org/schema/tx
  19. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
  20. <!-- derby创建用户名和密码参考:http://www.joyzhong.com/archives/643 -->
  21. <!-- 使用内存数据库时,该段配置不再需要
  22. <bean
  23. id="dataSource"
  24. class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  25. <property name="driverClassName">
  26. <value>org.apache.derby.jdbc.EmbeddedDriver</value>
  27. </property>
  28. <property name="url">
  29. <value>jdbc:derby:f:/zwh/db/secdb</value>
  30. </property>
  31. <property name="username">
  32. <value>root</value>
  33. </property>
  34. <property name="password">
  35. <value>root</value>
  36. </property>
  37. </bean>
  38. -->
  39. <!-- 使用内存数据库 -->
  40. <jdbc:embedded-database id="dataSource" type="DERBY">
  41. <jdbc:script location="classpath:schema.sql"/>
  42. <jdbc:script location="classpath:test-data.sql"/>
  43. </jdbc:embedded-database>
  44. <bean
  45. id="sessionFactory"
  46. class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  47. <property name="dataSource">
  48. <ref bean="dataSource"/>
  49. </property>
  50. <property name="hibernateProperties">
  51. <props>
  52. <prop key="hibernate.dialect">org.hibernate.dialect.DerbyDialect</prop>
  53. <prop key="hibernate.show_sql">true</prop>
  54. <prop key="hibernate.format_sql">true</prop>
  55. </props>
  56. </property>
  57. <property name="annotatedClasses">
  58. <list>
  59. <value>zwh.spring.security.po.User</value>
  60. <value>zwh.spring.security.po.Role</value>
  61. </list>
  62. </property>
  63. </bean>
  64. <!-- 使用annotation的方式配置Spring Bean -->
  65. <context:component-scan base-package="zwh.spring.security"></context:component-scan>
  66. </beans>

创建数据库表脚本:schema.sql

Sql代码

  1. create table sec_user(
  2. username varchar(100) primary key,
  3. password varchar(255)
  4. );
  5. create table sec_role(
  6. rolename varchar(100) primary key,
  7. prompt varchar(255)
  8. );
  9. create table sec_role_user(
  10. username varchar(100) not null,
  11. rolename varchar(100) not null,
  12. constraint ru_id primary key(username,rolename)
  13. );
  14. alter table sec_role_user add constraint fk_user foreign key(username) references sec_user(username);
  15. alter table sec_role_user add constraint fk_role foreign key(rolename) references sec_role(rolename);

向数据库表中插入数据:test-data.sql

Sql代码

  1. insert into sec_user values('admin','admin');
  2. insert into sec_user values('test','test');
  3. insert into sec_role values('ROLE_USER','common user privilege');
  4. insert into sec_role values('ROLE_ADMIN','administrator privilege');
  5. insert into sec_role_user values('test','ROLE_USER');
  6. insert into sec_role_user values('admin','ROLE_USER');
  7. insert into sec_role_user values('admin','ROLE_ADMIN');

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

spring中使用内存数据库(Embedded database)相关推荐

  1. 在Spring Boot中使用内存数据库

    文章目录 H2数据库 HSQLDB Apache Derby SQLite 在Spring Boot中使用内存数据库 所谓内存数据库就是可以在内存中运行的数据库,不需要将数据存储在文件系统中,但是相对 ...

  2. Spring 中的 context

    Spring 中的 context BeanFactory 首先看下,官方在代码中给出的注释: The root interface for accessing a Spring bean conta ...

  3. 解决Action: Consider the following: If you want an embedded database (H2, HSQL or Derby), ple..

    问题 springboot异常Action: Consider the following: If you want an embedded database (H2, HSQL or Derby), ...

  4. 解决 springboot 启动报错 -- Cannot determine embedded database driver class for database type NONE

    前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. 1. 问题描述 我只是新建一个全新的 springboot 工程,什么都没有写的情况下启动报错如题: ...

  5. SpringBoot项目报错Cannot determine embedded database driver class for database type NONE

    原因: Cannot determine embedded database driver class for database type NONE 这是因为spring boot默认会加载org.s ...

  6. Consider the following: If you want an embedded database (H2, HSQL or Derby), please put it on the

    在配置sharding-jdbc中 spring.shardingsphere.datasource.names=ds0,ds1spring.shardingsphere.datasource.ds0 ...

  7. 嵌入式数据库(Embedded Database)

    嵌入式数据库(Embedded Database)和数据库服务器(Database Server) 像Oracle.Sybase.MySQL和SQL Server这些大家熟知的数据库都属于数据库服务器 ...

  8. Spring5源码解析-Spring中的异步事件

    上一篇 Spring框架中的事件和监听器并未对Spring框架中的异步事件涉及太多,所以本篇是对其一个补充. 同步事件有一个主要缺点:它们在所调用线程的本地执行(也就是将所调用线程看成主线程的话,就是 ...

  9. Quartz 在 Spring 中如何动态配置时间

    在项目中有一个需求,需要灵活配置调度任务时间,并能自由启动或停止调度. 有关调度的实现我就第一就想到了Quartz这个开源调度组件,因为很多项目使用过,Spring结合Quartz静态配置调度任务时间 ...

最新文章

  1. android:themes.xml
  2. 值得推荐!安利8个小众好用的宝藏工具,解决各种需求
  3. centos 打包某个目录_Linux目录基础,带你了解Linux神秘面纱
  4. EXCEL自定义的应用
  5. 连遭主流社交应用抛弃,是时候宣判黑莓系统死刑了
  6. html里下拉标记,HTML: select 标签
  7. JAVA加密算法系列-AesCBC
  8. python面试题总结(2)--编码规范
  9. ---M文件-函数式M文件/脚本式、m文件基本构成
  10. 关于SQL中的两个问题的理解
  11. MySQL的备份与还原
  12. [Python可视化] pyecharts安装入门及绘制中国贵州地图
  13. 服务器安全证书过期怎么办,安全证书过期怎么办 网站安全证书失效处理【解决方法】...
  14. mysql right syntax_Mysql 出现the right syntax to use near USING BTREE错误解决办法
  15. 苹果邮箱怎么登录qq邮箱_电子邮箱 电子邮箱格式怎么写
  16. 软考高级可以作为高级职称直接落户上海吗?
  17. java面向对象小知识
  18. 微信分享到朋友圈的链接没有图片。开发工具中正常没有报错-解决方案
  19. 入网许可证_入网许可证查询
  20. 蚁群算法简介及matlab源代码

热门文章

  1. QT的QGLWidget类的使用
  2. C++对C的加强之C++中所有的变量和函数都必须有类型
  3. 二十种实战调优MySQL性能优化的经验(转自公众号:架构之家 2018-05-08)
  4. 用 Freemarker 生成 word 文档
  5. Python KNN K近邻分类
  6. 最小二乘法(多元)推导
  7. mysql可以存储整数数值的是_MySQL的数值类型
  8. mysql允许两个用户远程连接_mysql允许用户远程连接
  9. python采用单例模式游戏_Python实现Singleton模式的方式详解
  10. 教师计算机应用测试题,教师计算机等级考试模拟考试题演示教学