简介
GreenDAO是一个轻量、快速的ORM解决方案,它能高效地将对象映射到SQLite数据库,目前升级到了3.1版本,相较于之前的版本来说,易用性大幅度增加了,不需要一些麻烦的配置,详情参考官方文档 http://greenrobot.org/greendao/features/
1. 导入依赖
在project的build.gradle中插入:
    buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.greenrobot:greendao-gradle-plugin:3.1.0'
    }
}
在module的build.gradle中插入:
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.greenrobot:greendao-gradle-plugin:3.1.0'
    }
}
2. 编写需要存储对象的实体类
/**
 * Created by GongCheng on 2016/8/20.
 * 联系人数据库实体类
 */
@Entity
public class Contact {
    @Id
    private Long id; //注意Id必须是包装类
    @NonNull
    private String name;
    private String number;
 }
3. 编译项目
直接Rebuild,发现生成了下面这些文件
而刚才写的实体类也变成下面这样
**
 * Created by GongCheng on 2016/8/20.
 * 联系人数据库实体类
 */
@Entity
public class Contact {
    @Id
    private Long id;
    @NonNull
    private String name;
    private String number;
    public String getNumber() {
        return this.number;
    }
    public void setNumber(String number) {
        this.number = number;
    }
    public String getName() {
        return this.name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Long getId() {
        return this.id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    @Generated(hash = 212132701)
    public Contact(Long id, @NonNull String name, String number) {
        this.id = id;
        this.name = name;
        this.number = number;
    }
    @Generated(hash = 672515148)
    public Contact() {
    }
}
这些都是通过插件生成的,不要随意去改动
4.初始化
初始化工作一般放在项目的Application里面执行
/**
 * Created by GongCheng on 2016/8/5.
 */
public class MainApplication extends Application {
    private static  MainApplication INSTANCE ;
    private DaoSession daoSession;
    public static MainApplication getInstance(){
        return INSTANCE;
    }
    @Override
    public void onCreate() {
        super.onCreate();
        INSTANCE = this;
        //初始化SharedPreferences
        SharedPreferencesUtils.init();
        //初始化GreenDao
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this,"contacts-db");
        Database db = helper.getWritableDb();
        daoSession = new DaoMaster(db).newSession();
    }
    //得到DaoSession
    public DaoSession getDaoSession() {
        return daoSession;
    }
}
5.正式使用
//Contact的查询器
private Query<Contact> contactQuery;
//ContactDao
private ContactDao contactDao;
/**
 * 初始化Dao
 */
private void initDao() {
    //得到ContactDao
    DaoSession daoSession= MainApplication.getInstance().getDaoSession();
    contactDao= daoSession.getContactDao();
    //查询所有联系人(通过姓名)
    contactQuery = contactDao.queryBuilder().orderAsc(ContactDao.Properties.Name).build();
    updateContacts();
}
具体操作
//查询
List<Note> notes = notesQuery.list();
//添加
Contact contact = new Contact(null,name,number);
contactDao.insert(contact);
//删除
contactDao.deleteByKey(contactId);
还有一些其他操作没有一一列举了,GreenDao也提供了对RxJava的支持










网友评论