GreenDao
https://www.jianshu.com/p/53083f782ea2
简介
数据库存储数据基本上每个APP都有用到,GreenDAO 是一个将对象映射到 SQLite 数据库中的轻量且快速的 ORM 解决方案,至于用法这里不再多少,网上有很资料,这里主要说一下,利用GreenDao保存JavaBean类型或者List类型的数据。
举个栗子
{
rating: {
max: 10,
average: 7.9,
stars: "40",
min: 0
},
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: "1022614"
},
{
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: "1044702"
},
{
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: "1010539"
}
],
collect_count: 397576,
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: "1032122"
}
],
year: "2018",
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: "3878007"
}
上面是豆瓣电影一个电影详情的数据,一般复杂,有一些套嵌JsonObject和JsonArray,数据库存储的都是基本类型,像上面那些objec或者array类型又改如何存到数据库中,这是个问题。GreenDao为我们提了一个解决方案,就是把这些数据统一都转成String类型,存到数据库中,用的时候再转成相应的类型。
具体分析
这里分别列数两个例子,一个是JsonObject, 一个是JsonArray的。
首先第一个字段rating,电影的评分,返回的是一个JsonObject,写出它的JavaBean
package com.zs.project.bean.movie;
import java.io.Serializable;
/**
* Created by zs
* Date:2018年 01月 17日
* Time:15:20
* —————————————————————————————————————
* About: 电影评分
* —————————————————————————————————————
*/
public class MovieRating implements Serializable {
private int max;
private float average;
private int stars;
private int min;
public int getMax() {
return max;
}
public void setMax(int max) {
this.max = max;
}
public float getAverage() {
return average;
}
public void setAverage(float average) {
this.average = average;
}
public int getStars() {
return stars;
}
public void setStars(int stars) {
this.stars = stars;
}
public int getMin() {
return min;
}
public void setMin(int min) {
this.min = min;
}
@Override
public String toString() {
return "MovieRating{" +
"max=" + max +
", average=" + average +
", stars=" + stars +
", min=" + min +
'}';
}
}
然后,写它的转换类MovieRatingConvert, 这里是用到的fastJson解析的,用Gson当然也是可以的,这个转换类实现的PropertyConverter接口,里面有两个方法,很好理解,String转JavaBean 和JavaBean转String。
package com.zs.project.greendao.convert;
import com.alibaba.fastjson.JSON;
import com.zs.project.bean.movie.MovieRating;
import org.greenrobot.greendao.converter.PropertyConverter;
/**
* Created by zs
* Date:2018年 12月 10日
* Time:17:05
* —————————————————————————————————————
* About: 评分
* —————————————————————————————————————
*/
public class MovieRatingConvert implements PropertyConverter<MovieRating, String>{
@Override
public MovieRating convertToEntityProperty(String databaseValue) {
return JSON.parseObject(databaseValue , MovieRating.class);
}
@Override
public String convertToDatabaseValue(MovieRating entityProperty) {
return JSON.toJSONString(entityProperty);
}
}
下面是电影的主演casts字段,返回的是一个JsonArray,同样是写它的JavaBean和转换类MovieCastsConvert
package com.zs.project.bean.movie;
import java.io.Serializable;
/**
* Created by zs
* Date:2018年 01月 17日
* Time:15:17
* —————————————————————————————————————
* About: 主演 | 导演
* —————————————————————————————————————
*/
public class MovieCasts implements Serializable{
private String alt;
private MovieImages avatars;
private String name;
private String id;
public String getAlt() {
return alt;
}
public void setAlt(String alt) {
this.alt = alt;
}
public MovieImages getAvatars() {
return avatars;
}
public void setAvatars(MovieImages avatars) {
this.avatars = avatars;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Override
public String toString() {
return "MovieCasts{" +
"alt='" + alt + '\'' +
", avatars=" + avatars +
", name='" + name + '\'' +
", id='" + id + '\'' +
'}';
}
}
package com.zs.project.greendao.convert;
import com.alibaba.fastjson.JSON;
import com.zs.project.bean.movie.MovieCasts;
import org.greenrobot.greendao.converter.PropertyConverter;
import java.util.List;
/**
* Created by zs
* Date:2018年 12月 10日
* Time:17:07
* —————————————————————————————————————
* About:
* —————————————————————————————————————
*/
public class MovieCastsConvert implements PropertyConverter<List<MovieCasts>, String>{
@Override
public List<MovieCasts> convertToEntityProperty(String databaseValue) {
return JSON.parseArray(databaseValue , MovieCasts.class);
}
@Override
public String convertToDatabaseValue(List<MovieCasts> entityProperty) {
return JSON.toJSONString(entityProperty);
}
}
这些都写完了,就是把他们都用在整个详情的Bean里面了。
@Id(autoincrement = true)
private Long id;
private String title;
@Convert(converter = MovieRatingConvert.class , columnType = String.class)
private MovieRating rating;
private String year;
@Convert(converter = MovieImagesConvert.class , columnType = String.class)
private MovieImages images;
private String alt;
private String mobile_url;
private String share_url;
private String schedule_url;
private String collect_count;
private String comments_count;
private String ratings_count;
private String original_title;
private String summary;
@Convert(converter = StringConvert.class , columnType = String.class)
private List<String> countries;
@Convert(converter = StringConvert.class , columnType = String.class)
private List<String> genres;
@Convert(converter = StringConvert.class , columnType = String.class)
private List<String> aka;
@Convert(converter = MovieCastsConvert.class , columnType = String.class)
private List<MovieCasts> casts;
@Convert(converter = MovieCastsConvert.class , columnType = String.class)
private List<MovieCasts> directors;
@Convert(converter = MovieRatingConvert.class , columnType = String.class)
private MovieRating rating;
@Convert(converter = MovieCastsConvert.class , columnType = String.class)
private List<MovieCasts> casts;
用到了这个Convert注解,表明它们的转换类,这样就可以转换成String保存到数据库中了