Spring JPA 选择特定列 [英] Spring JPA selecting specific columns

查看:34
本文介绍了Spring JPA 选择特定列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Spring JPA 来执行所有数据库操作.但是我不知道如何从 Spring JPA 的表中选择特定的列?

I am using Spring JPA to perform all database operations. However I don't know how to select specific columns from a table in Spring JPA?

例如:
SELECT projectId, projectName FROM projects

推荐答案

您可以在 Repository@Query 注释中设置 nativeQuery = true代码>这样的类:

You can set nativeQuery = true in the @Query annotation from a Repository class like this:

public static final String FIND_PROJECTS = "SELECT projectId, projectName FROM projects";

@Query(value = FIND_PROJECTS, nativeQuery = true)
public List<Object[]> findProjects();

请注意,您必须自己进行映射.除非您真的只需要这两个值,否则使用这样的常规映射查找可能更容易:

Note that you will have to do the mapping yourself though. It's probably easier to just use the regular mapped lookup like this unless you really only need those two values:

public List<Project> findAll()

Spring 数据可能值得一看 文档.

It's probably worth looking at the Spring data docs as well.

这篇关于Spring JPA 选择特定列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆