关于第三方开源库GreenDao的使用心得

简介:GreenDao是一个用于操作Sqlite数据库的开源ORM框架,可以很方便的对数据库进行对象映射,增删改查等操作。优点是效率方面在类似框架中是最高的
使用步骤:
1、从github上将项目下载下来,导入开发工具。下载地址:https://github.com/greenrobot/greenDAO
2、其中有两个java项目,这两个java项目是用于生成bean文件和DaoMaster、DaoSession文件的,在示例文件中添加指定的bean,设置属性后运行java项目。此处注意,该项目运行java项目时,需要导入freemarker-xxx.jar; 指定生成的目录时要填写src-gen,该目录必须在android项目中创建好,否则会报错;要写android项目的包名及项目路径
3、生成完后,在android项目中刷新src-gen目录后找到生成的文件,在自定义application中,添加两个方法

    /**
     * 取得DaoMaster
     * 
     * @param context
     * @return
     */
    public static DaoMaster getDaoMaster(Context context) {
        if (daoMaster == null) {
            OpenHelper helper = new DaoMaster.DevOpenHelper(context,
                    Constants.DB_NAME, null);
            daoMaster = new DaoMaster(helper.getWritableDatabase());
        }
        return daoMaster;
    }


    /**
     * 取得DaoSession
     * 
     * @param context
     * @return
     */
    public static DaoSession getDaoSession(Context context) {
        if (daoSession == null) {
            if (daoMaster == null) {
                daoMaster = getDaoMaster(context);
            }
            daoSession = daoMaster.newSession();
        }
        return daoSession;
    }

4、然后创建数据库操作类

public class TestDao {
    private static TestDao mInstance;
    private DaoSession mDaoSession;

    private NoteDao noteDao;

    private TestDao() {
    }

    public static TestDao getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new TestDao();
            mInstance.mDaoSession = MyApplication.getDaoSession(context);
            mInstance.noteDao = mInstance.mDaoSession.getNoteDao();
        }

        return mInstance;
    }

    /**
     * 增加或修改某条数据
     * 
     * @param note
     */
    public void saveNote(Note note) {
        noteDao.insertOrReplace(note);
    }

    /**
     * 批量添加数据(开启线程)
     * 
     * @param list
     */
    public void saveNoteLists(final List list) {
        if (list == null || list.isEmpty()) {
            return;
        }
        noteDao.getSession().runInTx(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < list.size(); i++) {
                    Note note = list.get(i);
                    noteDao.insertOrReplace(note);
                }
            }
        });
    }

    /**
     * 删除指定id的数据
     * 
     * @param id
     */
    public void deleteNote(long id) {
        noteDao.deleteByKey(id);
    }

    /**
     * 删除指定的数据
     * 
     * @param note
     */
    public void deleteNote(Note note) {
        noteDao.delete(note);
    }

    /**
     * 删除全部数据
     */
    public void deleteAllNote() {
        noteDao.getSession().runInTx(new Runnable() {

            @Override
            public void run() {
                noteDao.deleteAll();
            }
        });
    }

    /**
     * 查找指定数据
     * 
     * @param id
     * @return
     */
    public Note loadNote(long id) {
        return noteDao.load(id);
    }

    /**
     * 查找全部数据
     * 
     * @return
     */
    public List loadAllNote() {
        return noteDao.loadAll();
    }

    /**
     * 根据条件查找数据
     * 
     * @param where
     * @param params
     * @return
     */
    public List queryNote(String where, String... params) {
        return noteDao.queryRaw(where, params);
    }

总结:在使用时直接调用对应的方法即可,可根据需求自定义数据库操作类,非常方便和高效的使用方式

你可能感兴趣的:(第三方框架总结)