首页 > 解决方案 > 使用与 Spring Boot 存储库相同的存储库方法返回不同的 Dto

问题描述

我有两个 DTO 一个用于获取方法,例如“/pessoas/{id}”,另一个用于“/pessoas/{id}/detalhes”,我将在其中看到 Pessoa 的更多属性。

我的代码在 Kotlin 中。

我的简单 DTO:

interface PessoaDTO {
    val idInstitucional: UUID?
    val nome: String?
}

data class PessoaDTOImpl(override val idInstitucional: UUID?, override val nome: String?): PessoaDTO

我的 DTO 详细信息:

interface PessoaDetalhesDTO {
    val idInstitucional: UUID?
    val nome: String?
    val email: String?
    val telefone: String?
    val cpf: Long?
}

data class PessoaDetalhesDTOImpl(override val idInstitucional: UUID?, override val nome: String?, override val email: String?, override val telefone: String?, override val cpf: Long?): PessoaDetalhesDTO

我有一个将由我的 PessoaController 访问的存储库。我正在考虑在我的存储库中使用两种方法,每种方法用于不同的 DTO。

那是我的存储库:

internal interface PessoaRepository : CrudRepository<Pessoa, Long>, JpaSpecificationExecutorWithProjection<Pessoa> {
    fun findByIdInstitucional(idInstitucional: UUID): PessoaDTO?
    fun findByIdInstitucional(idInstitucional: UUID): PessoaDetalhesDTO?
}

但是,对于不同的返回数据类型,我不能在存储库中有两个同名的函数。我该如何处理,而不必为 Pessoa 的详细信息创建另一个存储库?

标签: spring-bootjpakotlin

解决方案


推荐阅读