数据存储之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();