GreenDao官方教程擴展版(1)

官方網(wǎng)站:http://greenrobot.org/greendao/

建議能看懂E文的童鞋自行閱讀官網(wǎng)的文檔(documentation)和入門指南( getting started guide

GreenDao省事的地方主要是在自動生成了操作SQLite的代碼,你只需要調(diào)用就行了,建議看下他自動生成的代碼,寫的還是比較規(guī)范的。

一、添加依賴:

兩種方式:
  1. 通過Android Studio 的Maven Central直接搜索添加
    (1).打開Project Structure, 選擇Dependencies選項卡,添加選擇Library Dependencies。
    (2). 搜索 greendao,選擇de.greenrobot:greendao-generator:2.1.0確定后,等Gradle同步完。
    (3). 同樣的步驟添加de.greenrobot:greendao:2.1.0。
  • 通過手動修改build.gradle文件添加
    (1). 打開需要添加module的build.gradle文件。
    (2). dependencies最后加上
    <code>compile 'de.greenrobot:greendao:2.1.0'
    compile 'de.greenrobot:greendao-generator:2.1.0'</code>
    (3). 同步Gradle(sync Project with Gradle files)。

二、生成代碼

  1. 新建DaoGenerator類,官方給了個Example.


/*

 * Copyright (C) 2011 Markus Junginger, greenrobot (http://greenrobot.de)

 *

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 * http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */

package de.greenrobot.daogenerator.gentest;

import de.greenrobot.daogenerator.DaoGenerator;

import de.greenrobot.daogenerator.Entity;

import de.greenrobot.daogenerator.Property;

import de.greenrobot.daogenerator.Schema;

import de.greenrobot.daogenerator.ToMany;

/**

* Generates entities and DAOs for the example project DaoExample.

* 

* Run it as a Java application (not Android).

* 

* @author Markus

*/

public class ExampleDaoGenerator {

public static void main(String[] args) throws Exception {
//參數(shù):版本號:1000,包名:de.greenrobot.daoexample
Schema schema = new Schema(1000, "de.greenrobot.daoexample");

addNote(schema);

addCustomerOrder(schema);
//生成的Dao類的目的地址,在Windows下Android Studio運行該類,
//會在生成的jar下找這個相對路徑(目測是找不到的,最好還是用絕對路徑),建議用絕對路徑。
//例如:
//F:\\workshop\\Project\\DaoExample\\app\\src\\main\\java
new DaoGenerator().generateAll(schema, "../DaoExample/src/main/java");

}
//添加Entity:Note
private static void addNote(Schema schema) {

Entity note = schema.addEntity("Note");
//添加Note本身的屬性Id,這個在數(shù)據(jù)庫是主鍵,并可以自增
note.addIdProperty();
//添加Note本身的屬性text,類型是String,不可為空
note.addStringProperty("text").notNull();

note.addStringProperty("comment");
//Date類型
note.addDateProperty("date");

}

private static void addCustomerOrder(Schema schema) {

Entity customer = schema.addEntity("Customer");

customer.addIdProperty();

customer.addStringProperty("name").notNull();

Entity order = schema.addEntity("Order");

order.setTableName("ORDERS"); // "ORDER" is a reserved keyword

order.addIdProperty();

Property orderDate = order.addDateProperty("date").getProperty();

Property customerId = order.addLongProperty("customerId").notNull().getProperty();

order.addToOne(customer, customerId);

ToMany customerToOrders = customer.addToMany(order, customerId);

customerToOrders.setName("orders");

customerToOrders.orderAsc(orderDate);

}

運行這個類(注意是單獨運行這個類,在Android Studio里運行類型是Application),執(zhí)行成功后,會看到多了幾個類,里面有標(biāo)記
// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
然后就可以直接調(diào)用了。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容