網(wǎng)站首頁 編程語言 正文
數(shù)據(jù)持久化就是指將那些內(nèi)存中的瞬時數(shù)據(jù)保存到存儲設(shè)備中,保證即使在手機或電腦關(guān)機的情況下,這些數(shù)據(jù)仍然不會丟失。保存在內(nèi)存中的數(shù)據(jù)是處于瞬時狀態(tài)的,而保存在存儲設(shè)備中的數(shù)據(jù)是處于持久狀態(tài)的,持久化技術(shù)則提供了一種機制可以讓數(shù)據(jù)在瞬時狀態(tài)和持久狀態(tài)之間進行轉(zhuǎn)換。
目前,Android系統(tǒng)中提供了3種方式的數(shù)據(jù)持久化技術(shù),即文件存儲、SharedPreferences存儲以及數(shù)據(jù)庫存儲。當然,除了這3種方式之外,你還可以將數(shù)據(jù)保存在手機的SD卡中,不過使用文件、Shared Preferences或數(shù)據(jù)庫來保存數(shù)據(jù)會相對更簡單一些,而且比起將數(shù)據(jù)保存在SD卡中會更加地安全。Shared Preferences通常用在輕量級的數(shù)據(jù)存儲場景中,比如賬號/密碼的存儲,而數(shù)據(jù)庫則用在數(shù)據(jù)量比較大的場景中,比如聊天數(shù)據(jù)的存儲。
現(xiàn)在,使用數(shù)據(jù)庫存儲時候,一般都會使用一些第三方ORM框架,比如GreenDao。在Android開發(fā)中,集成Greendao通常需要如下幾步:
1.首先,在項目的build.gradle文件中添加依賴:
classpath 'org.greenrobot:greendao-gradle-plugin:3.3.0' // add plugin
2.然后,在app/build.gradle文件中添加如下依賴:
apply plugin: 'org.greenrobot.greendao' // apply plugin
implementation 'org.greenrobot:greendao:3.3.0'
為了方便操作GreenDao數(shù)據(jù)庫,我們還需要對其進行封裝。首先,我們創(chuàng)建一個實體對象:
package com.yufulife.xj.model;
import com.yufulife.xj.bean.CameraTakeBean;
import com.yufulife.xj.bean.JoinTakeBean;
import com.yufulife.xj.bean.ListSubCachedBeanConverter;
import org.greenrobot.greendao.annotation.Convert;
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Generated;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Unique;
import java.util.HashMap;
import java.util.List;
@Entity
public class People {
//不能用int
@Id(autoincrement = true)
private Long id;
//巡檢編號
@Unique
private String inspection_id="";
private String content="";
@Convert(converter = ListSubCachedBeanConverter.class, columnType = String.class)
private List<JoinTakeBean> mImgTT;
@Generated(hash = 574353758)
public People(Long id, String inspection_id, String content, List<JoinTakeBean> mImgTT) {
this.id = id;
this.inspection_id = inspection_id;
this.content = content;
this.mImgTT = mImgTT;
}
@Generated(hash = 1406030881)
public People() {
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getInspection_id() {
return this.inspection_id;
}
public void setInspection_id(String inspection_id) {
this.inspection_id = inspection_id;
}
public String getContent() {
return this.content;
}
public void setContent(String content) {
this.content = content;
}
public List<JoinTakeBean> getMImgTT() {
return this.mImgTT;
}
public void setMImgTT(List<JoinTakeBean> mImgTT) {
this.mImgTT = mImgTT;
}
}
然后再封裝一個統(tǒng)一的管理類:
package com.yufulife.xj.model;
import android.util.Log;
import com.google.gson.Gson;
import com.yufulife.xj.App;
import org.greenrobot.greendao.query.QueryBuilder;
import java.util.ArrayList;
import java.util.List;
public class FaceMsgDao {
private static final String TAG = "ClockInfoDao";
/**
* 添加數(shù)據(jù),如果有重復(fù)則覆蓋
*
* @param people
*/
public static void insertData(People people) {
long l = App.getmDaoSession().getPeopleDao().insertOrReplace(people);
System.out.println("liu:::::: "+l);
}
public static long getId(People people) {
return App.getmDaoSession().getPeopleDao().getKey(people);
}
public static People getPeopleWhere(String Inspection_id){
List<People> list = App.getmDaoSession().getPeopleDao().queryBuilder().where(PeopleDao.Properties.Inspection_id.eq(Inspection_id)).list();
if (list!=null){
if (list.size()>=1){
return list.get(0);
}
}
return null;
}
public static void deletePeopleWhere(String Inspection_id){
App.getmDaoSession().getPeopleDao().queryBuilder().where(PeopleDao.Properties.Inspection_id.eq(Inspection_id)) .buildDelete().executeDeleteWithoutDetachingEntities();
}
/**
* 更新數(shù)據(jù)
*
* @param people
*/
public static void updateData(People people) {
App.getmDaoSession().getPeopleDao().update(people);
}
/**
* 查詢?nèi)繑?shù)據(jù)
*/
public static List<People> queryAll() {
return App.getmDaoSession().getPeopleDao().loadAll();
}
public static void systemOutPeopleAll(){
List<People> people = queryAll();
for (People people1:people){
System.out.println("liu::all "+new Gson().toJson(people1));
}
}
public static People getPeople(String war) {
List<People> clockInfos = queryAll();
if (clockInfos==null){
System.out.println("liu::: "+"沒有找到數(shù)據(jù)");
return null;
}
for (People info : clockInfos) {
if (info.getInspection_id() .equals(war) ) {
return info;
}
}
System.out.println("liu::: "+"沒有找到數(shù)據(jù)");
return null;
}
}
需要注意的是,在使用GreenDao數(shù)據(jù)庫之前,需要先在項目中初始化,比如。
private static DaoSession mDaoSession;
public static DaoSession getmDaoSession() {
return mDaoSession;
}
/**
* 初始化數(shù)據(jù)庫
*/
private void setDataBaseData() {
//創(chuàng)建數(shù)據(jù)庫shop.db"
DaoMaster.DevOpenHelper mDaoMaster = new DaoMaster.DevOpenHelper(this, "inspection.db", null);
//獲取可寫的數(shù)據(jù)庫
SQLiteDatabase writableDatabase = mDaoMaster.getWritableDatabase();
//獲取數(shù)據(jù)庫對象
DaoMaster daoMaster = new DaoMaster(writableDatabase);
//獲取Dao對象管理者
mDaoSession = daoMaster.newSession();
}
最后,只需要在業(yè)務(wù)中使用FaceMsgDao操作數(shù)據(jù)即可。
原文鏈接:https://blog.csdn.net/xiangzhihong8/article/details/127249360
相關(guān)推薦
- 2022-06-20 golang常用加密解密算法總結(jié)(AES、DES、RSA、Sha1、MD5)_Golang
- 2021-12-09 Typora自動編號的具體操作_其它綜合
- 2022-09-15 SQL?bool盲注和時間盲注詳解_MsSql
- 2022-07-01 docker搭建mongodb單節(jié)點副本集的實現(xiàn)_docker
- 2022-09-21 android實現(xiàn)簡單底部導(dǎo)航欄_Android
- 2022-04-27 剖析后OpLog訂閱MongoDB的數(shù)據(jù)變更就沒那么難了_MongoDB
- 2022-06-19 C語言圖文并茂講解分支語句用法_C 語言
- 2022-09-22 IO流技術(shù)中的File類
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支