1: 使用的架包:greenDAO-generator.jar ,freemarker.jar,greenDAO.jar
2: 在同一个classpath 路径下建立一个javaProject
3: 引入架包 greenDAO-generator.jar,freemarker.jar
4:编写bean类,dao类,DaoMaster,DaoSession 生成器 代码如下:
package com.guanzhi.daogenerator;
import java.io.IOException;
import de.greenrobot.daogenerator.Entity;
import de.greenrobot.daogenerator.Schema;
public class MyDaoGenerator {
public static void main(String[] args) {
Schema schema = new Schema(3, "com.example.db");
addNote(schema);
try {
new de.greenrobot.daogenerator.DaoGenerator().generateAll(schema,
"../GrenDaoTest/src-gen");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void addNote(Schema schema) {
Entity note = schema.addEntity("Person");
note.addIdProperty().autoincrement();
note.addStringProperty("name").notNull();
note.addStringProperty("age").notNull();
note.addStringProperty("color").notNull();
note.addDateProperty("date");
}
}
5:运行上面的工程
6:如果报src错误,请在指定工程下新建src-gen目录
7: 生成的结构如下图:
8:编写BaseApplication实例化,DaoMaster,DaoSession
package com.example.application; import com.example.db.DaoMaster; import com.example.db.DaoSession; import com.example.db.DaoMaster.DevOpenHelper; import android.app.Application; import android.content.Context; import android.database.sqlite.SQLiteDatabase; public class BaseApplication extends Application { private static BaseApplication mInstance; private static final String DB_NAME = "pp-dbs"; private static DaoMaster mDaoMaster; private static DaoSession mDaoSession; @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); if (mInstance == null) mInstance = this; } public static DaoMaster getDaoMaster(Context context) { if (mDaoMaster == null) { DevOpenHelper openHelper = new DevOpenHelper(context, DB_NAME, null); SQLiteDatabase db = openHelper.getWritableDatabase(); mDaoMaster = new DaoMaster(db); } return mDaoMaster; }; public static DaoSession getDaoSession(Context context) { if (mDaoSession == null) { if (mDaoMaster == null) { getDaoMaster(context); } mDaoSession = mDaoMaster.newSession(); } return mDaoSession; } }
9:数据操作工具类; package com.example.db; import java.util.List; import com.example.application.BaseApplication; import android.content.Context; public class DbService { private final String TAG = DbService. class .getSimpleName(); private static DbService mInstance; private static Context mContext; private DaoSession mDaoSession ; public PersonDao mPersonDao ; private DbService() { } public static DbService getInstance(Context context) { if (mInstance == null) { mInstance = new DbService(); if (mContext == null) { mContext = context.getApplicationContext(); } mInstance .mDaoSession = BaseApplication.getDaoSession(context); mInstance .mPersonDao = mInstance .mDaoSession .getPersonDao(); } return mInstance ; } /** * 获取所有数据 * * @return */ public List<Person> getAllPerson() { return mPersonDao .loadAll(); } /** * 插入数据 * * @param pp * @return */ public long savePerson(Person pp) { return mPersonDao .insert(pp); } /** * 删除数据 * * @param pp */ public void deletePerson(Person pp) { mPersonDao .delete(pp); } public void deletePersonDetail( long id){ mPersonDao .deleteByKey(id); } public void deleteAllData(){ mPersonDao .deleteAll(); } /** * 更新数据 * * @param pp */ public void updatePerson(Person pp) { mPersonDao .update(pp); } /** * 根据id 获取数据 * * @param id * @return */ public Person loadPeron( long id) { return mPersonDao .load(id); } /** * 根据条件查询 */ public List<Person> queryPerson(String where, String... params) { return mPersonDao .queryRaw(where, params); } } 10:测试 package com.example.grendaotest; import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.Random; import com.example.db.DaoMaster; import com.example.db.DaoSession; import com.example.db.DaoMaster.DevOpenHelper; import com.example.db.DbService; import com.example.db.Person; import com.example.db.PersonDao; import com.example.db.PersonDao.Properties; import android.os.Bundle; import android.app.Activity; import android.database.sqlite.SQLiteDatabase; import android.util.Log; import android.view.Menu; public class MainActivity extends Activity { DbService mDbService; private final String TAG = MainActivity.class.getSimpleName(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mDbService = DbService.getInstance(this); // Log.e(TAG, "已经插入数据个数:" + createPersonData().size()); mDbService.deleteAllData(); createPersonData(); // deletePer(); getDataWhere() ; } /** * 删除年龄为100的person */ public void deletePer() { for (Iterator<Person> oo = getData().iterator(); oo.hasNext();) { Person pp = oo.next(); if (pp.getAge().equals("100")) { mDbService.deletePerson(pp); } } getData(); } public List<Person> createPersonData() { for (int i = 0; i < 10; i++) { Person p = new Person(); p.setAge(99 + i + ""); p.setColor("红色"+i); p.setDate(new Date()); p.setName("张三" + i); mDbService.savePerson(p); } getData(); return mDbService.getAllPerson(); } private List<Person> getDataWhere() { System.out.println("**********************************************"); List<Person> dataperson; dataperson = (List<Person>) mDbService.mPersonDao.queryBuilder().where( Properties.Age.between("100", "105"),Properties.Color.eq("红色3")).list(); for (int i = 0; i < dataperson.size(); i++) { Log.e(TAG, dataperson.get(i).getAge()); } System.out.println("**********************************************"); return dataperson; } private List<Person> getData() { System.out.println("一共有" + mDbService.getAllPerson().size() + "条数据"); for (Iterator<Person> ii = mDbService.getAllPerson().iterator(); ii .hasNext();) { Log.e(TAG, "年龄=" + "\n" + ii.next().getAge()); } return mDbService.getAllPerson(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
其它详情,具体操作
参考文章:http://greendao-orm.com/documentation/how-to-get-started/
http://my.oschina.net/cheneywangc/blog/196354
http://www.w2bc.com/Article/20726