You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java中如何将ExecuteStatementResult结果转为自定义对象列表?

解决思路与替代方案

直接强制转换List<Map<String, AttributeValue>>到List<PricingRule>行不通,因为两者属于完全不同的类型体系——前者元素是键值对集合,后者是自定义业务对象,Java无法自动完成跨类型的元素转换。以下是三种可行的实现方案:


方案1:手动映射(无依赖、可控性强)

先定义与DynamoDB字段匹配的PricingRule类,再遍历每个Map元素,手动提取AttributeValue中的值并赋值给对象字段。

1.1 定义PricingRule类

import java.time.LocalDate;

public class PricingRule {
    private String bookingClasses;
    private String suppliers;
    private String adjustmentType;
    private LocalDate departureStartDate;

    // 全参/无参构造器、getter、setter方法
    public PricingRule() {}

    public PricingRule(String bookingClasses, String suppliers, String adjustmentType, LocalDate departureStartDate) {
        this.bookingClasses = bookingClasses;
        this.suppliers = suppliers;
        this.adjustmentType = adjustmentType;
        this.departureStartDate = departureStartDate;
    }

    // 省略getter、setter
}

1.2 转换代码

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;

List<Map<String, AttributeValue>> items = executeStatementResult.getItems();
List<PricingRule> pricingRules = new ArrayList<>();

for (Map<String, AttributeValue> item : items) {
    PricingRule rule = new PricingRule();
    // 提取String类型字段
    rule.setBookingClasses(item.get("bookingClasses").getS());
    rule.setSuppliers(item.get("suppliers").getS());
    rule.setAdjustmentType(item.get("adjustmentType").getS());
    // 转换日期字段
    String dateStr = item.get("departureStartDate").getS();
    rule.setDepartureStartDate(LocalDate.parse(dateStr));
    
    pricingRules.add(rule);
}

注意:实际代码中需增加null值判断,避免NullPointerException(比如item.get(key)可能为null,或者getS()返回null)。


方案2:使用AWS SDK的DynamoDBMapper

如果项目已引入AWS SDK的DynamoDB模块,可以用DynamoDBMapper的注解式映射,更贴合AWS生态。

2.1 给PricingRule添加注解

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTypeConverted;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTypeConverter;
import java.time.LocalDate;

@DynamoDBTable(tableName = "YourPricingRuleTable") // 表名可按需填写,仅用于映射规则
public class PricingRule {
    private String bookingClasses;
    private String suppliers;
    private String adjustmentType;
    private LocalDate departureStartDate;

    @DynamoDBAttribute(attributeName = "bookingClasses")
    public String getBookingClasses() { return bookingClasses; }
    public void setBookingClasses(String bookingClasses) { this.bookingClasses = bookingClasses; }

    @DynamoDBAttribute(attributeName = "suppliers")
    public String getSuppliers() { return suppliers; }
    public void setSuppliers(String suppliers) { this.suppliers = suppliers; }

    @DynamoDBAttribute(attributeName = "adjustmentType")
    public String getAdjustmentType() { return adjustmentType; }
    public void setAdjustmentType(String adjustmentType) { this.adjustmentType = adjustmentType; }

    @DynamoDBAttribute(attributeName = "departureStartDate")
    @DynamoDBTypeConverted(converter = LocalDateConverter.class)
    public LocalDate getDepartureStartDate() { return departureStartDate; }
    public void setDepartureStartDate(LocalDate departureStartDate) { this.departureStartDate = departureStartDate; }

    // 自定义日期类型转换器
    public static class LocalDateConverter implements DynamoDBTypeConverter<String, LocalDate> {
        @Override
        public String convert(LocalDate source) {
            return source != null ? source.toString() : null;
        }

        @Override
        public LocalDate unconvert(String source) {
            return source != null ? LocalDate.parse(source) : null;
        }
    }
}

2.2 转换代码

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;

DynamoDBMapper mapper = new DynamoDBMapper(dynamoDB); // dynamoDB为你的AmazonDynamoDB实例
List<Map<String, AttributeValue>> items = executeStatementResult.getItems();
List<PricingRule> pricingRules = new ArrayList<>();

for (Map<String, AttributeValue> item : items) {
    PricingRule rule = mapper.marshallIntoObject(PricingRule.class, item);
    pricingRules.add(rule);
}

方案3:使用第三方对象映射库(如ModelMapper)

如果不想写重复的手动映射代码,可以用ModelMapper这类库自动完成字段转换,适合字段较多的场景。

3.1 添加依赖(Maven示例)

<dependency>
    <groupId>org.modelmapper</groupId>
    <artifactId>modelmapper</artifactId>
    <version>3.1.1</version>
</dependency>

3.2 转换代码

import org.modelmapper.ModelMapper;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;

ModelMapper modelMapper = new ModelMapper();
// 配置AttributeValue到String的转换规则
modelMapper.addConverter(context -> {
    AttributeValue source = context.getSource();
    return source != null ? source.getS() : null;
}, AttributeValue.class, String.class);
// 配置AttributeValue到LocalDate的转换规则
modelMapper.addConverter(context -> {
    AttributeValue source = context.getSource();
    return source != null && source.getS() != null ? LocalDate.parse(source.getS()) : null;
}, AttributeValue.class, LocalDate.class);

// 批量转换
List<Map<String, AttributeValue>> items = executeStatementResult.getItems();
List<PricingRule> pricingRules = items.stream()
    .map(item -> modelMapper.map(item, PricingRule.class))
    .collect(Collectors.toList());

内容的提问来源于stack exchange,提问作者Sikandar Sahab

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 22:35:22