gpt4 book ai didi

android - 为 greenDao 和改造创建通用数据模型

转载 作者:太空狗 更新时间:2023-10-29 13:12:04 25 4
gpt4 key购买 nike

我有来自这样的网络服务的 json 响应

{
"id":1,
"name":"abc",
"address": {
"streetName":"cde",
"city":NY
}
}

我正在为我的项目使用 Retrofit 和 greenDao。对于两者,我们都需要一个数据模型。仅用于改造,我的数据模型如下所示

public class Example {

private Integer id;
private String name;
private Address address;

public Example() {
}

public Example(Integer id, String name, Address address) {
this.id = id;
this.name = name;
this.address = address;
}

public Integer getId() {
return id;
}


public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}


public Address getAddress() {
return address;
}


public void setAddress(Address address) {
this.address = address;
}

}




public class Address {

private String streetName;
private String city;

public Address() {
}

public Address(String streetName, String city) {
this.streetName = streetName;
this.city = city;
}


public String getStreetName() {
return streetName;
}


public void setStreetName(String streetName) {
this.streetName = streetName;
}


public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

}

可以用于Retrofit,但是greenDao有一个生成器,也可以生成数据模型。我如何从 greenDao generator project 生成 Datamodel ,它可以用于 retrofit 和 greenDao

提前致谢

最佳答案

生成器应该是这样的:

    Schema schema = new Schema(1, <your.package.id.where.models.are.held>);

Entity model = schema.addEntity("Model");
model.addIdProperty();
model.addStringProperty("name");

Entity address = schema.addEntity("Address");
Property addressIdProperty = address.addIdProperty().getProperty();
address.addStringProperty("streetName");
address.addStringProperty("city");

model.addToOne(address, addressIdProperty).setName("address");

DaoGenerator generator = new DaoGenerator();
generator.generateAll(schema, "./app/src/main/java");

如您所见,Model 类包含对地址的引用,通过添加一对一关系并将其命名为“地址”。

这样任何 json 解析器,如 GSON可以解析您的对象并将它们放入数据库。

关于android - 为 greenDao 和改造创建通用数据模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39325195/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com