需要下载的jar包
greendao:greendao jar
greendap-generator:generator jar
freemark:http://freemarker.org/
官方实例:官方demo
GreenDao的使用可以分为步,首先是生成关联的数据库实体类,接着是在android项目中导入生成的文件,配置好环境后就可以使用了。
1.生成相关数据库文件。
生成表(在这只写了一个NoteEntity表),在表中你可以定义属性的类型,只是不能设定默认值。
package com.cn;
import java.io.IOException;
import de.greenrobot.daogenerator.DaoGenerator;
import de.greenrobot.daogenerator.Entity;
import de.greenrobot.daogenerator.Schema;
public class TestMain {
public static void main(String[] args) {
try {
createEntity();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 创建数据表
* createEntity
* @throws Exception
* @throws IOException
*/
private static void createEntity() throws IOException, Exception {
//你保存greendao 文件路径
Schema schema = new Schema(1, "com.cn.greenandroid.green");
Entity note = schema.addEntity("NoteEntity");
note.addIdProperty().primaryKey();
note.addStringProperty("noteName").notNull();
note.addStringProperty("noteContent");
note.addDateProperty("date");
new DaoGenerator().generateAll(schema, "../TestJGreen/dao");
}
}
执行过后,刷新项目就可以在dao文件夹下得到生成的相关文件了。
2.使用greendao
将生成的文件导入到你的项目中(记得存放的路径最好跟你生成文件的路径一样)。
推荐在BaseApplication中配置DaoMaster和DaoSession。
package com.cn.greenandroid;
import com.cn.greenandroid.green.DaoMaster;
import com.cn.greenandroid.green.DaoMaster.OpenHelper;
import com.cn.greenandroid.green.DaoSession;
import android.app.Application;
import android.content.Context;
public class BaseApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
}
private static DaoMaster daoMaster;
private static DaoSession daoSession;
/**
* 取得DaoMaster
*/
public static DaoMaster getDaoMaster(Context context) {
if (daoMaster == null) {
OpenHelper helper = new DaoMaster.DevOpenHelper(context, "green.db", null);
daoMaster = new DaoMaster(helper.getWritableDatabase());
}
return daoMaster;
}
/**
* 取得DaoSession
*/
public static DaoSession getDaoSession(Context context) {
if (daoSession == null) {
if (daoMaster == null) {
daoMaster = getDaoMaster(context);
}
daoSession = daoMaster.newSession();
}
return daoSession;
}
}
然后为写一个工具类,来执行表的相关操作。
package com.cn.greenandroid.util;
import java.util.List;
import com.cn.greenandroid.BaseApplication;
import com.cn.greenandroid.green.DaoSession;
import com.cn.greenandroid.green.NoteEntity;
import com.cn.greenandroid.green.NoteEntityDao;
import com.cn.greenandroid.green.NoteEntityDao.Properties;
import android.content.Context;
import de.greenrobot.dao.query.DeleteQuery;
import de.greenrobot.dao.query.QueryBuilder;
public class GreenHelper {
private static GreenHelper greenHelper;
private static Context mContext;
private DaoSession daoSession;
private static NoteEntityDao noteEntityDao;
private GreenHelper() {
}
public static GreenHelper getInstance(Context context) {
if (null == greenHelper) {
greenHelper = new GreenHelper();
if (null == mContext) {
mContext = context.getApplicationContext();
}
greenHelper.daoSession = BaseApplication.getDaoSession(context);
greenHelper.noteEntityDao = greenHelper.daoSession.getNoteEntityDao();
}
return greenHelper;
}
public static void insertNoteEntity(NoteEntity note) {
noteEntityDao.insertOrReplace(note);
}
public static void insertNoteEntity(List<NoteEntity> list) {
noteEntityDao.insertInTx(list);
}
public static void deleteNoteEntity(NoteEntity note) {
noteEntityDao.delete(note);
}
public static void updateNoteEntity(NoteEntity note) {
// noteEntityDao.insertOrReplace(note);//当存在条数据 也可以更新
noteEntityDao.update(note);
}
public static List<NoteEntity> selectNoteEntity() {
// return noteEntityDao.loadAll();
return noteEntityDao.loadAll();
}
public static List<NoteEntity> selectOrderNoteEntity() {
QueryBuilder<NoteEntity> qb = noteEntityDao.queryBuilder();
qb.orderDesc(Properties.Date);
return qb.list();
}
public static List<NoteEntity> selectIdNoteEntity(long id) {
QueryBuilder<NoteEntity> qb = noteEntityDao.queryBuilder();
qb.where(NoteEntityDao.Properties.Id.eq(id));
return qb.list();
}
public static void deleteNoteEntity() {
QueryBuilder<NoteEntity> qb = noteEntityDao.queryBuilder();
DeleteQuery<NoteEntity> bd = qb.where(NoteEntityDao.Properties.NoteContent.isNotNull()).buildDelete();
bd.executeDeleteWithoutDetachingEntities();
}
}
最后就是在项目中使用工具类的方法了。
package com.cn.greenandroid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.cn.greenandroid.green.NoteEntity;
import com.cn.greenandroid.util.GreenHelper;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
private String Tag = "MainActivity";
private GreenHelper mHelper;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mHelper = GreenHelper.getInstance(this);
tv = (TextView) findViewById(R.id.textView);
// insertEntity();
// insertListEntity();
// deleteEntity();
// updateEntity();
// selectCount(2L);
// deleteSome();
selectAll();
}
private void insertEntity() {// 插入3条数据
for (int i = 0; i < 3; i++) {
NoteEntity note = new NoteEntity();
note.setNoteName("name" + i);
note.setNoteContent("content" + i);
note.setDate(new Date());
mHelper.insertNoteEntity(note);
}
}
private void insertListEntity() {// 插入3条数据
List<NoteEntity> list = new ArrayList<NoteEntity>();
for (int i = 0; i < 3; i++) {
NoteEntity note = new NoteEntity();
note.setNoteName("name" + i);
note.setNoteContent("content" + i);
note.setDate(new Date());
list.add(note);
}
mHelper.insertNoteEntity(list);
}
private void deleteEntity() {// 删除一条数据
NoteEntity note = new NoteEntity();
note.setId(1L);
mHelper.deleteNoteEntity(note);
}
private void updateEntity() {
NoteEntity note = new NoteEntity();
note.setId(2L);
note.setNoteName("shenma");
note.setNoteContent("xyz");
mHelper.updateNoteEntity(note);
}
private void selectCount(long id) {
List<NoteEntity> list = mHelper.selectIdNoteEntity(id);
listNote(list);
}
private void deleteSome() {
mHelper.deleteNoteEntity();
}
private void selectAll() {
// List<NoteEntity> list = mHelper.selectNoteEntity();
List<NoteEntity> list = mHelper.selectOrderNoteEntity();
listNote(list);
}
private void listNote(List<NoteEntity> list) {
StringBuffer buf = new StringBuffer();
for (NoteEntity note : list) {
buf.append(note.getId() + note.getNoteName() + note.getNoteContent());
buf.append("\n");
}
tv.setText(buf.toString());
}
}