gpt4 book ai didi

java - 推土机 : String-To-Date Field Level Mapping for a List

转载 作者:行者123 更新时间:2023-12-01 12:02:54 26 4
gpt4 key购买 nike

我想将DTO(均为字符串数据类型)映射到VO(包含String、int、boolean、Date)

学生DTO

private StudentDetailDTO student;

学生详细信息DTO:

private String sid;
private String name;
private String createDt;
private String studentInd;
private List<FeeReceiptDTO> feeDetails;

费用收据DTO:

private String semisterNum;
private String feeAmount;
private String paidOn;

学生详细VO:

private int sid;
private String name;
private Date createDt;
private boolean studentInd;
private List<FeeReceiptVO> feeDetails;

费用收据VO:

private int semisterNum;
private Double feeAmount;
private Date paidOn;

我正在使用 DOZZER 将我的 DTO 映射到 VO String-to-Date-DozzerMapping

DozzerMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns="http://dozer.sourceforge.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://dozer.sourceforge.net http://dozer.sourceforge.net/schema/beanmapping.xsd">

<mapping date-format="MM/dd/yyyy HH:mm" map-null="true" map-empty-string="true" wildcard="true" type="one-way" >
<class-a>com.college.student.dto.StudentDTO</class-a>
<class-b>com.college.student.vo.StudentVO</class-b>
<field>
<a>student.sid</a>
<b>sid</b>
</field>
<field>
<a>student.name</a>
<b>name</b>
</field>
<field>
<a>student.createDt</a>
<b>createDt</b>
</field>
<field>
<a>student.studentInd</a>
<b>studentInd</b>
</field>
<field>
<a date-format="MM/dd/yyyy HH:mm">student.feeDetails</a>
<b>feeDetails</b>
</field>

</mapping>
</mappings>

但是,feeDetails 中的日期(即 paidOn)无法从字符串格式化为日期。

我做错了什么吗?

我需要为此 paidOn 字段编写一个 CustomStringToDateConvertor 吗?

错误日志:

ERROR [org.dozer.MappingProcessor] (http-localhost-127.0.0.1-9090-1) Field mapping error -->
MapId: null
Type: null
Source parent class: com.college.student.dto.StudentDTO
Source field name: paidOn
Source field type: class java.lang.String
Source field value: 01/01/2015 01:01
Dest parent class: com.college.student.vo.StudentVO
Dest field name: paidOn
Dest field type: java.util.Date: org.dozer.converters.ConversionException: Unable to determine time in millis of source object
at org.dozer.converters.DateConverter.convert(DateConverter.java:81) [dozer-5.4.0.jar:]
at org.dozer.converters.PrimitiveOrWrapperConverter.convert(PrimitiveOrWrapperConverter.java:70) [dozer-5.4.0.jar:]

我尝试调试,发现该日期的 dateFormat 在 Dozzer 的日期转换器中为 null org.dozer.converters.DateConvertor

我在字段级别定义了日期格式,但它对对象的List没有影响。

还有其他方法可以做到这一点吗?

最佳答案

As,Dozer 无法转换String-To-Date - 列表的字段级映射

我为列表定义了一个新的映射,即将其视为一个对象

我更改了DozerMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<mappings xmlns="http://dozer.sourceforge.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://dozer.sourceforge.net http://dozer.sourceforge.net/schema/beanmapping.xsd">

<mapping date-format="MM/dd/yyyy HH:mm" map-null="true" map-empty-string="true" wildcard="true" type="one-way" >
<class-a>com.college.student.dto.FeeReceiptDTO</class-a>
<class-b>com.college.student.vo.FeeReceiptVO</class-b>
</mapping>

<mapping date-format="MM/dd/yyyy HH:mm" map-null="true" map-empty-string="true" wildcard="true" type="one-way" >
<class-a>com.college.student.dto.StudentDTO</class-a>
<class-b>com.college.student.vo.StudentVO</class-b>
<field>
<a>student.sid</a>
<b>sid</b>
</field>
<field>
<a>student.name</a>
<b>name</b>
</field>
<field>
<a>student.createDt</a>
<b>createDt</b>
</field>
<field>
<a>student.studentInd</a>
<b>studentInd</b>
</field>
<field>
<a>student.feeDetails</a>
<b>feeDetails</b>
</field>

</mapping>
</mappings>

关于java - 推土机 : String-To-Date Field Level Mapping for a List,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27864640/

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