[Android] Android GreenDao 保存 JavaBean 或者List 类型数据

时间:2022-07-30 22:22:02

Android GreenDao 保存 JavaBean 或者List <JavaBean>类型数据

简介

数据库存储数据基本上每个APP都有用到,GreenDAO 是一个将对象映射到 SQLite 数据库中的轻量且快速的 ORM 解决方案,至于用法这里不再多少,网上有很资料,这里主要说一下,利用GreenDao保存JavaBean类型或者List类型的数据。

举个栗子

{
rating: {
max: ,
average: 7.9,
stars: "",
min:
},
genres: [
"动作",
"奇幻",
"冒险"
],
title: "海王",
casts: [
{
alt: "https://movie.douban.com/celebrity/1022614/",
avatars: {
small: "http://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p32853.webp",
large: "http://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p32853.webp",
medium: "http://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p32853.webp"
},
name: "杰森·莫玛",
id: ""
},
{
alt: "https://movie.douban.com/celebrity/1044702/",
avatars: {
small: "http://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p34697.webp",
large: "http://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p34697.webp",
medium: "http://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p34697.webp"
},
name: "艾梅柏·希尔德",
id: ""
},
{
alt: "https://movie.douban.com/celebrity/1010539/",
avatars: {
small: "http://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p9206.webp",
large: "http://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p9206.webp",
medium: "http://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p9206.webp"
},
name: "威廉·达福",
id: ""
}
],
collect_count: ,
original_title: "Aquaman",
subtype: "movie",
directors: [
{
alt: "https://movie.douban.com/celebrity/1032122/",
avatars: {
small: "http://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1509950363.8.webp",
large: "http://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1509950363.8.webp",
medium: "http://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1509950363.8.webp"
},
name: "温子仁",
id: ""
}
],
year: "",
images: {
small: "http://img3.doubanio.com/view/photo/s_ratio_poster/public/p2541280047.webp",
large: "http://img3.doubanio.com/view/photo/s_ratio_poster/public/p2541280047.webp",
medium: "http://img3.doubanio.com/view/photo/s_ratio_poster/public/p2541280047.webp"
},
alt: "https://movie.douban.com/subject/3878007/",
id: ""
}

上面是豆瓣电影一个电影详情的数据,一般复杂,有一些套嵌JsonObject和JsonArray,数据库存储的都是基本类型,像上面那些objec或者array类型又改如何存到数据库中,这是个问题。GreenDao为我们提了一个解决方案,就是把这些数据统一都转成String类型,存到数据库中,用的时候再转成相应的类型。

具体分析

这里分别列数两个例子,一个是JsonObject, 一个是JsonArray的。
首先第一个字段rating,电影的评分,返回的是一个JsonObject,写出它的JavaBean

本文参考:

https://www.jianshu.com/p/9b817a62eb22

https://blog.csdn.net/zxm317122667/article/details/73528387

https://www.jianshu.com/p/5d6de7cda713

本博客地址: wukong1688

本文原文地址:https://www.cnblogs.com/wukong1688/p/10748077.html

转载请著名出处!谢谢~~