生活随笔
收集整理的這篇文章主要介紹了
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)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。