greenDAO
greenDAO是一个轻和快速的ORM,用于将对象映射到SQLite数据库。针对Android进行高度优化,greenDAO提供了出色的性能,并消耗最少的内存。
主页,文档和支持链接:http://greenrobot.org/greendao/
特征
greenDAO的独特的功能集:
- 介绍:greenDAO自2011年以来一直使用,并被无数的着名应用程序使用
- 超简单:简明直观的API,在V3中带有注释
- 小:库是<150K,它只是纯Java jar(没有CPU依赖的本地部分)
- 快速:可能是Android中最快的ORM,由智能代码生成驱动
- 安全和表达式查询API:QueryBuilder使用属性常量来避免打印错误
- 强大的连接:跨实体查询,甚至链接连接的复杂关系
- 灵活的属性类型:使用自定义类或枚举来表示实体中的数据
- 加密:支持SQLCipher加密数据库
将greenDAO添加到项目中
greenDAO在Maven Central上可用。请确保您使用的是最新版本通过检查在这里 和这里
将以下Gradle配置添加到Android项目中:
buildscript { repositories { mavenCentral() } dependencies { classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1' } } apply plugin: 'org.greenrobot.greendao' dependencies { compile 'org.greenrobot:greendao:3.2.0' }
请注意,这会将greenDAO Gradle插件挂接到您的构建过程。当你构建项目时,它会生成类DaoMaster,DaoSession和DAO。
实例:开发环境:Win7 开发工具:Android Studio 2.2
一、在ANDROID工程中配置“GREENDAO GENERATOR”模块
1. 在的.src /主目录下新建一个与Java的同层级的「Java的根」目录,用于存放由greenDAO生成的豆,DAO,DaoMaster,DaoSession等类。
2.配置的Android工程(应用程序)的的build.gradle,如图分别添加 sourceSets 与依赖。
sourceSets{
main{
jniLibs.srcDirs = ['libs']
java.srcDirs = ['src/main/java', 'src/main/java-gen']
}
}
compile 'de.greenrobot:greendao:1.3.7'
二、 新建“GREENDAO GENERATOR”模块(纯JAVA工程)
1. 通过文件 - >新建 - >新建模块 - > Java库 - >填写相应的包名与类名 - >完成。
2. 配置daoexamplegenerator工程的build.gradle,添加依赖
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'de.greenrobot:greendao-generator:1.3.1'
}
编写 DaoGenerator 类,注意: 我们的 Java 工程只有一个类,它的内容决定了「GreenDao Generator」的输出,你可以在这个类中通过对象、关系等创建数据库结构
public class ExampleDaogenerator {
public static void main(String[] args) throws Exception {
// 正如你所见的,你创建了一个用于添加实体(Entity)的模式(Schema)对象。
// 两个参数分别代表:数据库版本号与自动生成代码的包路径。
Schema schema = new Schema(1, "doo.ddy.greendao");
// 当然,如果你愿意,你也可以分别指定生成的 Bean 与 DAO 类所在的目录,只要如下所示:
// Schema schema = new Schema(1, "me.itangqi.bean");
// schema.setDefaultJavaPackageDao("me.itangqi.dao");
// 模式(Schema)同时也拥有两个默认的 flags,分别用来标示 entity 是否是 activie 以及是否使用 keep sections。
// schema2.enableActiveEntitiesByDefault();
// schema2.enableKeepSectionsByDefault();
// 一旦你拥有了一个 Schema 对象后,你便可以使用它添加实体(Entities)了。
addNote(schema);
addEmp(schema);
// 最后我们将使用 DAOGenerator 类的 generateAll() 方法自动生成代码,此处你需要根据自己的情况更改输出目录(既之前创建的 java-gen)。
// 其实,输出目录的路径可以在 build.gradle 中设置,有兴趣的朋友可以自行搜索,这里就不再详解。
new DaoGenerator().generateAll(schema, "E:\\Android\\dyl\\Doo\\app\\src\\main\\java-gen");
}
/**
* @param schema
*/
private static void addNote(Schema schema) {
// 一个实体(类)就关联到数据库中的一张表,此处表名为「Note」(既类名)
Entity note = schema.addEntity("Note");
// 你也可以重新给表命名
// note.setTableName("NODE");
// greenDAO 会自动根据实体类的属性值来创建表字段,并赋予默认值
// 接下来你便可以设置表中的字段:
note.addIdProperty();
note.addStringProperty("text").notNull();
// 与在 Java 中使用驼峰命名法不同,默认数据库中的命名是使用大写和下划线来分割单词的。
// For example, a property called “creationDate” will become a database column “CREATION_DATE”.
note.addStringProperty("comment");
note.addDateProperty("date");
}
/**
* @param schema
*/
private static void addEmp(Schema schema) {
// 一个实体(类)就关联到数据库中的一张表,此处表名为「Note」(既类名)
Entity employee = schema.addEntity("Employee");
// 你也可以重新给表命名
// note.setTableName("NODE");
// greenDAO 会自动根据实体类的属性值来创建表字段,并赋予默认值
// 接下来你便可以设置表中的字段:
employee.addIntProperty("id");
employee.addStringProperty("name");
employee.addIntProperty("age");
employee.addStringProperty("sex");
employee.addStringProperty("jobNumber");
employee.addStringProperty("aliasName");
employee.addStringProperty("other");
employee.addStringProperty("loginName").notNull();
employee.addStringProperty("password").notNull();
}
}
三、编译
1.运行main()函数
如一切正常,你将会在控制台看到如下日志,并且在主工程「java-gen」下会发现生成了DaoMaster、DaoSession、NoteDao、Note、Employee、EmployeeDao共6个类文件。
四、在 ANDROID 工程中进行数据库操作
这里,我们只创建一个 NodeActivity 类,用于测试与讲解 greenDAO 的增、删、查功能。
NoteActivityPersenter.java
/**
* 作者: dyl
* 时间: 2016/12/30 0030 上午 11:45
* 类描述:
*/
public class NoteActivityPersenter extends BasePersenter {
private INoteActivityView iNoteActivityView;
public static final String TAG = "DaoExample";
String textColumn;
String orderBy;
Cursor cursor;
public NoteActivityPersenter(INoteActivityView noteActivityView) {
super(noteActivityView);
this.iNoteActivityView = noteActivityView;
// 获取 NoteDao 对象
getNoteDao();
textColumn = NoteDao.Properties.Text.columnName;
orderBy = textColumn + " COLLATE LOCALIZED ASC";
cursor = TApplication.getInstance().getDb().query(getNoteDao().getTablename(), getNoteDao().getAllColumns(), null, null, null, null, orderBy);
}
public String getTextColumn(){
return textColumn;
}
public String getOrderBy(){
return orderBy;
}
public Cursor getCursor(){
return cursor;
}
public void addNote(String string) {
final DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
String comment = "Added on " + df.format(new Date());
// 插入操作,简单到只要你创建一个 Java 对象
Note note = new Note(null, string, comment, new Date());
getNoteDao().insert(note);
iNoteActivityView.addSuc();
Log.d(TAG, "Inserted new note, ID: " + note.getId());
cursor.requery();
}
public NoteDao getNoteDao() {
return TApplication.getInstance().getDaoSession().getNoteDao();
}
public void search() {
// Query 类代表了一个可以被重复执行的查询
Query query = getNoteDao().queryBuilder()
.where(NoteDao.Properties.Text.eq("Test1"))
.orderAsc(NoteDao.Properties.Date)
.build();
// 查询结果以 List 返回
// List notes = query.list();
// 在 QueryBuilder 类中内置两个 Flag 用于方便输出执行的 SQL 语句与传递参数的值
QueryBuilder.LOG_SQL = true;
QueryBuilder.LOG_VALUES = true;
}
public void closerCursor(){
cursor.close();
}
}
NoteActivity.java
package myhome.goujiawang.doo.ui.activity.main;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import butterknife.Bind;
import butterknife.ButterKnife;
import doo.ddy.greendao.NoteDao;
import myhome.goujiawang.doo.R;
import myhome.goujiawang.doo.base.BaseActivity;
import myhome.goujiawang.doo.mvp.iview.INoteActivityView;
import myhome.goujiawang.doo.mvp.presenter.NoteActivityPersenter;
import myhome.goujiawang.doo.utils.SnackbarUtil;
/**
* 作者: dyl
* 时间: 2017/1/3 0003 下午 2:53
* 类描述:
*/
public class NoteActivity extends BaseActivity implements INoteActivityView ,AdapterView.OnItemClickListener {
private EditText editText;
@Bind(R.id.listview)
ListView listView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note);
ButterKnife.bind(this);
presenter = new NoteActivityPersenter(this);
initViews();
}
@Override
public void findViews() {
super.findViews();
}
@Override
public void initViews() {
super.initViews();
String[] from = {((NoteActivityPersenter)presenter).getTextColumn(), NoteDao.Properties.Comment.columnName};
int[] to = {android.R.id.text1, android.R.id.text2};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, ((NoteActivityPersenter)presenter).getCursor(), from,to);
listView.setAdapter(adapter);
editText = (EditText) findViewById(R.id.editTextNote);
listView.setOnItemClickListener(this);
}
/**
* Button 点击的监听事件
* @param view
*/
public void onMyButtonClick(View view) {
switch (view.getId()) {
case R.id.buttonAdd:
((NoteActivityPersenter)presenter).addNote(editText.getText().toString());
editText.setText("");
break;
case R.id.buttonSearch:
((NoteActivityPersenter)presenter).search();
break;
default:
Log.d(((NoteActivityPersenter)presenter).TAG, "what has gone wrong ?");
break;
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// 删除操作,你可以通过「id」也可以一次性删除所有
((NoteActivityPersenter)presenter).getNoteDao().deleteByKey(id);
// getNoteDao().deleteAll();
Log.d(((NoteActivityPersenter)presenter).TAG, "Deleted note, ID: " + id);
((NoteActivityPersenter)presenter).getCursor().requery();
}
@Override
public void addSuc() {
//这里可以数据保存成功之后的操作
SnackbarUtil.show(listView,"添加成功");
}
@Override
public void deleteSuc() {
//这里可以数据删除成功之后的操作
SnackbarUtil.show(listView,"删除成功");
}
@Override
public void querySuc() {
//这里可以数据查询成功之后的操作
SnackbarUtil.show(listView,"查询成功");
}
@Override
protected void onDestroy() {
super.onDestroy();
((NoteActivityPersenter)presenter).closerCursor();
}
}
activity_note.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/editTextNote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Enter new note"
android:inputType="text"></EditText>
<Button
android:id="@+id/buttonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onMyButtonClick"
android:text="Add"></Button>
<Button
android:id="@+id/buttonSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onMyButtonClick"
android:text="Search"></Button>
</LinearLayout>
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
五、运行结果