【问题标题】:Plain POJO without any annotations in AWS DynamoDBAWS DynamoDB 中没有任何注释的普通 POJO
【发布时间】:2019-05-04 08:26:16
【问题描述】:

如何使用没有任何注释的普通 POJO 类在 DynamoDB 中创建表?我还必须对它们执行基本的 CRUD 操作。

`@DynamoDBTable(tableName="ProductCatalog") 公共类 CatalogItem {

private Integer id;
private String title;
private String ISBN;
private Set<String> bookAuthors;
private String someProp;

@DynamoDBHashKey(attributeName="Id")  
public Integer getId() { return id; }
public void setId(Integer id) {this.id = id; }

@DynamoDBAttribute(attributeName="Title")  
public String getTitle() {return title; }
public void setTitle(String title) { this.title = title; }

@DynamoDBAttribute(attributeName="ISBN")  
public String getISBN() { return ISBN; }
public void setISBN(String ISBN) { this.ISBN = ISBN; }

@DynamoDBAttribute(attributeName="Authors")
public Set<String> getBookAuthors() { return bookAuthors; }
public void setBookAuthors(Set<String> bookAuthors) { this.bookAuthors = bookAuthors; }
`

在上面的代码中,我需要我的 POJO 没有任何注释并且仍然使用 dynamoDB。

【问题讨论】:

  • 嘿,答案对你有用吗?如果是这样,您应该接受它,以便其他 SO 用户知道它解决了问题。如果您需要调整一些事情或使用不同的解决方案,请发表评论或添加您自己的答案以与他人分享这些知识。

标签: amazon-dynamodb pojo aws-java-sdk


【解决方案1】:

DynamoDB 有一个可以帮助您的中级 SDK。它被称为Document Interface。这个接口的主要部分是TableItem 对象。

现在,要获得一个项目,您可以手动构建一个(但您不想这样做),或者您可以使用 Item.fromJson(String) 从一个 json blob 构建一个。现在剩下的就是让您使用您最喜欢的序列化程序将您的 java 数据模型转换为 json。

TLDR;

Pojo --> json --> Item

【讨论】:

    猜你喜欢
    • 2019-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多