上一篇介紹了如何建立類產生工程,現在介紹如何使用。
以下是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;@Overrideprotected 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這個應用)-->清除資料