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编辑器,排版很好,赞一下,博客园真的是时尚时尚最时尚!

SQLite及ORMlite在WebApp中的使用的更多相关文章

  1. WebApp中的页面生命周期及路由管理

    最近切换到一个新项目,使用的技术栈是Require+Backbone,鉴于对鞋厂webapp框架的了解,发现这个新项目有些缺陷,主要是单纯依赖Backbone造成的,也就是Backbone的好和坏都在 ...

  2. tomcat中session在两个webapp中实现共享

    现在遇到一个需求就是要求完成简单的单点登录,通过在一个tomcat实例中放置两个webapps应用ROOT应用和CEO应用来完成在ROOT应用登录后,在CEO可以直接使用,而未在ROOT应用登录时,不 ...

  3. ormlite 在android中 排序 条件查询

    ormlite 在android中 排序 条件查询 all = dao.queryBuilder().orderBy("Id", true).where().eq("Ty ...

  4. 如何在webapp中做出原生的ios下拉菜单效果

    github:https://github.com/zhoushengmufc/iosselect webapp模仿ios下拉菜单 html下拉菜单select在安卓和IOS下表现不一样,iossel ...

  5. 【HTML5&CSS3进阶04】CSS3动画应该如何在webapp中运用

    动画在webapp的现状 webapp模式的网站追求的就是一个体验,是HTML5&CSS3浪潮下的产物,抛开体验不说,webapp模式门槛比较高: 而体验优化的一个重点便是动画,可以说动画是w ...

  6. 动画在webapp中的现状

    webapp的一大优势便是在view切换时候可以拥有媲美与native的动画效果,但是很多时候那只是一种想法,真正的情况却不是这样 产生此问题的原因有: ① 手机CPU烂! ② 手机显卡烂!就算四核其 ...

  7. tomcat ROOT中的lib和webapp中的lib的作用

    相同点:都是用来存放jar包的 不同点:和webapps同个目录下的那个lib文件夹所放的jar包对tomcat 服务器和你的webapp 来说都是可以调用的(这时候假如tomcat和web都依赖某个 ...

  8. webapp中的meta

    <!--开发后删除--> <meta http-equiv="Pragma" name="no-store" /><!--必须联网 ...

  9. 关于webapp中的文字单位的一些捣腾

    前言 文字是网页内容的一枚大将,我们无时无刻都在看着它,只要是你盯屏幕上的任何一个地方都会有文字.地铁上无时无刻都在盯着屏幕上的人对于文字更为敏感,太大不行,太小TN又看不清上面到底在说什么,有时候车 ...

随机推荐

  1. Nodejs Express 4.X 中文API 1--- Application篇

    相关阅读: Express 4.X API 翻译[一] --  Application篇 Express4.XApi 翻译[二] --  Request篇 Express4.XApi 翻译[三] -- ...

  2. wireshark常用的过滤命令

    我们使用wireshark抓包,却不知道如何分析这些包,也无法从海量的包中提取自己需要的数据,下面简单介绍下wireshark的过滤规则. 过滤源ip.目的ip.在wireshark的过滤规则框Fil ...

  3. 如何在Asp.net中备份Access数据库?

    public   void   Create(   string   mdbPath   ) { if(   File.Exists(mdbPath)   )   //检查数据库是否已存在 { thr ...

  4. 使用 Microsoft Word 发布博客文章

    以 Microsoft Word 2010 为例: 依次选择:文件 -> 保存并发送 -> 发布为博客文章 配置说明:新建账户 的 博客文章 URL  一栏填写 http://rpc.cn ...

  5. Hadoop 系统配置 map 100% reduce 0%

    之前在本地配置了hadoop伪分布模式,hdfs用起来没问题,mapreduce的单机模式也没问题. 今天写了个程序,想在伪分布式上跑一下mapreduce,结果出现 map 100% reduce ...

  6. 理解Session的几种模式

    一.写在前面 我们在使用ASP.NET开发的过程中,有时会进行数据存储以实现请求前后的状态保持(HTTP是无状态保持的协议),而Session作为一种快速简单易于实现的方式被我们经常使用,当然如果出于 ...

  7. ftp命令和scp命令

    ftp命令: 服务器有安装ftp Server,另外一台linux可以使用ftp的client程序来进行文件的拷贝读取和下载. 1. 连接ftp服务器  格式:ftp [hostname| ip-ad ...

  8. visual studio 2012 Github

    前言 一直以来都想使用Git来管理自己平时积累的小代码,就是除了工作之外的代码了.有时候自己搞个小代码,在公司写了,就要通过U盘或者网盘等等一系列工具进行Copy,然后回家才能继续在原来的基础上作业. ...

  9. Linux客户/服务器程序设计范式1——并发服务器(多进程)

    引言 本文会写一个并发服务器(concurrent server)程序,它为每个客户请求fork出一个子进程. 注意 1. 信号处理问题 对于相同信号,按信号的先后顺序依次处理.可能会产生的问题是,正 ...

  10. redis资料汇总

    redis资源比较零散,引用nosqlfan上的文章,方便大家需要时翻阅.大家看完所有的,如果整理出文章的,麻烦知会一下,方便学习. 1.Redis是什么? 十五分钟介绍 Redis数据结构 Redi ...