</pre> 因為看了開源項目中使用greenDAO的ORM架構。今天總算搞清楚是如何自動產生entity,dao等java檔案的了。<p></p><p><span style="white-space:pre"></span>準備工作:需要到greenDAO官網下載jar包,地址為:http://greendao-orm.com/download-and-source/。總共有兩個jar包。<img src="https://img-blog.csdn.net/20150422232323268?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaHVuYW5fbGlqaWVf/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /></p><p><span style="white-space:pre"></span>當然還需要另外一個包:freemarker-2.3.19.jar。</p><p></p><p><span style="white-space:pre"></span>有了三個jar包以後,就可以新建立一個java工程,產生代碼了。</p><p></p><p></p><pre name="code" class="java">package com.test.example;import java.io.IOException;import de.greenrobot.daogenerator.DaoGenerator;import de.greenrobot.daogenerator.Entity;import de.greenrobot.daogenerator.Schema;public class TestDemo {/** * @param args */public static void main(String[] args) {
<span style="white-space:pre"></span> // 第一個參數資料庫版本,第二個參數表示產生java的包名。 Schema schema = new Schema(3, "com.example.greendao.mytest.bean"); addNote(schema); try {
<span style="white-space:pre"></span>// 第二個參數表示組建檔案存放路徑。new DaoGenerator().generateAll(schema, "../src");} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();} System.out.println("ok");} private static void addNote(Schema schema) { Entity note = schema.addEntity("Note"); note.addIdProperty(); note.addStringProperty("text").notNull(); note.addStringProperty("comment"); note.addDateProperty("date"); }}
代碼下載地址: 點擊開啟連結