给JFinal添加 Sqlite 数据库支持
[转自] 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将增加此类)
package com.jfinal.plugin.activerecord.dialect; import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import com.jfinal.plugin.activerecord.ActiveRecordException;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.TableInfo; /**
* Sqlite3Dialect. Try to use Sqlite3 SQL dialect with ActiveRecordPlugin.
* <p>
* A clever person solves a problem. A wise person avoids it.
*/
public class Sqlite3SqlDialect implements IDialect { public String forTableInfoBuilderDoBuildTableInfo(String tableName) {
return "select * from " + tableName + " where 1 = 2";
} public void forModelSave(TableInfo tableInfo, Map<String, Object> attrs, StringBuilder sql, List<Object> paras) {
sql.append("insert into ").append(tableInfo.getTableName()).append("(");
StringBuilder temp = new StringBuilder(") values(");
for (Entry<String, Object> e: attrs.entrySet()) {
String colName = e.getKey();
if (tableInfo.hasColumnLabel(colName)) {
if (paras.size() > 0) {
sql.append(", ");
temp.append(", ");
}
sql.append(colName);
temp.append("?");
paras.add(e.getValue());
}
}
sql.append(temp.toString()).append(")");
} public String forModelDeleteById(TableInfo tInfo) {
String pKey = tInfo.getPrimaryKey();
StringBuilder sql = new StringBuilder(45);
sql.append("delete from ");
sql.append(tInfo.getTableName());
sql.append(" where ").append(pKey).append(" = ?");
return sql.toString();
} public void forModelUpdate(TableInfo tableInfo, Map<String, Object> attrs, Set<String> modifyFlag, String pKey, Object id, StringBuilder sql, List<Object> paras) {
sql.append("update ").append(tableInfo.getTableName()).append(" set ");
for (Entry<String, Object> e : attrs.entrySet()) {
String colName = e.getKey();
if (!pKey.equalsIgnoreCase(colName) && modifyFlag.contains(colName) && tableInfo.hasColumnLabel(colName)) {
if (paras.size() > 0)
sql.append(", ");
sql.append(colName).append(" = ? ");
paras.add(e.getValue());
}
}
sql.append(" where ").append(pKey).append(" = ?");
paras.add(id);
} public String forModelFindById(TableInfo tInfo, String columns) {
StringBuilder sql = new StringBuilder("select ");
if (columns.trim().equals("*")) {
sql.append(columns);
}
else {
String[] columnsArray = columns.split(",");
for (int i=0; i<columnsArray.length; i++) {
if (i > 0)
sql.append(", ");
sql.append(columnsArray[i].trim());
}
}
sql.append(" from ");
sql.append(tInfo.getTableName());
sql.append(" where ").append(tInfo.getPrimaryKey()).append(" = ?");
return sql.toString();
} public String forDbFindById(String tableName, String primaryKey, String columns) {
StringBuilder sql = new StringBuilder("select ");
if (columns.trim().equals("*")) {
sql.append(columns);
}
else {
String[] columnsArray = columns.split(",");
for (int i=0; i<columnsArray.length; i++) {
if (i > 0)
sql.append(", ");
sql.append(columnsArray[i].trim());
}
}
sql.append(" from ");
sql.append(tableName.trim());
sql.append(" where ").append(primaryKey).append(" = ?");
return sql.toString();
} public String forDbDeleteById(String tableName, String primaryKey) {
StringBuilder sql = new StringBuilder("delete from ");
sql.append(tableName.trim());
sql.append(" where ").append(primaryKey).append(" = ?");
return sql.toString();
} public void forDbSave(StringBuilder sql, List<Object> paras, String tableName, Record record) {
sql.append("insert into ");
sql.append(tableName.trim()).append("(");
StringBuilder temp = new StringBuilder();
temp.append(") values("); for (Entry<String, Object> e: record.getColumns().entrySet()) {
if (paras.size() > 0) {
sql.append(", ");
temp.append(", ");
}
sql.append(e.getKey());
temp.append("?");
paras.add(e.getValue());
}
sql.append(temp.toString()).append(")");
} public void forDbUpdate(String tableName, String primaryKey, Object id, Record record, StringBuilder sql, List<Object> paras) {
sql.append("update ").append(tableName.trim()).append(" set ");
for (Entry<String, Object> e: record.getColumns().entrySet()) {
String colName = e.getKey();
if (!primaryKey.equalsIgnoreCase(colName)) {
if (paras.size() > 0) {
sql.append(", ");
}
sql.append(colName).append(" = ? ");
paras.add(e.getValue());
}
}
sql.append(" where ").append(primaryKey).append(" = ?");
paras.add(id);
} public void forPaginate(StringBuilder sql, int pageNumber, int pageSize, String select, String sqlExceptSelect) {
int offset = pageSize * (pageNumber - 1);
sql.append(select).append(" ");
sql.append(sqlExceptSelect);
sql.append(" limit ").append(offset).append(", ").append(pageSize); // limit can use one or two '?' to pass paras
}
}
6、在DemoConfig.java (或你自己的 Config 类)中:
import com.jfinal.plugin.activerecord.dialect.Sqlite3Dialect;
public void configPlugin(Plugins me) {
// 配置C3p0数据库连接池插件
C3p0Plugin c3p0Plugin = new C3p0Plugin(getProperty("jdbcUrl"), getProperty("user"), getProperty("password"));
c3p0Plugin.setDriverClass("org.sqlite.JDBC"); //指定驱动程序
me.add(c3p0Plugin);
// 配置ActiveRecord插件
ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
me.add(arp);
arp.setDialect(new Sqlite3Dialect()); //指定 Dialect
arp.addMapping("blog", Blog.class); // 映射blog 表到 Blog模型
}
7、至此大功告成!启动,服务器,在浏览器中查看效果吧!
给JFinal添加 Sqlite 数据库支持的更多相关文章
- 让PDF.NET支持最新的SQLite数据库
最近项目中用到了SQLite,之前项目中用的是PDF.NET+MySQL的组合,已经写了不少代码,如果能把写好的代码直接用在SQLite上就好了,PDF.NET支持大部分主流的数据库,这个当然可以,只 ...
- Android之SQLite数据库篇
一.SQLite简介 Google为Andriod的较大的数据处理提供了SQLite,他在数据存储.管理.维护等各方面都相当出色,功能也非常的强大. 二.SQLite的特点 1.轻量级使用 SQLit ...
- Android开发-之SQLite数据库
之前我们讲了如何将数据存储在文件中,那么除了这种方式呢,就是我们常见的大家都知道的将数据存储在数据库当中了. 将数据存储在数据库中的优势: 1)存储在数据库中的数据更加方便操作,比如增.删.改.查等 ...
- android开发--sqlite数据库
一.SQLite简介 Google为Andriod的较大的数据处理提供了SQLite,他在数据存储.管理.维护等各方面都相当出色,功能也非常的强大.SQLite具备下列特点: 1.轻量级 使用 SQL ...
- 安卓数据存储(3):SQLite数据库存储
SQLite简介 Google为Andriod的较大的数据处理提供了SQLite,他在数据存储.管理.维护等各方面都相当出色,功能也非常的强大.SQLite具备下列特点: 1.轻量级:使用 SQLit ...
- Android 开发中使用 SQLite 数据库
SQLite 介绍 SQLite 一个非常流行的嵌入式数据库,它支持 SQL 语言,并且只利用很少的内存就有很好的性能. 此外它还是开源的,任何人都可以使用它.许多开源项目((Mozilla, PHP ...
- Android 之数据存储(sdCard,sharedPreference,sqlite数据库)
sdCard:默认路径在 /storage/sdcard/... Android支持OpenFileOutput和openFileInput方式访问手机存储器上的文件. Context提供了如下两个方 ...
- 【Android 应用开发】Android 数据存储 之 SQLite数据库详解
. 作者 :万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/19028665 . SQLiteDataBase示例程序下 ...
- Android 数据存储 之 SQLite数据库详解
. 作者 :万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/19028665 . SQLiteDataBase示例程序下 ...
随机推荐
- python pipelines 用法
http://zacstewart.com/2014/08/05/pipelines-of-featureunions-of-pipelines.html http://blog.csdn.net/m ...
- 用Linq取两个数组的差集
两个数组,取其差集,用Linq做比较方便,效率也比较高,具体如下示例 有两个数组list1 和list2 ,如下 List<int> list1 = new List<int> ...
- storm源码分析之任务分配--task assignment
在"storm源码分析之topology提交过程"一文最后,submitTopologyWithOpts函数调用了mk-assignments函数.该函数的主要功能就是进行topo ...
- poj 1988 Cube Stacking (并查集)
题意:有N(N<=30,000)堆方块,开始每堆都是一个方块.方块编号1 – N. 有两种操作: M x y : 表示把方块x所在的堆,拿起来叠放到y所在的堆上. C x : 问方块x下面有多少 ...
- The user specified as a definer (”@sa’%') does not exist 解决方法
mysql数据库报错The user specified as a definer (”@sa’%') does not exist.尝试过两种方式,第一种重启之后好用,但是一会就又不好用了.第二种算 ...
- App测试从入门到精通之更新测试
我们都知道,app在使用一段时间,都会有更新,而且更新会不止一次.在实际测试中,关于更新的测试场景也是我们需要重点关注的,接下来我们就看一下关于App的更新测试有哪些测试点我们需要注意: APP更新测 ...
- HDU 4081 Peach Blossom Spring (最小生成树+dfs)
题意:给定一个 n 个点和相应的权值,要求你用 n-1 条边连接起来,其中一条边是魔法边,不用任何费用,其他的边是长度,求该魔法边的两端的权值与其他边费用的尽量大. 析:先求出最小生成树,然后再枚举每 ...
- URAL 1748. The Most Complex Number(反素数)
题目链接 题意 :给你一个n,让你找出小于等于n的数中因子个数最多的那个数,并且输出因子个数,如果有多个答案,输出数最小的那个 思路 : 官方题解 : (1)此题最容易想到的是穷举,但是肯定超时. ( ...
- Codeforces 12D Ball(线段树)
N ladies attend the ball in the King's palace. Every lady can be described with three values: beauty ...
- MongoDB整理笔记の减少节点
当应用的压力小时,可以减少一些节点来减少硬件资源的成本:总之这是一个长期且持续的工作. 下面将刚刚添加的两个新节点28013 和28014 从复制集中去除掉,只需执行rs.remove 指令就可以了, ...