[英]MapStruct 3 Entity 1 DTO
我需要一個包含 5 個列、desc、voice、startDate、endDate、flag 的 DTO。
來自 NatureEntity 的 desc,來自 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;
}
Qwerty實體
@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 的 desc 來自 VoiceEntity 的聲音 startDate、endDate 和來自 QwertyEntity 的標志
@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 Controller
@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);
}
}
您可以將其他“實體”對象添加為映射方法的參數,例如
@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
排除未映射的屬性
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.