Android 对greendao数据库封装及使用

实体类:DbBookshelf

package com.reader.client.module.bookshelf;

import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Generated;
import org.greenrobot.greendao.annotation.Id;

import java.util.Date;

/**
 * Created by Eddie on 2017/7/11.
 */

@Entity(nameInDb = "bookshelf")
public class DbBookshelf {


    /**
     * 书排序
     */
    @Id(autoincrement = true)
    private Long id;

    /**
     * 书名称
     */
    private String name;


    /**
     * 作者名称
     */
    private String author;

    /**
     * CDN地址
     */
    private String cdn_flow_url;

    /**
     * CDN地址
     */
    private String cdn_peak_url;

    /**
     * 章节数
     */
    private int chapter_count;

    /**
     * 完结
     */
    private int completed;

    /**
     * NCX文件地址
     */
    private String cp_list_url;

    /**
     * 书名称
     */
    private String cps_path;

    /**
     * 非CDN地址
     */
    private String default_url;

    /**
     * 简介(详情)
     */
    private String summary;

    /**
     * 图片地址
     */
    private String image_url;

    /**
     * 书编号
     */
    private String book_id;

    /**
     * 解密key
     */
    private String key;

    /**
     * 新章节
     */
    private String newest_chapter;

    /**
     * 字数
     */
    private int word_count;

    /**
     * 书排序
     */
    private int sortId;

    /**
     * 最后阅读时间
     */
    private Date read_datetime;

    /**
     * 增加类型 0、系统推荐 1、用户自己增加
     */
    private int book_add_type;

    /**
     * 下载类型
     */
    private int down_type;

    /**
     * 加入书架时间
     */
    private Date add_date;

    /**
     * 阅读状态
     */
    private int read_type;

    /**
     * 阅读章节
     */
    private int read_chapter;

    /**
     * 阅读章节名称
     */
    private String read_chapter_title;

    /**
     * 阅读页数
     */
    private int read_chapter_page;

    @Generated(hash = 292715416)
    public DbBookshelf(Long id, String name, String author, String cdn_flow_url,
            String cdn_peak_url, int chapter_count, int completed,
            String cp_list_url, String cps_path, String default_url, String summary,
            String image_url, String book_id, String key, String newest_chapter,
            int word_count, int sortId, Date read_datetime, int book_add_type,
            int down_type, Date add_date, int read_type, int read_chapter,
            String read_chapter_title, int read_chapter_page) {
        this.id = id;
        this.name = name;
        this.author = author;
        this.cdn_flow_url = cdn_flow_url;
        this.cdn_peak_url = cdn_peak_url;
        this.chapter_count = chapter_count;
        this.completed = completed;
        this.cp_list_url = cp_list_url;
        this.cps_path = cps_path;
        this.default_url = default_url;
        this.summary = summary;
        this.image_url = image_url;
        this.book_id = book_id;
        this.key = key;
        this.newest_chapter = newest_chapter;
        this.word_count = word_count;
        this.sortId = sortId;
        this.read_datetime = read_datetime;
        this.book_add_type = book_add_type;
        this.down_type = down_type;
        this.add_date = add_date;
        this.read_type = read_type;
        this.read_chapter = read_chapter;
        this.read_chapter_title = read_chapter_title;
        this.read_chapter_page = read_chapter_page;
    }

    @Generated(hash = 315207481)
    public DbBookshelf() {
    }

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return this.author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getCdn_flow_url() {
        return this.cdn_flow_url;
    }

    public void setCdn_flow_url(String cdn_flow_url) {
        this.cdn_flow_url = cdn_flow_url;
    }

    public String getCdn_peak_url() {
        return this.cdn_peak_url;
    }

    public void setCdn_peak_url(String cdn_peak_url) {
        this.cdn_peak_url = cdn_peak_url;
    }

    public int getChapter_count() {
        return this.chapter_count;
    }

    public void setChapter_count(int chapter_count) {
        this.chapter_count = chapter_count;
    }

    public int getCompleted() {
        return this.completed;
    }

    public void setCompleted(int completed) {
        this.completed = completed;
    }

    public String getCp_list_url() {
        return this.cp_list_url;
    }

    public void setCp_list_url(String cp_list_url) {
        this.cp_list_url = cp_list_url;
    }

    public String getCps_path() {
        return this.cps_path;
    }

    public void setCps_path(String cps_path) {
        this.cps_path = cps_path;
    }

    public String getDefault_url() {
        return this.default_url;
    }

    public void setDefault_url(String default_url) {
        this.default_url = default_url;
    }

    public String getSummary() {
        return this.summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    public String getImage_url() {
        return this.image_url;
    }

    public void setImage_url(String image_url) {
        this.image_url = image_url;
    }

    public String getBook_id() {
        return this.book_id;
    }

    public void setBook_id(String book_id) {
        this.book_id = book_id;
    }

    public String getKey() {
        return this.key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getNewest_chapter() {
        return this.newest_chapter;
    }

    public void setNewest_chapter(String newest_chapter) {
        this.newest_chapter = newest_chapter;
    }

    public int getWord_count() {
        return this.word_count;
    }

    public void setWord_count(int word_count) {
        this.word_count = word_count;
    }

    public int getSortId() {
        return this.sortId;
    }

    public void setSortId(int sortId) {
        this.sortId = sortId;
    }

    public Date getRead_datetime() {
        return this.read_datetime;
    }

    public void setRead_datetime(Date read_datetime) {
        this.read_datetime = read_datetime;
    }

    public int getBook_add_type() {
        return this.book_add_type;
    }

    public void setBook_add_type(int book_add_type) {
        this.book_add_type = book_add_type;
    }

    public int getDown_type() {
        return this.down_type;
    }

    public void setDown_type(int down_type) {
        this.down_type = down_type;
    }

    public Date getAdd_date() {
        return this.add_date;
    }

    public void setAdd_date(Date add_date) {
        this.add_date = add_date;
    }

    public int getRead_type() {
        return this.read_type;
    }

    public void setRead_type(int read_type) {
        this.read_type = read_type;
    }

    public int getRead_chapter() {
        return this.read_chapter;
    }

    public void setRead_chapter(int read_chapter) {
        this.read_chapter = read_chapter;
    }

    public String getRead_chapter_title() {
        return this.read_chapter_title;
    }

    public void setRead_chapter_title(String read_chapter_title) {
        this.read_chapter_title = read_chapter_title;
    }

    public int getRead_chapter_page() {
        return this.read_chapter_page;
    }

    public void setRead_chapter_page(int read_chapter_page) {
        this.read_chapter_page = read_chapter_page;
    }

    

}

数据库的管理类:DaoManager

package com.reader.client.module.bookshelf;

/**
 * Created by Eddie on 2017/7/11.
 */

import android.content.Context;

import org.greenrobot.greendao.query.QueryBuilder;

/**
 * 1、创建数据库 * 2、创建数据库的表 * 3、包含对数据库的CRUD * 4、对数据库的升级
 */
public class DaoManager {


    private static final String TAG = DaoManager.class.getSimpleName();
    private static final String DB_NAME = "novel.sqlite";//数据库名称
    private volatile static DaoManager manager;//多线程访问
    private static DaoMaster.DevOpenHelper helper;
    private static DaoMaster daoMaster;
    private static DaoSession daoSession;
    private Context context;

    /**
     * 使用单例模式获得操作数据库的对象
     *
     * @return
     */
    public static DaoManager getInstance() {
        DaoManager instance = null;
        if (manager == null) {
            synchronized (DaoManager.class) {
                if (instance == null) {
                    instance = new DaoManager();
                    manager = instance;
                }
            }
        }
        return instance;
    }

    public void init(Context context) {
        this.context = context;
    }

    /**
     * 判断是否存在数据库,如果没有则创建数据库
     * @return DaoMaster
     */
    public DaoMaster getDaoMaster() {
        if (daoMaster == null) {
            DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, DB_NAME, null);
            daoMaster = new DaoMaster(helper.getWritableDatabase());
        }
        return daoMaster;
    }

    /**
     * 完成对数据库的添加、删除、修改、查询的操作,仅仅是一个接口
     * @return DaoSession
     */
    public DaoSession getDaoSession() {
        if (daoSession == null) {
            if (daoMaster == null) {
                daoMaster = getDaoMaster();
            }
            daoSession = daoMaster.newSession();
        }
        return daoSession;
    }

    /**
     * 打开输出日志的操作,默认是关闭的
     */
    public void setDebug() {
        QueryBuilder.LOG_SQL = true;
        QueryBuilder.LOG_VALUES = true;
    }

    /**
     * 关闭所有的操作,数据库开启的时候,使用完毕了必须要关闭
     */
    public void closeConnection() {
        closeHelper();
        closeDaoSession();
    }

    public void closeHelper() {
        if (helper != null) {
            helper.close();
            ;
            helper = null;
        }
    }

    public void closeDaoSession() {
        if (daoSession != null) {
            daoSession.clear();
            ;
            daoSession = null;
        }
    }
}

对表的操作类:DbBookshelfManager

package com.reader.client.module.bookshelf;

/**
 * Created by Eddie on 2017/7/11.
 */

import android.content.Context;

import java.util.List;

/**
 * 完成对某一张表的具体操作,ORM 操作的是对象,DbBookshelf
 */
public class DbBookshelfManager {


    private DaoManager manager;

    public DbBookshelfManager(Context context) {
        manager = DaoManager.getInstance();
        manager.init(context);
    }

    /**
     * 完成对数据库中bookshelfModel 表的插入操作
     *
     * @param bookshelfModel
     * @return flag
     */
    public boolean insertBookshelfModel(DbBookshelf bookshelfModel) {
        boolean flag = manager.getDaoSession().insertOrReplace(bookshelfModel) != -1 ? true : false;
        return flag;
    }

    /**
     * 插入多条记录,需要开辟新的线程
     *
     * @param bookshelfModels
     * @return flag
     **/
    public boolean insertMultBookshelfModel(final List bookshelfModels) {
        boolean flag = false;

        try {
            manager.getDaoSession().runInTx(new Runnable() {
                @Override
                public void run() {
                    for (DbBookshelf bookshelfModel : bookshelfModels) {
                        manager.getDaoSession().insertOrReplace(bookshelfModel);
                    }
                }
            });
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 完成对student的某一条记录的修改
     *
     * @param bookshelfModel
     * @return flag
     */
    public boolean updateBookshelfModel(DbBookshelf bookshelfModel) {
        boolean flag = false;
        try {
            manager.getDaoSession().update(bookshelfModel);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * @param bookshelfModel
     * @return flag
     */
    public boolean deleteBookshelfModel(DbBookshelf bookshelfModel) {
        boolean flag = false;
        try {
            //按照指定的id进行删除 delete from student where _id = ?
            manager.getDaoSession().delete(bookshelfModel);
            //manager.getDaoSession().deleteAll();//删除所有的记录
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /** * */
    public void deleteAllBookshelfModel(Class cls) {
        manager.getDaoSession().deleteAll(cls);
    }

    /**
     * 返回多行记录 * * @return
     */
    public List listAll() {
        return manager.getDaoSession().loadAll(DbBookshelf.class);
    }

    /**
     * 按照主键返回单行记录 * * @param key * @return
     */
    public DbBookshelf listOneBookshelfModel(long key) {
        return manager.getDaoSession().load(DbBookshelf.class, key);
    }

    /**
     * 查询bookshelf表中所有数据
     * @return List
     */
    public List queryAll() {
        List list = manager.getDaoSession().loadAll(DbBookshelf.class);
        return list;
    }

}

工具类:DaoUtils

package com.reader.base.util;

import android.content.Context;

import com.reader.client.module.bookshelf.DbBookshelfManager;

/**
 * Created by Eddie on 2017/7/12.
 */

public class DaoUtils {


    private static DbBookshelfManager bookshelfManager;
    public static Context context;

    public static void init(Context context){
        DaoUtils.context = context.getApplicationContext();
    }

    /**
     * 单列模式获取StudentManager对象
     * @return
     */
    public static DbBookshelfManager getBookshelfInstance(){
        if (bookshelfManager == null) {
            bookshelfManager = new DbBookshelfManager(context);
        }
        return bookshelfManager;
    }

}

初始化

DaoUtils.init(this.mContext);

使用:

DbBookshelf bookshelfModel =new DbBookshelf();
bookshelfModel.setName("忘情水");
bookshelfModel.setAuthor("刘德华");
bookshelfModel.setBook_id("121212");
boolean flag = DaoUtils.getBookshelfInstance().insertBookshelfModel(bookshelfModel);

你可能感兴趣的:(Android 对greendao数据库封装及使用)