官方網(wǎng)站:http://greenrobot.org/greendao/
建議能看懂E文的童鞋自行閱讀官網(wǎng)的文檔(documentation)和入門指南( getting started guide)
GreenDao省事的地方主要是在自動(dòng)生成了操作SQLite的代碼南誊,你只需要調(diào)用就行了,建議看下他自動(dòng)生成的代碼蜜托,寫的還是比較規(guī)范的抄囚。
一、添加依賴:
兩種方式:
- 通過(guò)Android Studio 的Maven Central直接搜索添加
(1).打開Project Structure, 選擇Dependencies選項(xiàng)卡橄务,添加選擇Library Dependencies幔托。
(2). 搜索greendao
,選擇de.greenrobot:greendao-generator:2.1.0
確定后露氮,等Gradle同步完蟀架。
(3). 同樣的步驟添加de.greenrobot:greendao:2.1.0
。
- 通過(guò)手動(dòng)修改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)棠涮。
二谬哀、生成代碼
- 新建DaoGenerator類,官方給了個(gè)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ù):版本號(hào):1000,包名:de.greenrobot.daoexample
Schema schema = new Schema(1000, "de.greenrobot.daoexample");
addNote(schema);
addCustomerOrder(schema);
//生成的Dao類的目的地址故爵,在Windows下Android Studio運(yùn)行該類玻粪,
//會(huì)在生成的jar下找這個(gè)相對(duì)路徑(目測(cè)是找不到的隅津,最好還是用絕對(duì)路徑),建議用絕對(duì)路徑劲室。
//例如:
//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伦仍,這個(gè)在數(shù)據(jù)庫(kù)是主鍵,并可以自增
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);
}
運(yùn)行這個(gè)類(注意是單獨(dú)運(yùn)行這個(gè)類,在Android Studio里運(yùn)行類型是Application),執(zhí)行成功后喉磁,會(huì)看到多了幾個(gè)類谓苟,里面有標(biāo)記
// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
然后就可以直接調(diào)用了。