1.实现方式
主要是通过greenDao 提供的PropertyConverter,gson,转换对象到字符串存到数据库。
2.Converent实现
public class MyObjectConverent implements PropertyConverter<List<MyObjectItem>,String> {
private final Gson mGson;
public FreeCuttingCLJCConverent() {
mGson = new Gson();
}
@Override
public List<MyObjectItem> convertToEntityProperty(String databaseValue) {
Type type = new TypeToken<ArrayList<MyObjectItem>>() {
}.getType();
ArrayList<MyObjectItem> itemList= mGson.fromJson(databaseValue, type);
return itemList;
}
@Override
public String convertToDatabaseValue(List<MyObjectItem> entityProperty) {
String dbString = mGson.toJson(entityProperty);
return dbString;
}
}
2.ORM实体实现
@Entity
public class MyObjectEntity{
@Id
public Long tableId;
@Convert(/**指定转换器 **/converter = MyObjectConverent .class,/**指定数据库中的列字段**/columnType =String.class )
private List<MyObjectItem> mObjectList;
}
3.使用
//伪代码
//存的时候
MyObjectEntity.setObjectItem( new MyObjectItem());
//取的时候
MyObjectEntity.getObjectItem();
//就这么简单!!