This repository has been archived by the owner on Apr 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
729 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
app/src/main/java/com/leon/biuvideo/greendao/dao/DaoBaseUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
package com.leon.biuvideo.greendao.dao; | ||
|
||
import org.greenrobot.greendao.AbstractDao; | ||
import org.greenrobot.greendao.query.QueryBuilder; | ||
import org.greenrobot.greendao.query.WhereCondition; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* @Author Leon | ||
* @Time 2021/4/1 | ||
* @Desc CRUD基础类 | ||
*/ | ||
public class DaoBaseUtils<T> { | ||
private final DaoSession daoSession; | ||
private final Class<T> entityClass; | ||
private final AbstractDao<T, Long> entityDao; | ||
|
||
public DaoBaseUtils(Class<T> entityClass, AbstractDao<T, Long> entityDao, DaoSession daoSession) { | ||
this.daoSession = daoSession; | ||
this.entityClass = entityClass; | ||
this.entityDao = entityDao; | ||
} | ||
|
||
/** | ||
* 插入单条数据 | ||
* | ||
* @param entity 数据 | ||
* @return 插入状态 | ||
*/ | ||
public boolean insert(T entity) { | ||
return entityDao.insert(entity) != -1; | ||
} | ||
|
||
/** | ||
* 插入多条数据 | ||
* | ||
* @param entityList 数据集合 | ||
* @return 插入状态 | ||
*/ | ||
public boolean insertMultiple (final List<T> entityList) { | ||
try { | ||
daoSession.runInTx(new Runnable() { | ||
@Override | ||
public void run() { | ||
for (T entity : entityList) { | ||
daoSession.insertOrReplace(entity); | ||
} | ||
} | ||
}); | ||
|
||
return true; | ||
} catch (Exception e) { | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* 修改数据 | ||
* | ||
* @param entity 新数据 | ||
* @return 修改状态 | ||
*/ | ||
public boolean update (T entity) { | ||
try { | ||
daoSession.update(entity); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* 删除数据 | ||
* | ||
* @param entity 被删除数据 | ||
* @return 删除状态 | ||
*/ | ||
public boolean delete (T entity) { | ||
try { | ||
daoSession.delete(entity); | ||
return true; | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* 删除所有数据 | ||
* | ||
* @return 删除状态 | ||
*/ | ||
public boolean deleteAll () { | ||
try { | ||
daoSession.deleteAll(entityClass); | ||
return true; | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* 查询所有数据 | ||
* | ||
* @return 查询到的结果 | ||
*/ | ||
public List<T> queryAll () { | ||
return daoSession.loadAll(entityClass); | ||
} | ||
|
||
/** | ||
* 根据ID进行查询 | ||
* | ||
* @param key ID/Key | ||
* @return 查询到的数据 | ||
*/ | ||
public T queryById (long key) { | ||
return daoSession.load(entityClass, key); | ||
} | ||
|
||
/** | ||
* 使用native sql进行查询 | ||
* | ||
* @param sql SQL语句 | ||
* @param conditions 条件 | ||
* @return 查询到的结果 | ||
*/ | ||
public List<T> queryByNativeSql (String sql, String[] conditions) { | ||
return daoSession.queryRaw(entityClass, sql, conditions); | ||
} | ||
|
||
/** | ||
* 使用queryBuilder进行查询 | ||
*/ | ||
public List<T> queryByQueryBuilder (WhereCondition condition, WhereCondition ... conditionsMore) { | ||
QueryBuilder<T> queryBuilder = daoSession.queryBuilder(entityClass); | ||
return queryBuilder.where(condition, conditionsMore).list(); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
app/src/main/java/com/leon/biuvideo/greendao/dao/DaoManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.leon.biuvideo.greendao.dao; | ||
|
||
import android.content.Context; | ||
|
||
/** | ||
* @Author Leon | ||
* @Time 2021/4/1 | ||
* @Desc Dao管理器 | ||
*/ | ||
public class DaoManager { | ||
private static final String DB_NAME = "biuvideo"; | ||
private DaoMaster daoMaster; | ||
private DaoSession daoSession; | ||
private final Context context; | ||
private DaoMaster.DevOpenHelper devOpenHelper; | ||
|
||
public DaoManager(Context context) { | ||
this.context = context; | ||
} | ||
|
||
/** | ||
* 判断是否存在数据库,如果没有就创建 | ||
*/ | ||
public DaoMaster getDaoMaster () { | ||
if (daoMaster == null) { | ||
devOpenHelper = new DaoMaster.DevOpenHelper(context, DB_NAME); | ||
daoMaster = new DaoMaster(devOpenHelper.getWritableDatabase()); | ||
} | ||
|
||
return daoMaster; | ||
} | ||
|
||
/** | ||
* 获取Session对象 | ||
* | ||
* @return DaoSession | ||
*/ | ||
public DaoSession getDaoSession () { | ||
if (daoSession == null) { | ||
if (daoMaster == null) { | ||
daoMaster = getDaoMaster(); | ||
} | ||
|
||
daoSession = daoMaster.newSession(); | ||
} | ||
|
||
return daoSession; | ||
} | ||
|
||
/** | ||
* 关闭与数据库的链接 | ||
*/ | ||
public void closeConnection () { | ||
closeHelper(); | ||
closeDaoSession(); | ||
} | ||
|
||
private void closeHelper () { | ||
if (devOpenHelper != null) { | ||
devOpenHelper.close(); | ||
devOpenHelper = null; | ||
} | ||
} | ||
|
||
private void closeDaoSession () { | ||
if (daoSession != null) { | ||
daoSession.clear(); | ||
daoSession = null; | ||
} | ||
} | ||
} |
Oops, something went wrong.