拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 将DTO映射到物体,如果您的所有栏位为空,则将物体留空

将DTO映射到物体,如果您的所有栏位为空,则将物体留空

白鹭 - 2022-01-24 1976 0 0

我的物体是回传栏位空示例:EntityTest{id=null, name=null}

但我需要如果所有栏位都为空回传 EntityTest = null

测验物体

@Entity
@Table(name="TestEntity")
public class TestEntity {
    @Id
    @Column(name="id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @Column(name="test_name")
    private String testName;
}

测验物体Dto

public class TestEntityDto {

    private long id;
    private String test_name;
}

测验物体映射器

@Mapper
public interface TestEntityMapper {
    TestEntity TestEntityDTOToTestEntity(TestEntityDTO testEntityDTO);

实施

testEntityDTO.setId(null);
testEntityDTO.setNameTest(null);
TestEntity testEntity = TestEntityMapper.TestEntityDTOToTestEntity(testEntityDTO);

实际结果:

EntityTest{id=null, name=null}

预期结果:

EntityTest = null

uj5u.com热心网友回复:

我建议只执行 toString 来检查所有 null 情况:

toString(){"id:" id ",name:" name;}

因此,如果这回传 "id:null,name:null" 那么您可以确定它为 null 并在检查等于后回传 null

标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *