[转自] http://my.oschina.net/u/237078/blog/69934

Sqlite 的单文件便携性、高性能在开发中方便性无与伦比,即使部署在中小型应用中也胜任有余。

在JFinal中添加对 Sqlite 的支持 Step by Step:

1、点击 http://www.xerial.org/maven/repository/artifact/org/xerial/sqlite-jdbc/3.7.2/sqlite-jdbc-3.7.2.jar 下载
sqlite JDBC驱动,并将其放入 Jfinal_app/WEB-INF/lib ;

2、下载一个Sqlite的数据库管理工具,推荐 Navicate Premium,在此下载:http://www.navicat.com/en/products/navicat_premium/premium_overview.html ;或者你也可以到
Sqlite 官网下载Sqlite Shell ,当前版本是 3.7.13: http://www.sqlite.org/sqlite-shell-win32-x86-3071300.zip ;

3、使用数据库管理工具创建一个数据库,存放路径为 Jfinal_app/WEB-INF/data.db ;创建必要的表,插入测试记录,等等;

4、在 a_little_config.txt (或你自己的配置文件) 中设置:

jdbcUrl = jdbc:sqlite:D:/webs/Jfinal_app/WEB-INF/data.db 

user = 

password = 

devMode = true

5、新建一个类 Sqlite3Dialect.java :(注:也可以等待JFinal 1.1.1版Jar将增加此类)

  1. package com.jfinal.plugin.activerecord.dialect;
  2.  
  3. import java.util.List;
  4. import java.util.Map;
  5. import java.util.Map.Entry;
  6. import java.util.Set;
  7. import com.jfinal.plugin.activerecord.ActiveRecordException;
  8. import com.jfinal.plugin.activerecord.Record;
  9. import com.jfinal.plugin.activerecord.TableInfo;
  10.  
  11. /**
  12. * Sqlite3Dialect. Try to use Sqlite3 SQL dialect with ActiveRecordPlugin.
  13. * <p>
  14. * A clever person solves a problem. A wise person avoids it.
  15. */
  16. public class Sqlite3SqlDialect implements IDialect {
  17.  
  18. public String forTableInfoBuilderDoBuildTableInfo(String tableName) {
  19. return "select * from " + tableName + " where 1 = 2";
  20. }
  21.  
  22. public void forModelSave(TableInfo tableInfo, Map<String, Object> attrs, StringBuilder sql, List<Object> paras) {
  23. sql.append("insert into ").append(tableInfo.getTableName()).append("(");
  24. StringBuilder temp = new StringBuilder(") values(");
  25. for (Entry<String, Object> e: attrs.entrySet()) {
  26. String colName = e.getKey();
  27. if (tableInfo.hasColumnLabel(colName)) {
  28. if (paras.size() > 0) {
  29. sql.append(", ");
  30. temp.append(", ");
  31. }
  32. sql.append(colName);
  33. temp.append("?");
  34. paras.add(e.getValue());
  35. }
  36. }
  37. sql.append(temp.toString()).append(")");
  38. }
  39.  
  40. public String forModelDeleteById(TableInfo tInfo) {
  41. String pKey = tInfo.getPrimaryKey();
  42. StringBuilder sql = new StringBuilder(45);
  43. sql.append("delete from ");
  44. sql.append(tInfo.getTableName());
  45. sql.append(" where ").append(pKey).append(" = ?");
  46. return sql.toString();
  47. }
  48.  
  49. public void forModelUpdate(TableInfo tableInfo, Map<String, Object> attrs, Set<String> modifyFlag, String pKey, Object id, StringBuilder sql, List<Object> paras) {
  50. sql.append("update ").append(tableInfo.getTableName()).append(" set ");
  51. for (Entry<String, Object> e : attrs.entrySet()) {
  52. String colName = e.getKey();
  53. if (!pKey.equalsIgnoreCase(colName) && modifyFlag.contains(colName) && tableInfo.hasColumnLabel(colName)) {
  54. if (paras.size() > 0)
  55. sql.append(", ");
  56. sql.append(colName).append(" = ? ");
  57. paras.add(e.getValue());
  58. }
  59. }
  60. sql.append(" where ").append(pKey).append(" = ?");
  61. paras.add(id);
  62. }
  63.  
  64. public String forModelFindById(TableInfo tInfo, String columns) {
  65. StringBuilder sql = new StringBuilder("select ");
  66. if (columns.trim().equals("*")) {
  67. sql.append(columns);
  68. }
  69. else {
  70. String[] columnsArray = columns.split(",");
  71. for (int i=0; i<columnsArray.length; i++) {
  72. if (i > 0)
  73. sql.append(", ");
  74. sql.append(columnsArray[i].trim());
  75. }
  76. }
  77. sql.append(" from ");
  78. sql.append(tInfo.getTableName());
  79. sql.append(" where ").append(tInfo.getPrimaryKey()).append(" = ?");
  80. return sql.toString();
  81. }
  82.  
  83. public String forDbFindById(String tableName, String primaryKey, String columns) {
  84. StringBuilder sql = new StringBuilder("select ");
  85. if (columns.trim().equals("*")) {
  86. sql.append(columns);
  87. }
  88. else {
  89. String[] columnsArray = columns.split(",");
  90. for (int i=0; i<columnsArray.length; i++) {
  91. if (i > 0)
  92. sql.append(", ");
  93. sql.append(columnsArray[i].trim());
  94. }
  95. }
  96. sql.append(" from ");
  97. sql.append(tableName.trim());
  98. sql.append(" where ").append(primaryKey).append(" = ?");
  99. return sql.toString();
  100. }
  101.  
  102. public String forDbDeleteById(String tableName, String primaryKey) {
  103. StringBuilder sql = new StringBuilder("delete from ");
  104. sql.append(tableName.trim());
  105. sql.append(" where ").append(primaryKey).append(" = ?");
  106. return sql.toString();
  107. }
  108.  
  109. public void forDbSave(StringBuilder sql, List<Object> paras, String tableName, Record record) {
  110. sql.append("insert into ");
  111. sql.append(tableName.trim()).append("(");
  112. StringBuilder temp = new StringBuilder();
  113. temp.append(") values(");
  114.  
  115. for (Entry<String, Object> e: record.getColumns().entrySet()) {
  116. if (paras.size() > 0) {
  117. sql.append(", ");
  118. temp.append(", ");
  119. }
  120. sql.append(e.getKey());
  121. temp.append("?");
  122. paras.add(e.getValue());
  123. }
  124. sql.append(temp.toString()).append(")");
  125. }
  126.  
  127. public void forDbUpdate(String tableName, String primaryKey, Object id, Record record, StringBuilder sql, List<Object> paras) {
  128. sql.append("update ").append(tableName.trim()).append(" set ");
  129. for (Entry<String, Object> e: record.getColumns().entrySet()) {
  130. String colName = e.getKey();
  131. if (!primaryKey.equalsIgnoreCase(colName)) {
  132. if (paras.size() > 0) {
  133. sql.append(", ");
  134. }
  135. sql.append(colName).append(" = ? ");
  136. paras.add(e.getValue());
  137. }
  138. }
  139. sql.append(" where ").append(primaryKey).append(" = ?");
  140. paras.add(id);
  141. }
  142.  
  143. public void forPaginate(StringBuilder sql, int pageNumber, int pageSize, String select, String sqlExceptSelect) {
  144. int offset = pageSize * (pageNumber - 1);
  145. sql.append(select).append(" ");
  146. sql.append(sqlExceptSelect);
  147. sql.append(" limit ").append(offset).append(", ").append(pageSize); // limit can use one or two '?' to pass paras
  148. }
  149. }

6、在DemoConfig.java (或你自己的 Config 类)中:

  1. import com.jfinal.plugin.activerecord.dialect.Sqlite3Dialect;
  2.  
  3. public void configPlugin(Plugins me) {
  4. // 配置C3p0数据库连接池插件
  5. C3p0Plugin c3p0Plugin = new C3p0Plugin(getProperty("jdbcUrl"), getProperty("user"), getProperty("password"));
  6. c3p0Plugin.setDriverClass("org.sqlite.JDBC"); //指定驱动程序
  7. me.add(c3p0Plugin);
  8. // 配置ActiveRecord插件
  9. ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
  10. me.add(arp);
  11. arp.setDialect(new Sqlite3Dialect()); //指定 Dialect
  12. arp.addMapping("blog", Blog.class); // 映射blog 表到 Blog模型
  13. }

7、至此大功告成!启动,服务器,在浏览器中查看效果吧!

给JFinal添加 Sqlite 数据库支持的更多相关文章

  1. 让PDF.NET支持最新的SQLite数据库

    最近项目中用到了SQLite,之前项目中用的是PDF.NET+MySQL的组合,已经写了不少代码,如果能把写好的代码直接用在SQLite上就好了,PDF.NET支持大部分主流的数据库,这个当然可以,只 ...

  2. Android之SQLite数据库篇

    一.SQLite简介 Google为Andriod的较大的数据处理提供了SQLite,他在数据存储.管理.维护等各方面都相当出色,功能也非常的强大. 二.SQLite的特点 1.轻量级使用 SQLit ...

  3. Android开发-之SQLite数据库

    之前我们讲了如何将数据存储在文件中,那么除了这种方式呢,就是我们常见的大家都知道的将数据存储在数据库当中了. 将数据存储在数据库中的优势: 1)存储在数据库中的数据更加方便操作,比如增.删.改.查等 ...

  4. android开发--sqlite数据库

    一.SQLite简介 Google为Andriod的较大的数据处理提供了SQLite,他在数据存储.管理.维护等各方面都相当出色,功能也非常的强大.SQLite具备下列特点: 1.轻量级 使用 SQL ...

  5. 安卓数据存储(3):SQLite数据库存储

    SQLite简介 Google为Andriod的较大的数据处理提供了SQLite,他在数据存储.管理.维护等各方面都相当出色,功能也非常的强大.SQLite具备下列特点: 1.轻量级:使用 SQLit ...

  6. Android 开发中使用 SQLite 数据库

    SQLite 介绍 SQLite 一个非常流行的嵌入式数据库,它支持 SQL 语言,并且只利用很少的内存就有很好的性能. 此外它还是开源的,任何人都可以使用它.许多开源项目((Mozilla, PHP ...

  7. Android 之数据存储(sdCard,sharedPreference,sqlite数据库)

    sdCard:默认路径在 /storage/sdcard/... Android支持OpenFileOutput和openFileInput方式访问手机存储器上的文件. Context提供了如下两个方 ...

  8. 【Android 应用开发】Android 数据存储 之 SQLite数据库详解

    . 作者 :万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/19028665 . SQLiteDataBase示例程序下 ...

  9. Android 数据存储 之 SQLite数据库详解

    . 作者 :万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/19028665 . SQLiteDataBase示例程序下 ...

随机推荐

  1. Python pandas.DataFrame调整列顺序及修改index名

    1. 从字典创建DataFrame >>> import pandas >>> dict_a = {'],'mark_date':['2017-03-07','20 ...

  2. CAP理论与HBase

    The short summary of the article is that CAP isn't "C, A, or P, choose two," but rather &q ...

  3. c# 二分查找法(2分钟算法)

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  4. WordCountPro

    github项目地址:https://github.com/Hoyifei/SQ-T-Homework-WordCount-Advanced PSP表格   PSP2.1 PSP阶段 预估耗时 (分钟 ...

  5. Asp.net WebPages框架运行原理浅析

    [来源] 达内    [编辑] 达内   [时间]2012-09-14 在Asp.net4和4.5中,新增了WebPages Framework,编写页面代码使用了新的Razor语法,代码更加的简洁和 ...

  6. 桥梁(Bridge)模式

    桥梁(Bridge)模式:桥梁模式是一个非常有用的模式,也是比较复杂的一个模式.熟悉这个模式对于理解面向对象的设计原则,包括"开-闭"原则(OCP)以及组合/聚合复用原则(CARP ...

  7. java學習書

    轉載 成为Java顶尖程序员 ,看这11本书就够了 以下是我推荐给Java开发者们的一些值得一看的好书.但是这些书里面并没有Java基础.Java教程之类的书,不是我不推荐,而是离我自己学习 Java ...

  8. [python]模块及包

    一 .module 通常模块为一个文件,直接使用import来导入就好了.可以作为module的文件类型有".py".".pyo".".pyc&quo ...

  9. 选择性的使用 serialize() 进行序列化

    serialize 非常方便的帮我们创建 URL 编码文本字符串 输出的字符串格式为 a=1&b=2&c=3  直接可用于Url传参 下面介绍一下选择性的序列化某些标签的使用方法 将 ...

  10. angular component元素