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

内存数据库(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.       
  45.     <bean  
  46.         id="sessionFactory"  
  47.         class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">  
  48.         <property name="dataSource">  
  49.             <ref bean="dataSource"/>  
  50.         </property>  
  51.         <property name="hibernateProperties">  
  52.             <props>  
  53.                 <prop key="hibernate.dialect">org.hibernate.dialect.DerbyDialect</prop>  
  54.                 <prop key="hibernate.show_sql">true</prop>  
  55.                 <prop key="hibernate.format_sql">true</prop>  
  56.             </props>  
  57.         </property>  
  58.         <property name="annotatedClasses">  
  59.             <list>  
  60.                 <value>zwh.spring.security.po.User</value>  
  61.                 <value>zwh.spring.security.po.Role</value>  
  62.             </list>  
  63.         </property>  
  64.     </bean>  
  65.     <!-- 使用annotation的方式配置Spring Bean -->  
  66.     <context:component-scan base-package="zwh.spring.security"></context:component-scan>  
  67.       
  68. </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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值