上一篇介绍了如何建立类生成工程,现在介绍如何使用。
以下是ExampleDaoGenerator工程代码,做了一些修改
/*
* 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");
第一个参数为版本号,第二个参数为产生的类放在引用工程的那个地方,需要结合下面代码
new DaoGenerator().generateAll(schema, "../../HelloWorld/src-gen");
那么产生的文件将放到
ExampleDaoGenerator工程目录的上上两级目录下的 HelloWorld/src-gen/de/greenrobot/daoexample 目录下。
1、我们在ExampleDaoGenerator工程下,按下键盘的 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到手机,即可。
补充:
在调试过程中,修改了生成工程的类之后发现,重新编译helloworld工程有出现代码异常的情况 代码位于"db = helper.getWritableDatabase();"
该问题暂时未知道什么原因。但有方法解决:在手机端-->在设置界面-->选择对应的应用(对于我来说,是helloworld这个应用)-->清除数据