數據存儲之GreenDao的使用

數據存儲之GreenDao的使用

1、Project的build.gradle中加入

dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'
        classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
        classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7'
        classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2' // 添加插件 更好支持GreenDao
    }

2、app的build.gradle中加入

android {
	......
	greendao {
        //指定數據庫schema版本號,遷移等操作會用到;
        schemaVersion 1
        //dao的包名,包名默認是entity所在的包;
        daoPackage 'com.greendao.gen'
        //生成數據庫文件的目錄;
        targetGenDir 'src/main/java'
    }
}

dependencies {
	 //數據庫GreenDao
    implementation 'org.greenrobot:greendao:3.2.2'
}

3、創建DbController來初始化和增刪改查數據

public class DbController {
    /**
     * Helper
     */
    private DaoMaster.DevOpenHelper mHelper;//獲取Helper對象
    /**
     * 數據庫
     */
    private SQLiteDatabase db;
    /**
     * DaoMaster
     */
    private DaoMaster mDaoMaster;
    /**
     * DaoSession
     */
    private DaoSession mDaoSession;
    /**
     * 上下文
     */
    private Context context;
    /**
     * dao
     */
    private HistorySearchBeanDao historySearchBeanDao;

    private static DbController mDbController;

    /**
     * 獲取單例
     */
    public static DbController getInstance(Context context) {
        if (mDbController == null) {
            synchronized (DbController.class) {
                if (mDbController == null) {
                    mDbController = new DbController(context);
                }
            }
        }
        return mDbController;
    }

    /**
     * 初始化
     *
     * @param context
     */
    public DbController(Context context) {
        this.context = context;
        mHelper = new DaoMaster.DevOpenHelper(context, "searchhistory.db", null);
        mDaoMaster = new DaoMaster(getWritableDatabase());
        mDaoSession = mDaoMaster.newSession();
        historySearchBeanDao = mDaoSession.getHistorySearchBeanDao();
    }

    /**
     * 獲取可讀數據庫
     */
    private SQLiteDatabase getReadableDatabase() {
        if (mHelper == null) {
            mHelper = new DaoMaster.DevOpenHelper(context, "searchhistory.db", null);
        }
        SQLiteDatabase db = mHelper.getReadableDatabase();
        return db;
    }

    /**
     * 獲取可寫數據庫
     *
     * @return
     */
    private SQLiteDatabase getWritableDatabase() {
        if (mHelper == null) {
            mHelper = new DaoMaster.DevOpenHelper(context, "searchhistory.db", null);
        }
        SQLiteDatabase db = mHelper.getWritableDatabase();
        return db;
    }

    /**
     * 會自動判定是插入還是替換
     *
     * @param historySearchBean
     */
    public void insertOrReplace(HistorySearchBean historySearchBean) {
        historySearchBeanDao.insertOrReplace(historySearchBean);
    }

    /**
     * 插入一條記錄,表裏面要沒有與之相同的記錄
     *
     * @param historySearchBean
     */
    public void insert(HistorySearchBean historySearchBean) {
        HistorySearchBean mOldHistorySearchBean = historySearchBeanDao.queryBuilder().where(HistorySearchBeanDao.Properties.HistoryKey.eq(historySearchBean.getHistoryKey())).build().unique();
        if (mOldHistorySearchBean == null){
            historySearchBeanDao.insert(historySearchBean);
        }
    }

    /**
     * 更新數據
     *
     * @param historySearchBean
     */
    public void update(HistorySearchBean historySearchBean) {
        HistorySearchBean mOldHistorySearchBean = historySearchBeanDao.queryBuilder().where(HistorySearchBeanDao.Properties.Id.eq(historySearchBean.getId())).build().unique();//拿到之前的記錄
        if (mOldHistorySearchBean != null) {
            mOldHistorySearchBean.setHistoryKey("");
            historySearchBeanDao.update(mOldHistorySearchBean);
        }
    }

    /**
     * 按條件查詢數據
     */
    public List<HistorySearchBean> searchByWhere(String wherecluse) {
        List<HistorySearchBean> historySearchBeanList = (List<HistorySearchBean>) historySearchBeanDao.queryBuilder().where(HistorySearchBeanDao.Properties.HistoryKey.eq(wherecluse)).build().unique();
        return historySearchBeanList;
    }

    /**
     * 查詢所有數據
     */
    public List<HistorySearchBean> searchAll() {
        List<HistorySearchBean> historySearchBeanList = historySearchBeanDao.queryBuilder().list();
        return historySearchBeanList;
    }

    /**
     * 刪除指定數據
     */
    public void delete(String wherecluse) {
        historySearchBeanDao.queryBuilder().where(HistorySearchBeanDao.Properties.HistoryKey.eq(wherecluse)).buildDelete().executeDeleteWithoutDetachingEntities();
    }

    /**
     * 刪除所有數據
     */
    public void deleteAll(){
        //清除daoSession的緩存
//        mDaoSession.clear();
        //清除指定dao類的緩存
        historySearchBeanDao.deleteAll();
    }
}

4、創建一個存放數據的bean

@Entity
public class HistorySearchBean {
    @Id(autoincrement = true)//設置自增長
    private Long id;

    private String historyKey;

    public Long getId() {
        return id;
    }

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

    @Generated(hash = 504811364)
    public HistorySearchBean(Long id, String historyKey) {
        this.id = id;
        this.historyKey = historyKey;
    }

    @Generated(hash = 954352461)
    public HistorySearchBean() {
    }

    public String getHistoryKey() {
        return historyKey;
    }

    public void setHistoryKey(String historyKey) {
        this.historyKey = historyKey;
    }
}

5、初始化調用

我的是再搜索界面調用的,所以再搜索activity中全局定義並初始化greendao

private DbController mDbController;
//在onCreate()方法中初始化:
 @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hot_search);
		 //初始化GreenDao
        mDbController = DbController.getInstance(SearchHotActivity.this);
}

搜索的關鍵詞跳轉時保存,返回或者重新創建activity時加載,並加個刪除功能

保存當前搜索的key用:
 //插入數據
 mDbController.insert(new HistorySearchBean(null,searchKey));
加載全部搜索歷史用:
//搜索搜索詞的集合
    private List<HistorySearchBean> historySearchBeanList = new ArrayList<>();
        if (null!= mDbController){
            historySearchBeanList = mDbController.searchAll();
        }
刪除全部歷史用:
 mDbController.deleteAll();

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章