android greenDAO

三个jar包

 android项目导入

greendao-1.3.7.jar          

     

java项目导入以下两个包

freemarker-2.3.20.jar

greendao-generator-1.3.0.jar


1:新建一个java项目

ExampleDaoGenerator.java

加入要导的包


三个包下载路径 http://download.csdn.net/detail/argonwu/8233303



package com.daotest;

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;

public class ExampleDaoGenerator {
	public static void main(String[] args) throws Exception {
		// first parameter for version, <span></span> second for default
		// generate package
		Schema schema = new Schema(1, "com.trustyapp.baseframe.dao");

		addCompose(schema);
		/*addNote(schema);
		addCustomerOrder(schema);
		addUser(schema);*/
		// set dao class generate package
		schema.setDefaultJavaPackageDao("com.trustyapp.baseframe.dao");
		// keep custom code block
		schema.enableKeepSectionsByDefault();
		new DaoGenerator().generateAll(schema, "../DAOTest/src");
	}

	private static void addNote(Schema schema) {
		Entity note = schema.addEntity("Note");
		note.addIdProperty();
		note.addStringProperty("text").notNull();
		note.addStringProperty("comment");
		note.addStringProperty("date");
	}
	private static void addCompose(Schema schema){
		Entity note = schema.addEntity("Compose");
		note.addIdProperty();
		note.addStringProperty("eid").notNull();
		note.addStringProperty("etitle");
		note.addStringProperty("etype");
		note.addStringProperty("ebody");
		note.addDateProperty("edate");
	}
	private static void addUser(Schema schema) {
		Entity user = schema.addEntity("User");
		user.setTableName("t_user");
		user.addIdProperty();
		user.addStringProperty("account").unique();
		user.addStringProperty("password");
		user.addDateProperty("birthday");
		user.addShortProperty("gender");
		user.addIntProperty("height");
		user.addFloatProperty("weight");
		user.addDateProperty("registerTime");
		user.implementsInterface("Jsonable<User>");
	}

	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);
	}
}

运行后会在

DAOTest
下生成几个dao文件。

到此ok

你可能感兴趣的:(android greenDAO)