第四章:Room数据库直接存储对象(二进制数据,Android面试必问的HashMap

  • 获取Cache数据库操作对象

  • @return

*/

public abstract CacheDao getCacheDao();

  • 创建缓存数据类(注意一定要序列化Serializable)

package cn.yumakeji.jetpackroomstudy.bean;

import java.io.Serializable;

public class CacheBean implements Serializable {

private String name;

private String title;

private long time;

private String imgUrl;

private String desc;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getTitle() {

return title;

}

public void setTitle(String title) {

this.title = title;

}

public String getImgUrl() {

return imgUrl;

}

public long getTime() {

return time;

}

public void setTime(long time) {

this.time = time;

}

public void setImgUrl(String imgUrl) {

this.imgUrl = imgUrl;

}

public String getDesc() {

return desc;

}

public void setDesc(String desc) {

this.desc = desc;

}

@Override

public String toString() {

return “CacheBean{” +

“name=’” + name + ‘’’ +

“, title=’” + title + ‘’’ +

“, time=” + time +

“, imgUrl=’” + imgUrl + ‘’’ +

“, desc=’” + desc + ‘’’ +

‘}’;

}

}

  • 序列化和反序列化工具

package cn.yumakeji.jetpackroomstudy.room;

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

public class RoomManager {

//反序列,把二进制数据转换成java object对象

public static Object toObject(byte[] data) {

ByteArrayInputStream bais = null;

ObjectInputStream ois = null;

try {

bais = new ByteArrayInputStream(data);

ois = new ObjectInputStream(bais);

return ois.readObject();

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

if (bais != null) {

bais.close();

}

if (ois != null) {

ois.close();

}

} catch (Exception ignore) {

ignore.printStackTrace();

}

}

return null;

}

//序列化存储数据需要转换成二进制

public static byte[] toByteArray(T body) {

ByteArrayOutputStream baos = null;

ObjectOutputStream oos = null;

try {

baos = new ByteArrayOutputStream();

oos = new ObjectOutputStream(baos);

oos.writeObject(body);

oos.flush();

return baos.toByteArray();

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

if (baos != null) {

baos.close();

}

if (oos != null) {

oos.close();

}

} catch (Exception e) {

e.printStackTrace();

}

}

return new byte[0];

}

}

  • 使用

在这里插入图片描述

/**

  • 增加对象数据库

  • @param view

*/

public void onInsertDataClick(View view) {

for (int i = 0; i < 5; i++) {

CacheBean cacheBean = new CacheBean();

cacheBean.setName(“huang” + i);

cacheBean.setTime(System.currentTimeMillis());

cacheBean.setTitle(“测试数据” + i);

cacheBean.setDesc(“这是偶的描述信息” + i);

cacheBean.setImgUrl(“https://blog.csdn.net/huangxiaoguo1”);

Cache cache = new Cache();

cache.key = cacheBean.getName();

//序列化存储数据需要转换成二进制

cache.data = RoomManager.toByteArray(cacheBean);

RoomDataBase.get().getCacheDao().第四章:Room数据库直接存储对象(二进制数据,Android面试必问的HashMap_第1张图片
insert(cache);

}

}

/**

  • 删除对象数据库

  • @param view

*/

public void onDeleteDataClick(View view) {

Cache cache = RoomDataBase.get().getCacheDao().findByKey(“huang2”);

if (cache == null) {

return;

}

RoomDataBase.get().getCacheDao().delete(cache);

}

/**

  • 更新对象数据库

  • @param view

*/

public void onUpdataDataClick(View view) {

Cache cache = RoomDataBase.get().getCacheDao().findByKey(“huang3”);

if (cache == null) {

return;

}

CacheBean cacheBean = (CacheBean) RoomManager.toObject(cache.data);

if (cacheBean == null) {

Log.e(“TTT”, “获取到的数据为空”);

return;

}

cacheBean.setTitle(“我被修改了”);

cache.data = RoomManager.toByteArray(cacheBean);

RoomDataBase.get().getCacheDao().update(cache);

}

/**

  • 获取所有对象数据

  • @param view

*/

public void onFindAllDataClick(View view) {

List lists = RoomDataBase.get().getCacheDao().findAll();

for (Cache list : lists) {

Log.e(“TTT”, “key:” + list.key + “,data:” + ((CacheBean) RoomManager.toObject(list.data)).toString());

}

}

/**

  • 一对一所有对象数据

  • @param view

*/

public void onFindOneDataClick(View view) {

Cache cache = RoomDataBase.get().getCacheDao().findByKey(“huang3”);
@param view

*/

public void onFindAllDataClick(View view) {

List lists = RoomDataBase.get().getCacheDao().findAll();

for (Cache list : lists) {

Log.e(“TTT”, “key:” + list.key + “,data:” + ((CacheBean) RoomManager.toObject(list.data)).toString());

}

}

/**

  • 一对一所有对象数据

  • @param view

*/

public void onFindOneDataClick(View view) {

Cache cache = RoomDataBase.get().getCacheDao().findByKey(“huang3”);

你可能感兴趣的:(程序员,架构,移动开发,android)