关于如何建立类生成一个演示project。今天介绍如何使用。
这是ExampleDaoGeneratorproject代码,做了一些改动
/*
* 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 {
//该方法第一个參数用来更新数据库版本。第二个參数为要生成的DAO类所在包路径。
Schema schema = new Schema(1, "de.greenrobot.daoexample"); //然后进行建表
addNote(schema);
//addCustomerOrder(schema); //设置要生成DAO文件的目标工程的项目路径,当中src-gen这个文件夹名须要在执行前手动创建。否则会报错
new DaoGenerator().generateAll(schema, "../../HelloWorld/src-gen");
} private static void addNote(Schema schema) {
/**
* 在使用greenDAO时,一个实体类仅仅能相应一个表,眼下没法做到一个表相应多个实体类。或者多个表共用一种对象类型。 * 兴许的升级也不会针对这一点进行扩展。
* */
Entity note = schema.addEntity("Note");//创建表
//note.addIdProperty(); //添加ID列
note.addIdProperty().primaryKey().autoincrement();//设置一个自增长ID列为主键:
note.addStringProperty("text").notNull();//创建非空的列
note.addStringProperty("comment").unique();//创建唯一的
//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);
} }
上面代码主要有两个
Schema schema = new Schema(1, "de.greenrobot.daoexample");
第一个參数为版本,第二个參数为产生的类放在引用project的那个地方,须要结合以下代码
new DaoGenerator().generateAll(schema, "../../HelloWorld/src-gen");
那么产生的文件将放到
ExampleDaoGeneratorproject文件夹的上上两级文件夹下的 HelloWorld/src-gen/de/greenrobot/daoexample 文件夹下。
1、我们在ExampleDaoGeneratorproject下,按下键盘的 ctrl+F11 则会自己主动生成四个java类放到HelloWorld/src-gen/de/greenrobot/daoexample文件夹以下
2、把这4个类依赖的相关类放到一起。依赖的类直接把 "greenDAO-master\DaoCore\src\de"文件夹下的内容放到一起4个类一起。
3、将src-gen文件夹设置为编译文件夹
4、依据生成类添加相关代码,这里仅仅贴相关用到的代码
private DaoMaster daoMaster;
private DaoSession daoSession;
private NoteDao noteDao; private Cursor cursor;
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); DevOpenHelper helper = new DaoMaster.DevOpenHelper(MainActivity.this, "Note", null);
db = helper.getWritableDatabase();
daoMaster = new DaoMaster(db);
daoSession = daoMaster.newSession();
noteDao = daoSession.getNoteDao();
}
final Button btn6 = (Button) findViewById(R.id.button6);
btn6.setOnClickListener(new OnClickListener() {
public void onClick(View v) { Toast.makeText(MainActivity.this, "长度 :" + noteDao.count(),
Toast.LENGTH_SHORT).show();
Note note = new Note(null, "noteText1", "comment1");
try {
noteDao.insert(note); } catch (Exception e) {
Toast.makeText(MainActivity.this, "数据反复 :" + noteDao.count(),
Toast.LENGTH_SHORT).show();
}
5、按ctrl+shift+o自己主动补齐相关类
6、下载APK到手机,就可以。
补充:
在调试过程中,改动了生成project的类之后发现,又一次编译helloworldproject有出现代码异常的情况 代码位于"db = helper.getWritableDatabase();"
该问题临时未知道什么原因。但有方法解决:在手机端-->在设置界面-->选择相应的应用程序(为了我,它是helloworld此应用程序)-->清除数据