在大型项目中,android的数据库操作不可能再像第二篇文章那样,用最基础的API去实现各方面的操作,那样效率会十分的底下。那么,在android开发中,提拱了androrm,Ormlite,greenDao三个主流框架,博主三生有幸,再开发第一个项目的时候,就被逼着学习使用greenDao这个ORM框架。现在回过头看,greenDao似乎是三种框架中效率最高的,但是对于初学者来说,使用起来非常的不方面,因为官方是纯英文文档,例子也不够详细。博主那个时候真的是在前进中摸索,各种坎坷,网上的博文介绍的都不够详细,不能满足博主的需求,对于数据库的多表设计,网上的例子也都不够详细,而且大部分都是从官方文档直接copy过来,并不是自己写的,更不会有注释。对于新手不好上手。最近这段时间博主比较有时间,于是整理之前的笔记,在数据库专题来详细讲解一下greendao的使用。博主从零开始,一段一段代码自己敲,并配上注释,保证新手容易上手。熟练掌握greendao的使用。
GreenDao官网:http://greendao-orm.com/
GreenDao github下载地址:https://github.com/greenrobot/greenDAO
GreenDao设计目的:最大性能,最快的Android ORM框架,易于使用的API,高度优化,最小内存的损耗
有兴趣的同学可以将完整的工程下载下来学习,看完后绝对可以掌握GreenDao的使用,可以直接拿去自己的项目工程里使用
示例代码下载地址:http://download.csdn.net/detail/victorfreedom/8353631
好了,废话说完了,接下来一步一步的开发一个使用greenDao的android项目工程。
package com.batways.apopo.generator;
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;
/**
* @ClassName: TestCase
* @author victor_freedom ([email protected])
* @createddate 2015-1-12 下午2:17:52
* @Description: TODO
*/
public class TestCase {
// 数据库升级
private static int dbVersion = 1;
private String modelPackage = "com.example.freedomsql.bean";
private Schema schema = new Schema(dbVersion, modelPackage);
public static void main(String[] args) throws Exception {
TestCase testCase = new TestCase();
testCase.init();
testCase.schema.enableKeepSectionsByDefault();
testCase.schema.enableActiveEntitiesByDefault();
new DaoGenerator().generateAll(testCase.schema,
"E:\\mayflygeek\\mayflygeekprojects\\FreedomSql\\src");
}
public void init() {
// 定义一个实体
Entity OrderHeader = schema.addEntity("OrderHeader");
// 实现序列化接口
OrderHeader.implementsSerializable();
// 定义ID主键
OrderHeader.addIdProperty();
// 增加其他字段,这里可以定义很多类型,还可以指定属性
OrderHeader.addStringProperty("orderName").notNull();
//如果不想用上面的定义ID主键,还可以自己这样定义。
// OrderHeader.addLongProperty("orderId").primaryKey().autoincrement();
//后面的实体定义和上面的差不多。就不在详细描述
Entity OrderItem = schema.addEntity("OrderItem");
OrderItem.implementsSerializable();
OrderItem.addIdProperty();
OrderItem.addStringProperty("itemName");
// 用于做多表设计使用
Property orderId = OrderItem.addLongProperty("orderId").getProperty();
Entity Student = schema.addEntity("Student");
Student.implementsSerializable();
Student.addIdProperty();
Student.addStringProperty("studentName");
// 增加一个字段,数据库升级
// Student.addDoubleProperty("results");
Entity Teacher = schema.addEntity("Teacher");
Teacher.implementsSerializable();
Teacher.addIdProperty();
Teacher.addStringProperty("teacherName");
Entity StudentTeacher = schema.addEntity("StudentTeacher");
Property teacherId = StudentTeacher.addLongProperty("teacherId")
.getProperty();
Property studentId = StudentTeacher.addLongProperty("studentId")
.getProperty();
// Entity Grade = schema.addEntity("Grade");
// Grade.implementsSerializable();
// Grade.addIdProperty();
// Grade.addStringProperty("gradeName");
// 树状结构,自身实现1对多
Entity Tree = schema.addEntity("Tree");
Tree.addIdProperty();
Tree.addStringProperty("treeName");
Property parentId = Tree.addLongProperty("parentId").getProperty();
Tree.addToOne(Tree, parentId).setName("parent");
Tree.addToMany(Tree, parentId).setName("children");
// 外键添加,1对多
OrderItem.addToOne(OrderHeader, orderId);
ToMany addToMany = OrderHeader.addToMany(OrderItem, orderId);
addToMany.setName("orderItems");
// greenDao不支持多对多的实现,但是我们可以 自定义实现多对多
StudentTeacher.addToOne(Student, studentId);
StudentTeacher.addToOne(Teacher, teacherId);
Student.addToMany(StudentTeacher, studentId)
.setName("studentsteachers");
Teacher.addToMany(StudentTeacher, teacherId)
.setName("studentsteachers");
}
}
package com.example.freedomsql.bean;
import java.util.List;
import com.example.freedomsql.bean.DaoSession;
import de.greenrobot.dao.DaoException;
// THIS CODE IS GENERATED BY greenDAO, EDIT ONLY INSIDE THE "KEEP"-SECTIONS
// KEEP INCLUDES - put your custom includes here
// KEEP INCLUDES END
/**
* Entity mapped to table ORDER_HEADER.
*/
public class OrderHeader implements java.io.Serializable {
private Long id;
private String orderName;
/** Used to resolve relations */
private transient DaoSession daoSession;
/** Used for active entity operations. */
private transient OrderHeaderDao myDao;
private List orderItems;
// 如果设置了enableKeepSectionsByDefault();enableActiveEntitiesByDefault();这两个属性,那么我们可以再指定的区域内写入自定义代码,方便下次升级的时候不会被覆盖掉
// KEEP FIELDS - put your custom fields here
// KEEP FIELDS END
public OrderHeader() {
}
public OrderHeader(Long id) {
this.id = id;
}
public OrderHeader(Long id, String orderName) {
this.id = id;
this.orderName = orderName;
}
/** called by internal mechanisms, do not call yourself. */
public void __setDaoSession(DaoSession daoSession) {
this.daoSession = daoSession;
myDao = daoSession != null ? daoSession.getOrderHeaderDao() : null;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getOrderName() {
return orderName;
}
public void setOrderName(String orderName) {
this.orderName = orderName;
}
/**
* To-many relationship, resolved on first access (and after reset). Changes
* to to-many relations are not persisted, make changes to the target
* entity.
*/
public List getOrderItems() {
if (orderItems == null) {
if (daoSession == null) {
throw new DaoException("Entity is detached from DAO context");
}
OrderItemDao targetDao = daoSession.getOrderItemDao();
List orderItemsNew = targetDao
._queryOrderHeader_OrderItems(id);
synchronized (this) {
if (orderItems == null) {
orderItems = orderItemsNew;
}
}
}
return orderItems;
}
/**
* Resets a to-many relationship, making the next get call to query for a
* fresh result.
*/
public synchronized void resetOrderItems() {
orderItems = null;
}
/**
* Convenient call for {@link AbstractDao#delete(Object)}. Entity must
* attached to an entity context.
*/
public void delete() {
if (myDao == null) {
throw new DaoException("Entity is detached from DAO context");
}
myDao.delete(this);
}
/**
* Convenient call for {@link AbstractDao#update(Object)}. Entity must
* attached to an entity context.
*/
public void update() {
if (myDao == null) {
throw new DaoException("Entity is detached from DAO context");
}
myDao.update(this);
}
/**
* Convenient call for {@link AbstractDao#refresh(Object)}. Entity must
* attached to an entity context.
*/
public void refresh() {
if (myDao == null) {
throw new DaoException("Entity is detached from DAO context");
}
myDao.refresh(this);
}
// KEEP METHODS - put your custom methods here
// KEEP METHODS END
}
package com.example.freedomsql.bean;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.DaoConfig;
import com.example.freedomsql.bean.OrderHeader;
// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/**
* DAO for table ORDER_HEADER.
*/
public class OrderHeaderDao extends AbstractDao {
public static final String TABLENAME = "ORDER_HEADER";
/**
* Properties of entity OrderHeader.
* Can be used for QueryBuilder and for referencing column names.
*/
public static class Properties {
public final static Property Id = new Property(0, Long.class, "id", true, "_id");
public final static Property OrderName = new Property(1, String.class, "orderName", false, "ORDER_NAME");
};
private DaoSession daoSession;
public OrderHeaderDao(DaoConfig config) {
super(config);
}
public OrderHeaderDao(DaoConfig config, DaoSession daoSession) {
super(config, daoSession);
this.daoSession = daoSession;
}
/** Creates the underlying database table. */
public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
String constraint = ifNotExists? "IF NOT EXISTS ": "";
db.execSQL("CREATE TABLE " + constraint + "'ORDER_HEADER' (" + //
"'_id' INTEGER PRIMARY KEY ," + // 0: id
"'ORDER_NAME' TEXT);"); // 1: orderName
}
/** Drops the underlying database table. */
public static void dropTable(SQLiteDatabase db, boolean ifExists) {
String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "'ORDER_HEADER'";
db.execSQL(sql);
}
/** @inheritdoc */
@Override
protected void bindValues(SQLiteStatement stmt, OrderHeader entity) {
stmt.clearBindings();
Long id = entity.getId();
if (id != null) {
stmt.bindLong(1, id);
}
String orderName = entity.getOrderName();
if (orderName != null) {
stmt.bindString(2, orderName);
}
}
@Override
protected void attachEntity(OrderHeader entity) {
super.attachEntity(entity);
entity.__setDaoSession(daoSession);
}
/** @inheritdoc */
@Override
public Long readKey(Cursor cursor, int offset) {
return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
}
/** @inheritdoc */
@Override
public OrderHeader readEntity(Cursor cursor, int offset) {
OrderHeader entity = new OrderHeader( //
cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1) // orderName
);
return entity;
}
/** @inheritdoc */
@Override
public void readEntity(Cursor cursor, OrderHeader entity, int offset) {
entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
entity.setOrderName(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
}
/** @inheritdoc */
@Override
protected Long updateKeyAfterInsert(OrderHeader entity, long rowId) {
entity.setId(rowId);
return rowId;
}
/** @inheritdoc */
@Override
public Long getKey(OrderHeader entity) {
if(entity != null) {
return entity.getId();
} else {
return null;
}
}
/** @inheritdoc */
@Override
protected boolean isEntityUpdateable() {
return true;
}
}
我们可以看到对应的表生成语句和字段绑定等都在这个类里面。其实这个类和之前哪篇文件说的Dao类一样,是操作数据库用的。增删改查全部靠这个类来实行。
package com.example.freedomsql.bean;
import android.content.Context;
import com.example.freedomsql.utils.Config;
/**
* @ClassName: GreenDao
* @author victor_freedom ([email protected])
* @createddate 2015-1-12 下午3:21:02
* @Description: TODO
*/
public class GreenDao {
private static DaoMaster daoMaster;
private static DaoSession daoSession;
/**
* 获取DaoMaster实例
*
* @param context
* @return
*/
public static DaoMaster getDaoMaster(Context context) {
if (daoMaster == null) {
DaoMaster.OpenHelper helper = new DaoMaster.DevOpenHelper(context,
Config.DB_NAME, null);
daoMaster = new DaoMaster(helper.getWritableDatabase());
}
return daoMaster;
}
/**
* 获取DaoSession实例
*
* @param context
* @return
*/
public static DaoSession getDaoSession(Context context) {
if (daoSession == null) {
if (daoMaster == null) {
daoMaster = getDaoMaster(context);
}
daoSession = daoMaster.newSession();
}
return daoSession;
}
}
package com.example.freedomsql.service;
import com.example.freedomsql.bean.OrderHeader;
import com.example.freedomsql.bean.OrderItem;
import com.example.freedomsql.bean.Student;
import com.example.freedomsql.bean.Teacher;
public interface IOrderHeaderService {
/**
* @Title: createOrder
* @Description: 创建一个订单
* @param order
* @return
* @throws
*/
public OrderHeader createOrder(OrderHeader order);
/**
* @Title: updateOrder
* @Description: 更新一个订单
* @param orderHeader
* @return
* @throws
*/
public OrderHeader updateOrder(OrderHeader orderHeader);
/**
* @Title: findOrderByName
* @Description: 根据名称查找订单
* @param orderName
* @return
* @throws
*/
public OrderHeader findOrderByName(String orderName);
/**
* @Title: findOrderById
* @Description: 根据主键ID查找订单
* @param orderId
* @return
* @throws
*/
public OrderHeader findOrderById(long orderId);
/**
* @Title: findOrderItemById
* @Description:根据主键ID查找订单明细
* @param orderItemId
* @return
* @throws
*/
public OrderItem findOrderItemById(long orderItemId);
/**
* @Title: findOrderItemByName
* @Description: 根据名称查找订单明细
* @param orderItemName
* @return
* @throws
*/
public OrderItem findOrderItemByName(String orderItemName);
/**
* @Title: createOrderItem
* @Description: 创建订单明细
* @param orderHeader
* @param name
* @return
* @throws
*/
public OrderItem createOrderItem(OrderHeader orderHeader, String name);
}
接下来写实现类:
package com.example.freedomsql.service.impl;
import android.content.Context;
import com.example.freedomsql.bean.DaoSession;
import com.example.freedomsql.bean.GreenDao;
import com.example.freedomsql.bean.OrderHeader;
import com.example.freedomsql.bean.OrderHeaderDao;
import com.example.freedomsql.bean.OrderItem;
import com.example.freedomsql.bean.OrderItemDao;
import com.example.freedomsql.service.IOrderHeaderService;
/**
* @ClassName: OrderHeaderService
* @author victor_freedom ([email protected])
* @createddate 2015-1-12 下午3:26:41
* @Description: TODO
*/
public class OrderHeaderService implements IOrderHeaderService {
private static DaoSession daoSession;
private static OrderHeaderService service;
private OrderHeaderDao orderHeaderDao;
private OrderItemDao orderItemDao;
private OrderHeaderService(OrderHeaderDao orderHeaderDao,
OrderItemDao orderItemDao) {
this.orderHeaderDao = orderHeaderDao;
this.orderItemDao = orderItemDao;
}
/**
* @param context
* @return
*/
public static OrderHeaderService getService(Context context) {
if (service == null) {
daoSession = GreenDao.getDaoSession(context);
service = new OrderHeaderService(daoSession.getOrderHeaderDao(),
daoSession.getOrderItemDao());
}
return service;
}
@Override
public OrderHeader createOrder(OrderHeader order) {
return orderHeaderDao.loadByRowId(orderHeaderDao.insert(order));
}
@Override
public OrderHeader updateOrder(OrderHeader orderHeader) {
orderHeaderDao.update(orderHeader);
return orderHeader;
}
@Override
public OrderHeader findOrderByName(String orderName) {
OrderHeader orderHeader = orderHeaderDao.queryBuilder()
.where(OrderHeaderDao.Properties.OrderName.eq(orderName))
.unique();
return orderHeader;
}
@Override
public OrderHeader findOrderById(long orderId) {
return orderHeaderDao.load(orderId);
}
@Override
public OrderItem findOrderItemById(long orderItemId) {
return orderItemDao.load(orderItemId);
}
@Override
public OrderItem findOrderItemByName(String orderItemName) {
return orderItemDao.queryBuilder()
.where(OrderItemDao.Properties.ItemName.eq(orderItemName))
.unique();
}
@Override
public OrderItem createOrderItem(OrderHeader orderHeader, String name) {
OrderItem orderItem = new OrderItem();
orderItem.setItemName(name);
orderItem.setOrderHeader(orderHeader);
return orderItemDao.load(orderItemDao.insert(orderItem));
}
}
package com.example.freedomsql;
import android.app.Application;
import com.example.freedomsql.service.IClassService;
import com.example.freedomsql.service.IOrderHeaderService;
import com.example.freedomsql.service.impl.ClassService;
import com.example.freedomsql.service.impl.OrderHeaderService;
/**
* @ClassName: FreedomApplication
* @author victor_freedom ([email protected])
* @createddate 2015-1-12 下午3:39:56
* @Description: TODO
*/
public class FreedomApplication extends Application {
public IClassService classService;
public IOrderHeaderService orderHeaderService;
@Override
public void onCreate() {
super.onCreate();
classService = ClassService.getService(getApplicationContext());
orderHeaderService = OrderHeaderService
.getService(getApplicationContext());
}
}
2、主Activity的编写
package com.example.freedomsql;
import android.app.Activity;
import android.os.Bundle;
import com.example.freedomsql.bean.OrderHeader;
import com.example.freedomsql.bean.Student;
import com.example.freedomsql.bean.StudentTeacher;
import com.example.freedomsql.bean.Teacher;
import com.example.freedomsql.service.IClassService;
import com.example.freedomsql.service.IOrderHeaderService;
public class MainActivity extends Activity {
private IClassService classService;
private IOrderHeaderService orderHeaderService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
classService = ((FreedomApplication) getApplication()).classService;
orderHeaderService = ((FreedomApplication) getApplication()).orderHeaderService;
initGreenDaoDB();
}
private void initGreenDaoDB() {
Teacher t1 = new Teacher();
t1.setTeacherName("freedom");
// t1.setTeacherName("freedom2");
classService.createTeacher(t1);
Teacher t2 = new Teacher();
t2.setTeacherName("freedom1");
// t2.setTeacherName("freedom3");
Student t3 = new Student();
t3.setStudentName("victor");
// t3.setStudentName("victor2");
Student t4 = new Student();
t4.setStudentName("victor1");
// t4.setStudentName("victor3");
classService.createTeacher(t1);
classService.createTeacher(t2);
classService.createStudent(t3);
classService.createStudent(t4);
StudentTeacher st1 = new StudentTeacher(t1.getId(), t3.getId());
StudentTeacher st2 = new StudentTeacher(t1.getId(), t4.getId());
StudentTeacher st3 = new StudentTeacher(t2.getId(), t3.getId());
StudentTeacher st4 = new StudentTeacher(t2.getId(), t4.getId());
classService.createStudentTeacher(st1);
classService.createStudentTeacher(st2);
classService.createStudentTeacher(st3);
classService.createStudentTeacher(st4);
OrderHeader order = new OrderHeader();
order.setOrderName("订单1");
// order.setOrderName("订单3");
OrderHeader order1 = new OrderHeader();
order1.setOrderName("订单2");
// order1.setOrderName("订单4");
orderHeaderService.createOrder(order);
orderHeaderService.createOrder(order1);
orderHeaderService.createOrderItem(order1, "明细1");
orderHeaderService.createOrderItem(order1, "明细2");
// orderHeaderService.createOrderItem(order1, "明细3");
// orderHeaderService.createOrderItem(order1, "明细4");
}
}
private static int dbVersion = 2;
// 增加一个字段,数据库升级
Student.addDoubleProperty("results");
Entity Grade = schema.addEntity("Grade");
Grade.implementsSerializable();
Grade.addIdProperty();
Grade.addStringProperty("gradeName");
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.i("greenDAO", "Upgrading schema from version " + oldVersion
+ " to " + newVersion + " by dropping all tables");
//正常情况下,GreenDao默认升级的时候,将所有的表删除后再建,所以我们需要在这里处理
// dropAllTables(db, true);
// onCreate(db);
if (oldVersion == 1 && newVersion == 2) {
// 增加一个实体表
GradeDao.createTable(db, false);
// 修改Student表
db.execSQL("ALTER TABLE 'STUDENT' ADD 'RESULTS' REAL");
}
}
package com.example.freedomsql;
import android.app.Activity;
import android.os.Bundle;
import com.example.freedomsql.bean.OrderHeader;
import com.example.freedomsql.bean.Student;
import com.example.freedomsql.bean.StudentTeacher;
import com.example.freedomsql.bean.Teacher;
import com.example.freedomsql.service.IClassService;
import com.example.freedomsql.service.IOrderHeaderService;
public class MainActivity extends Activity {
private IClassService classService;
private IOrderHeaderService orderHeaderService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
classService = ((FreedomApplication) getApplication()).classService;
orderHeaderService = ((FreedomApplication) getApplication()).orderHeaderService;
initGreenDaoDB();
}
private void initGreenDaoDB() {
Teacher t1 = new Teacher();
// t1.setTeacherName("freedom");
t1.setTeacherName("freedom2");
classService.createTeacher(t1);
Teacher t2 = new Teacher();
// t2.setTeacherName("freedom1");
t2.setTeacherName("freedom3");
Student t3 = new Student();
// t3.setStudentName("victor");
t3.setStudentName("victor2");
Student t4 = new Student();
// t4.setStudentName("victor1");
t4.setStudentName("victor3");
classService.createTeacher(t1);
classService.createTeacher(t2);
classService.createStudent(t3);
classService.createStudent(t4);
StudentTeacher st1 = new StudentTeacher(t1.getId(), t3.getId());
StudentTeacher st2 = new StudentTeacher(t1.getId(), t4.getId());
StudentTeacher st3 = new StudentTeacher(t2.getId(), t3.getId());
StudentTeacher st4 = new StudentTeacher(t2.getId(), t4.getId());
classService.createStudentTeacher(st1);
classService.createStudentTeacher(st2);
classService.createStudentTeacher(st3);
classService.createStudentTeacher(st4);
OrderHeader order = new OrderHeader();
// order.setOrderName("订单1");
order.setOrderName("订单3");
OrderHeader order1 = new OrderHeader();
// order1.setOrderName("订单2");
order1.setOrderName("订单4");
orderHeaderService.createOrder(order);
orderHeaderService.createOrder(order1);
// orderHeaderService.createOrderItem(order1, "明细1");
// orderHeaderService.createOrderItem(order1, "明细2");
orderHeaderService.createOrderItem(order1, "明细3");
orderHeaderService.createOrderItem(order1, "明细4");
}
}