安卓ORM框架greenDAO

greenDAO   ormlite的性能对比:


上面可以看到,greeendao的insert和update效率要比ormlite快两倍左右,load更是夸张到4倍多。


    我们可以在官网(http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22de.greenrobot%22%20AND%20a%3A%22greendao-generator%22)上直接下来,也可去github项目主页上下载源码。

下面新建一个java工程,注意是java工程不是android工程,工程名GreenDaoExample。并手动在工程中新建src-gen文件夹。导入freemaker.jar和greendao-generator.jar,加入到build path。建一个如下的类:

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
    {
        Schema schema = new Schema(3, "com.young.do");
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
        addNote(schema);
        addCustomerOrder(schema);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
        new DaoGenerator().generateAll(schema, "../DaoExample/src-gen");
    }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
    private static void addNote(Schema schema)
    {
        Entity note = schema.addEntity("Note");
        note.addIdProperty();
        note.addStringProperty("text").notNull();
        note.addStringProperty("comment");
        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);
    }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
}

直接运行,代码生成

安卓ORM框架greenDAO_第1张图片



自动生成model和dao,随便看一个model类:

package com.young.do;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT. Enable "keep" sections if you want to edit. 
/**
 * Entity mapped to table "NOTE".
 */
public class Note {

    private Long id;
    /** Not-null value. */
    private String text;
    private String comment;
    private java.util.Date date;

    public Note() {
    }

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

    public Note(Long id, String text, String comment, java.util.Date date) {
        this.id = id;
        this.text = text;
        this.comment = comment;
        this.date = date;
    }

    public Long getId() {
        return id;
    }

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

    /** Not-null value. */
    public String getText() {
        return text;
    }

    /** Not-null value; ensure this value is available before it is saved to the database. */
    public void setText(String text) {
        this.text = text;
    }

    public String getComment() {
        return comment;
    }

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

    public java.util.Date getDate() {
        return date;
    }

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

}





在安卓中使用greenDAO 

 

  首先加入greendao.jar包,并把java工程中生成的类复制到安卓工程中。然后就可以进行增删改查了,具体代码如下。

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		DevOpenHelper helper = new DaoMaster.DevOpenHelper(this,
				"greendao_test.db", null);
		SQLiteDatabase db = helper.getWritableDatabase();
		DaoMaster daoMaster = new DaoMaster(db);
		DaoSession daoSession = daoMaster.newSession();
		final NoteDao noteDao = daoSession.getNoteDao();

		// noteDao.insertInTx(new ArrayList<Note>(), true);

		findViewById(R.id.but).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Note note = new Note();
				note.setDate(new Date());
				note.setId(System.currentTimeMillis());
				note.setText("ttteesstt");
				noteDao.insert(note);
			}
		});

		findViewById(R.id.get).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				List<Note> l = noteDao.loadAll();
				StringBuilder builder = new StringBuilder();
				for (Note note : l) {
					builder.append(note.getId()).append("  ")
							.append(note.getText()).append("  ")
							.append(note.getDate()).append("\n");
				}
				Toast.makeText(getApplicationContext(), builder, 1).show();
			}
		});
	}

}

由于NoteDao继承自AbstractDao,所以可以直接调用各种增删改查方法


你可能感兴趣的:(安卓,orm,greenDAO)