java - 在 spring boot 中从 @Query View 返回 List<object>

标签 java mysql spring spring-boot

我正在使用 Spring Boot 。我想在 @Query 中应用 join 并在 List 中获取结果。

objDto.java

@ToString
@Setter
@Getter
@AllArgsConstructor
public class objDto {
    public int id;
    private String title;
}

ObjDomain.java

@ToString
@Getter
@Setter
@Entity
@NoArgsConstructor
public class ObjDomain implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public int id;
    public String title;
}

ShowDetailsRepository.java


@Repository
public interface ShowDetailsRepository extends JpaRepository<ObjDomain, Long> {
    @Query(value ="//JOIN QUERY HERE//", nativeQuery = true)
    List<ObjDto> showDetails();

}

当我在 MYSQL CLI 中使用它时,我的连接查询运行良好。

Error : No converter found capable of converting from type [org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap] to type [com.example.com.data.transfer.objects.ObjDto]

Expected Result : [{id: "1", title: "DEF"}]

最佳答案

您可以创建一个接口(interface)投影而不是 DTO,并且在该接口(interface)中您只能拥有 getter 方法并将该接口(interface)作为 List 类型传递。

@Query(value ="//JOIN QUERY HERE//", nativeQuery = true)
List<YourInterface> showDetails();

您的界面将如下所示:

public interface YourInterface {
     int getid();
    String gettitle();
}

并确保接口(interface)中的 getter 方法与值查询结果的顺序相同。

关于java - 在 spring boot 中从 @Query View 返回 List<object>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59169818/

相关文章:

spring - 构建器模式不返回 "this"

MySQL - Timestamp(8) 替代方案?

java - 如何自定义 Spring Boot 嵌入式 Tomcat 线程池?

java - 如何将日期格式化为文字

java - 删除未使用的文件后,Drawables 链接失败

python - 在 python 的侧面安装中安装 mysql-connector(除了默认的 python 安装)

mysql - 让客户创建他/她自己的自定义 MySQL 查询以从 vb.net 中的数据库获取数据

java - 数据库连接mysql和Spring框架

java - 将 float 负值转换为正值

java - Android - 阻止可绘制背景超出 View