您的位置:首页 > 其它

greenDao实践,使用orm优秀的第三方框架

2016-05-10 17:33 387 查看
今天公司app需要使用到数据库缓存相关信息,最终选择采用第三方ORM库来代替手输SQL语句的代码。

ORM主要的优点:

1.ORM隐藏了数据访问的细节,使代码简洁;

2.避免了手输sql语句带来的bug,以及代码写的难度;

3.直接转化数据为指定的对象,使代码层面统一;

4.为代码的修改提供了方便、降低编码难度。

ORM缺点:

1.可能对app这种需要及时的处理用户操作的软件,可能会影响性能;

2.复杂的查询语句实现的难度会比较大;

3.程序员使用ORM的时候需要付出一定的学习成本。

基于主要的性能的考虑,网上有很多文章测试后,发现greenDao在所有的ORM第三方框架速度是最快的,评价也很不错,最终选择在项目中加入greenDao。

使用流程:

1.加入第三方的库:

compile 'de.greenrobot:greendao:2.1.0'
compile 'de.greenrobot:greendao-generator:2.1.0'


2.创建一个java同目录下的java-gen

2.创建一个generator:

package com.android.icredit.db;

import java.io.File;
import java.io.IOException;

import de.greenrobot.daogenerator.DaoGenerator;
import de.greenrobot.daogenerator.Entity;
import de.greenrobot.daogenerator.Schema;

public class ICreditDaoGenerator {
public static void main(String[] args) throws Exception {
// 创建了一个用于添加实体(Entity)的模式(Schema)对象。
// 两个参数分别代表:数据库版本号与自动生成代码的包路径。
//Schema schema = new Schema(1, "credit.greendao");
//当然,如果你愿意,你也可以分别指定生成的 Bean 与 DAO 类所在的目录,只要如下所示:
Schema schema = new Schema(1, "credit.bean");
schema.setDefaultJavaPackageDao("credit.dao");

// 模式(Schema)同时也拥有两个默认的 flags,分别用来标示 entity 是否是 active 以及是否使用 keep sections。
// schema2.enableActiveEntitiesByDefault();
// schema2.enableKeepSectionsByDefault();

// 一旦你拥有了一个 Schema 对象后,你便可以使用它添加实体(Entities)了。
addSearchHistoryTable(schema);

// 最后我们将使用 DAOGenerator 类的 generateAll() 方法自动生成代码,此处你需要根据自己的情况更改输出目录(既之前创建的 java-gen)。
String destPath = getAppDirPath() + "app\\src\\main\\java-gen";

System.out.println(destPath);
new DaoGenerator().generateAll(schema, destPath);
}

/**
* 动态获得项目所在的绝对路径
*
* @return dirPath 项目所在的绝对路径
* @throws IOException
*/

private static String getAppDirPath() throws IOException {
//一个虚建的file,获得其父类路径,就是项目的绝对路径
String flagStr = "..@";
File file = new File(flagStr);

String path = file.getAbsolutePath();
int endIndex = path.lastIndexOf(flagStr);

//通过裁剪字符串获得路径
String dirPath = path.substring(0, endIndex);
return dirPath;
}

/**
* @param schema
*/

private static void addSearchHistoryTable(Schema schema) {
// 一个实体(类)就关联到数据库中的一张表,此处表名为「SearchHistory」(既类名)
Entity searchHistory = schema.addEntity("SearchHistory");

// greenDAO 会自动根据实体类的属性值来创建表字段,并赋予默认值
// 接下来你便可以设置表中的字段:
searchHistory.addIdProperty();
searchHistory.addStringProperty("userId")
.javaDocField("用户名\n不同的用户保存不同的历史信息");
// 与在 Java 中使用驼峰命名法不同,默认数据库中的命名是使用大写和下划线来分割单词的。
// For example, a property called “creationDate” will become a database column “CREATION_DATE”.
searchHistory.addStringProperty("keyword")
.javaDocField("关键字\n" +
"用户输入或者点击的关键字");
searchHistory.addStringProperty("historyType")

a457
.javaDocField("历史类别\n" +
"搜索历史:001\n" +
"浏览历史:002");
searchHistory.addStringProperty("searchEntrance").javaDocField("搜索的入口\n" +
".......waiting to add");
}

}



以上代码中包含了利用greenDao自动生成表的代码,参照代码即可自己生成对应的表,同时也可以参照官网api

4.在你的Application中加入获取全局的DaoMaster和DaoSession

//数据库相关的变量
public static DaoMaster daoMaster;
private static DaoSession daoSession;

/**
* 取得DaoMaster
* @return DaoMaster
**/
public static DaoMaster getDaoMaster() {
if (daoMaster == null) {
DaoMaster.OpenHelper helper = new DaoMaster.DevOpenHelper(application.getApplicationContext()
, DBConstants.DB_NAME, null);
daoMaster = new DaoMaster(helper.getWritableDatabase());
}
return daoMaster;
}
/**
* 取得DaoSession
* @return DaoSession
**/
public static DaoSession getDaoSession()
{
if (daoSession == null)
{
if (daoMaster == null)
{
daoMaster = getDaoMaster();
}
daoSession = daoMaster.newSession();
}
return daoSession;
}


5.经过以上步骤,已经可以使用greenDao进行增、删、改、查的操作了,详见官网:http://greenrobot.org/greendao/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: