【问题标题】:MapStruct 3 Entity 1 DTOMapStruct 3 实体 1 DTO
【发布时间】:2020-09-04 08:36:08
【问题描述】:

我需要一个包含 5 列、desc、voice、startDate、endDate、flag 的 DTO。

来自 NatureEntity 的描述, 来自 VoiceEntity 的声音, 其他三个来自 QwertyEntity

我不知道如何在同一个 JSON 中检索此信息。

自然实体

@Data
@EqualsAndHashCode(callSuper=false)
@NoArgsConstructor
@Entity
@Table(name="NATURE")
public class NatureEntity {

    @Id 
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="SEQ_NATURE", unique=true, nullable=false, precision=15)
    private Long seqNature;

    @Column(name="DESC", nullable=false, length=150)
    private String desc;

    @OneToMany(mappedBy="fkNature")
    private List<VoiceEntity> VoiceAssociations;

}

语音实体

@Data
@EqualsAndHashCode(callSuper=false)
@NoArgsConstructor
@Entity
@Table(name="VOICE")
public class VoiceEntity {

    @Id 
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="SEQ_VOICE", unique=true, nullable=false, precision=15)
    private Long seqVoice;

    @Column(name="VOICE", nullable=false, length=255)
    private String voice;

    @ManyToOne
    @JoinColumn(name="FK_NATURE", nullable=false)
    private NatureEntity fkNature;

    @OneToMany(mappedBy="fkVoice")
    private List<QwertyEntity> qwertyAssociations;

}

QwertyEntity

@Data
@EqualsAndHashCode(callSuper=false)
@NoArgsConstructor
@Entity
@Table(name="QWERTY")
public class QwertyEntity {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="SEQ_QWERTY",unique=true, nullable=false, precision=15)
    private Long seqQwerty;

    @ManyToOne
    @JoinColumn(name="FK_VOICE", nullable=false)
    private VoiceEntity fkVoice;

    @Column(name="FLAG", nullable=true, length=1)
    private String flag;

    @Temporal(TemporalType.DATE)
    @Column(name="START_DATE", nullable=false)
    private Date startDate;

    @Temporal(TemporalType.DATE)
    @Column(name="END_DATE", nullable=false)
    private Date endDate;

}

DTO

@Data
@EqualsAndHashCode(callSuper=false)
@NoArgsConstructor
public class AbcDTO {

    private String desc;
    private String voice;
    private Date startDate;
    private Date endDate;
    private String flag;

}

映射器:

来自 NatureEntity 的描述 来自 VoiceEntity 的声音 来自 QwertyEntity 的 startDate、endDate 和标志

@Mapper(componentModel = "spring")
public interface AbcMapper {

       @Mapping(source = "desc", target = "desc")
       @Mapping(source = "voice", target = "voice")
       @Mapping(source = "startDate", target = "startDate")
       @Mapping(source = "endDate", target = "endDate")
       @Mapping(source = "flag", target = "flag")
       AbcDTO from(QwertyEntity qwerty);

}

存储库

我必须创建一个带有三个可选参数的查询。

@Repository
public interface QwertyEntityRepository extends JpaRepository<QwertyEntity, Long> {

    @Query("SELECT q FROM QwertyEntity q WHERE (:desc is null or q.desc = :desc) and (:startDate is null"
            + " or q.startDate = :startDate) and (:endDate is null or q.endDate = :endDate)")
    List <QwertyEntity> findByDescAndStartDateAndEndDate(@Param("desc") String desc, @Param("startDate") Date startDate, @Param("endDate") Date endDate);

}

服务

@Service
public interface AbcService {

    public List<AbcDTO> findByDescAndStartDateAndEndDate(String desc, Date startDate, Date endDate);

}

服务实现

@Component
public class AbcServiceImpl implements AbcService {

    @Autowired
    private AbcMapper abcMapper;
    @Autowired
    private QwertyEntityRepository qwertyEntityRepository;

    @Override
    @Transactional
    public List<AbcDTO> findByDescAndStartDateAndEndDate(String desc, Date startDate, Date endDate) {
        List<AbcDTO> dtoList = new ArrayList<>();
        List<QwertyEntity> qwertyList = qwertyEntityRepository.findByDescAndStartDateAndEndDate(desc, startDate, endDate);
        for(QwertyEntity qwerty : qwertyList) {
            dtoList.add(abcMapper.from(qwerty);
        }
        return dtoList;
    }

}

REST 控制器

@RestController
@RequestMapping("/services")
public class AbcRestController {

    @Autowired
    private AbcService abcService;

    @GetMapping(value = "/abc", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public List<AbcDTO> getAbc(@PathVariable String desc, @PathVariable Date startDate, @PathVariable Date endDate){
        return abcService.findByDescAndStartDateAndEndDate(desc, endDate, endDate);
    }

}

【问题讨论】:

    标签: java spring hibernate mapstruct


    【解决方案1】:

    您可以添加其他“实体”对象作为映射方法的参数,例如

    @Mapper(componentModel = "spring",
        unmappedTargetPolicy = ReportingPolicy.IGNORE)
    public interface AbcMapper {
    
        @Mapping(source = "nature.desc", target = "desc")
        @Mapping(source = "voice.voice", target = "voice")
        @Mapping(source = "qwerty.startDate", target = "startDate")
        @Mapping(source = "qwerty.endDate", target = "endDate")
        @Mapping(source = "qwerty.flag", target = "flag")
        AbcDTO from(QwertyEntity qwerty, NatureEntity nature, VoiceEntity voice);
    
    }
    

    * unmappedTargetPolicy = ReportingPolicy.IGNORE 排除未映射的属性

    【讨论】:

    • 它的 qwerty.getFkVoice() 不为空?您可以使用abcMapper.from(qwerty, qwerty.getFkVoice(), qwerty.getFkVoice().getFkNature()) 之类的方式调用映射器。还记得你可以有一个映射器的自定义实现,在接口上创建一个默认方法。
    猜你喜欢
    • 2017-09-17
    • 1970-01-01
    • 2020-05-14
    • 1970-01-01
    • 2020-10-06
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多