spring中使用内存数据库(Embedded database)
内存数据库(Embedded database或in-momery database)具有配置简单、启动速度快、尤其是其可测试性等优点,使其成为开发过程中非常有用的轻量级数据库。在spring中支持HSQL、H2和Derby三种数据库。
下面看一下具体使用方法:
1.需要在applicationContext.xml中添加如下:
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代码
- <?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
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
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
spring中使用内存数据库(Embedded database)相关推荐
- 在Spring Boot中使用内存数据库
文章目录 H2数据库 HSQLDB Apache Derby SQLite 在Spring Boot中使用内存数据库 所谓内存数据库就是可以在内存中运行的数据库,不需要将数据存储在文件系统中,但是相对 ...
- Spring 中的 context
Spring 中的 context BeanFactory 首先看下,官方在代码中给出的注释: The root interface for accessing a Spring bean conta ...
- 解决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), ...
- 解决 springboot 启动报错 -- Cannot determine embedded database driver class for database type NONE
前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. 1. 问题描述 我只是新建一个全新的 springboot 工程,什么都没有写的情况下启动报错如题: ...
- 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 ...
- 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 ...
- 嵌入式数据库(Embedded Database)
嵌入式数据库(Embedded Database)和数据库服务器(Database Server) 像Oracle.Sybase.MySQL和SQL Server这些大家熟知的数据库都属于数据库服务器 ...
- Spring5源码解析-Spring中的异步事件
上一篇 Spring框架中的事件和监听器并未对Spring框架中的异步事件涉及太多,所以本篇是对其一个补充. 同步事件有一个主要缺点:它们在所调用线程的本地执行(也就是将所调用线程看成主线程的话,就是 ...
- Quartz 在 Spring 中如何动态配置时间
在项目中有一个需求,需要灵活配置调度任务时间,并能自由启动或停止调度. 有关调度的实现我就第一就想到了Quartz这个开源调度组件,因为很多项目使用过,Spring结合Quartz静态配置调度任务时间 ...
最新文章
- android:themes.xml
- 值得推荐!安利8个小众好用的宝藏工具,解决各种需求
- centos 打包某个目录_Linux目录基础,带你了解Linux神秘面纱
- EXCEL自定义的应用
- 连遭主流社交应用抛弃,是时候宣判黑莓系统死刑了
- html里下拉标记,HTML: select 标签
- JAVA加密算法系列-AesCBC
- python面试题总结(2)--编码规范
- ---M文件-函数式M文件/脚本式、m文件基本构成
- 关于SQL中的两个问题的理解
- MySQL的备份与还原
- [Python可视化] pyecharts安装入门及绘制中国贵州地图
- 服务器安全证书过期怎么办,安全证书过期怎么办 网站安全证书失效处理【解决方法】...
- mysql right syntax_Mysql 出现the right syntax to use near USING BTREE错误解决办法
- 苹果邮箱怎么登录qq邮箱_电子邮箱 电子邮箱格式怎么写
- 软考高级可以作为高级职称直接落户上海吗?
- java面向对象小知识
- 微信分享到朋友圈的链接没有图片。开发工具中正常没有报错-解决方案
- 入网许可证_入网许可证查询
- 蚁群算法简介及matlab源代码
热门文章
- QT的QGLWidget类的使用
- C++对C的加强之C++中所有的变量和函数都必须有类型
- 二十种实战调优MySQL性能优化的经验(转自公众号:架构之家 2018-05-08)
- 用 Freemarker 生成 word 文档
- Python KNN K近邻分类
- 最小二乘法(多元)推导
- mysql可以存储整数数值的是_MySQL的数值类型
- mysql允许两个用户远程连接_mysql允许用户远程连接
- python采用单例模式游戏_Python实现Singleton模式的方式详解
- 教师计算机应用测试题,教师计算机等级考试模拟考试题演示教学