下面的databaseUrl在windows下,指向了c:/user/yourhome路徑,暫時沒想到怎麼配置到WEBAPP根路徑下。
由於是輕量級工控webapp,數據庫規模不大,也不須要暴露URL給其餘主機訪問,因此選擇了SQLite,Hibernate用慣了,須要使用ORM來作數據庫操做,因此選擇了ORMlite,J2EE開發習慣了,因此使用了Spring。java
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <context:annotation-config/> <bean id="databaseUrl" class="java.lang.String"> <constructor-arg index="0" value="jdbc:sqlite:../test.db"></constructor-arg> </bean> <!-- our data-source that controlls connections to the datbase --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="org.sqlite.JDBC" /> <property name="url" ref="databaseUrl" /> </bean> <!-- connection-source that delegates to a data-source --> <bean id="connectionSource" class="com.j256.ormlite.jdbc.DataSourceConnectionSource" init-method="initialize"> <property name="databaseUrl" ref="databaseUrl" /> <property name="dataSource" ref="dataSource" /> </bean> <!-- our daos that are created by using the DaoFactory --> <bean id="userDao" class="com.j256.ormlite.spring.DaoFactory" factory-method="createDao"> <constructor-arg index="0" ref="connectionSource" /> <constructor-arg index="1" value="com.saiyang.newflypig.rwt.entity.User" /> </bean> <!-- auto-creates tables as necessary, probably only useful for testing --> <bean id="tableCreator" class="com.j256.ormlite.spring.TableCreator" init-method="initialize"> <property name="connectionSource" ref="connectionSource" /> <property name="configuredDaos"> <list> <ref bean="userDao" /> </list> </property> </bean> </beans>
上面的 spring-core.xml 最下面一個bean是配置根據Entity類自動建表的功能,可是僅僅這麼作是不夠了,不知道爲何ORMlite還須要設置 AUTO_CREATE_TABLE 這個property才能實現自動建表,根據官方文檔,須要在spring初始化以前設置System.property,略顯無聊啊!
官網示例傳送門
看見第27行的代碼嗎:git
System.setProperty(TableCreator.AUTO_CREATE_TABLES, Boolean.toString(true));
好吧,我被擊敗了,要在spring加載以前執行這句話,那只有重寫spring的Listener了,來吧:github
package com.saiyang.newflypig.rwt.servlet; import javax.servlet.ServletContextEvent; import org.springframework.web.context.ContextLoaderListener; import com.j256.ormlite.spring.TableCreator; public class MyContextLoaderListener extends ContextLoaderListener { @Override public void contextInitialized(ServletContextEvent event) { System.setProperty(TableCreator.AUTO_CREATE_TABLES, Boolean.toString(true)); super.contextInitialized(event); } }
再修改web.xml:web
<!-- 配置Spring --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:/spring-*.xml</param-value> </context-param> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath*:/log4j.properties</param-value> </context-param> <listener> <listener-class>com.saiyang.newflypig.rwt.servlet.MyContextLoaderListener</listener-class> </listener>
至此完成spring的配置。在service中就能夠直接從spring容器中取出userDao進行User對象的持久化操做了。spring
官方在github有一段spring的配置示例是有問題的,將databaseUrl寫成了url,致使spring加載時一直報錯,後來查看源代碼才發現變量名稱有問題,這個bug來自於 這裏 ,在第23行,url應該寫成databaseUrl。sql