GreenDao使用-入門

GreenDao基本使用-入門

GreenDao應該是一個很受歡迎的orm框架,很早之前使用過,如今已是3.0了,這裏作個記錄。
先看下官網上說的優勢:
性能最好(多是android平臺最快的orm框架)
簡單易用
GreenDao體積小
數據庫加密:支持SQLCipher
強大的社區屬性(開源,star多)java

項目地址:https://github.com/greenrobot...android

gradle配置:git

build.gradle(project):github

buildscript {
    repositories {
        jcenter()
        mavenCentral() // add repository
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'
        classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' // add plugin
    }
}

build.gradle(app):數據庫

apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao' // apply plugin


dependencies {
    compile 'org.greenrobot:greendao:3.2.2' // add library
}

greendao {
    schemaVersion 2//greenDAO架構版本,至關於數據庫版本
}

使用:
Note Entity:表明了一個在數據庫中保存的類(表),這個類的屬性至關於數據庫表中列的字段。多線程

@Entity(indexes = {
        @Index(value = "text, date DESC", unique = true)//索引
})
public class Note {

    @Id
    private Long id;

    @NotNull//text非空
    private String text;
    private String comment;
    private Date date;

 
    @Convert(converter = NoteTypeConverter.class, columnType = String.class)
    private NoteType type;

    @Generated(hash = 1272611929)//自動生成
    public Note() {
    }

    public Note(Long id) {
        this.id = id;
    }

    @Generated(hash = 59778150)//自動生成
    public Note(Long id, @NotNull String text, String comment, Date date, NoteType type) {
        this.id = id;
        this.text = text;
        this.comment = comment;
        this.date = date;
        this.type = type;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getText() {
        return text;
    }

    public void setText(@NotNull String text) {
        this.text = text;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public NoteType getType() {
        return type;
    }

    public void setType(NoteType type) {
        this.type = type;
    }
}

NoteType:枚舉類型,自定義了幾種數據類型架構

public enum NoteType {
    TEXT, LIST, PICTURE
}

NoteTypeConverter:能夠將咱們定義的枚舉類型和數據庫的type互相裝換.app

public class NoteTypeConverter implements PropertyConverter<NoteType, String> {

    @Override
    public NoteType convertToEntityProperty(String databaseValue) {
        return NoteType.valueOf(databaseValue);
    }

    @Override
    public String convertToDatabaseValue(NoteType entityProperty) {

        return entityProperty.name();
    }
}

當咱們調用Build > Make project後,greenDao會自動幫咱們生成greenDao類,好比這裏生成的是NoteDao.java,咱們根據這個Dao類來執行數據庫表的各項操做.框架

爲了操做數據庫首先咱們得初始化數據庫,在自定義的Application類中初始化,供整個app使用.maven

public class App extends Application {

    public static final boolean ENCRYPTED = true;//是否加密

    private DaoSession daoSession;

    @Override
    public void onCreate() {
        super.onCreate();

        //建立一個數據庫,DevOpenHelper實際上最終實現了SQLiteOpenHelper,
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "notes-db");
        Database db = ENCRYPTED ? helper.getEncryptedWritableDb("super-secret") : helper.getWritableDb();
        daoSession = new DaoMaster(db).newSession();

    }

    public DaoSession getDaoSession() {
        return daoSession;
    }


    public static App sInstance = new App();

    public static App getInstance() {
        return sInstance;
    }
}

Activity中獲取NoteDao對象:

DaoSession daoSession = ((App)getApplication()).getDaoSession();

    noteDao  = daoSession.getNoteDao();

幾個核心類:
DaoMaster:greenDAO的入口類,持有SQLiteDatabase對象,而且管理Dao類(不是對象),能夠建立或者刪除表,它的兩個內部類OpenHelper和DevOpenHelper都實現了SQLiteOpenHelper。
DaoSession:管理特殊模式下所有可用的Dao對象,咱們可用經過get方法獲取相應的Dao對象(代碼在下面),DaoSession提供了插入,加載,更新,刷新和刪除等針對實體(Entity)的操做。
Daos:Data access objects (DAOs),咱們經過這個類操做數據,相比DaoSession,它提供了更多的持久化方法,好比count, loadAll, and insertInTx。
Entities:持久化數據對象,每一個對象至關於數據庫表中的每條記錄.

核心類初始化流程:

// do this once, for example in your Application class
helper = new DaoMaster.DevOpenHelper(this, "notes-db", null);
db = helper.getWritableDatabase();//獲取到讀寫數據庫
daoMaster = new DaoMaster(db);
daoSession = daoMaster.newSession();
// do this in your activities/fragments to get hold of a DAO
noteDao = daoSession.getNoteDao();//獲取的noteDao供咱們操做數據

常規操做:

插入:

private void addNote() {

        Note note = new Note();
        note.setText(noteText);
        note.setComment(comment);
        note.setDate(new Date());
        note.setType(NoteType.TEXT);
        noteDao.insert(note);

        Log.d("DaoExample", "Inserted new note, ID" + note.getId());
    }

刪除:

noteDao.deleteByKey(id);

更新:

note.setText("This note has changed.");
noteDao.update(note);

查找:

List<User> joes = noteDao.queryBuilder().list();

上面的操做都很簡潔明瞭,查找操做內容其實比較較多,由於包括各類條件查找和多線程查找,下一篇再說(具體代碼可看github上greenDao團隊提供的demo)。

相關文章
相關標籤/搜索