官方网站:http://greenrobot.org/greendao/
建议能看懂E文的童鞋自行阅读官网的文档(documentation)和入门指南( getting started guide)
GreenDao省事的地方主要是在自动生成了操作SQLite的代码,你只需要调用就行了,建议看下他自动生成的代码,写的还是比较规范的。
一、添加依赖:
两种方式:
- 通过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)。
二、生成代码
- 新建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 {
//参数:版本号: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,这个在数据库是主键,并可以自增
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),执行成功后,会看到多了几个类,里面有标记
// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
然后就可以直接调用了。
网友评论