SQLite及ORMlite在WebApp中的使用

时间:2023-03-08 20:58:04

Spring 配置

下面的databaseUrl在windows下,指向了c:/user/yourhome路径,暂时没想到怎么配置到WEBAPP根路径下。

因为是轻量级工控webapp,数据库规模不大,也不需要暴露URL给其他主机访问,所以选择了SQLite,Hibernate用惯了,需要使用ORM来做数据库操作,所以选择了ORMlite,J2EE开发习惯了,所以使用了Spring。

<?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>

web.xml 配置

上面的 spring-core.xml 最下面一个bean是配置根据Entity类自动建表的功能,但是仅仅这么做是不够了,不知道为什么ORMlite还需要设置 AUTO_CREATE_TABLE 这个property才能实现自动建表,根据官方文档,需要在spring初始化之前设置System.property,略显无聊啊!

官网示例传送门

看见第27行的代码吗:

System.setProperty(TableCreator.AUTO_CREATE_TABLES, Boolean.toString(true));

好吧,我被击败了,要在spring加载之前执行这句话,那只有重写spring的Listener了,来吧:

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:

    <!-- 配置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对象的持久化操作了。

PS:官方一个bug

官方在github有一段spring的配置示例是有问题的,将databaseUrl写成了url,导致spring加载时一直报错,后来查看源代码才发现变量名称有问题,这个bug来自于 这里 ,在第23行,url应该写成databaseUrl。

第一次用博客园的markdown编辑器,排版很好,赞一下,博客园真的是时尚时尚最时尚!