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

例如: SELECT projectName FROM projects


当前回答

在本地sql中可以指定null作为字段值。

@Query(value = "select p.id, p.uid, p.title, null as documentation, p.ptype " +
            " from projects p " +
            "where p.uid = (:uid)" +
            "  and p.ptype = 'P'", nativeQuery = true)
Project findInfoByUid(@Param("uid") String uid);

其他回答

Use:

@Query("SELECT e FROM #{#entityName} e where e.userId=:uid")
List<ClienteEnderecoEntity> findInfoByUid(@Param("uid") UUID uid);

使用本机查询:

Query query = entityManager.createNativeQuery("SELECT projectId, projectName FROM projects");
List result = query.getResultList();

你可以使用@jombie给出的答案,并且:

将接口放在实体类之外的单独文件中; 是否使用本机查询(选择取决于您的需要); 不要为此重写findAll()方法,而是使用自己选择的名称; 记得用你的新接口返回一个参数化的List(例如List<SmallProject>)。

您可以在存储库接口类中应用下面的代码。

Entityname表示您的数据库表名,例如projects。 列表意味着项目是项目中的实体类。

@Query(value="select p from #{#entityName} p where p.id=:projectId and p.projectName=:projectName")

List<Project> findAll(@Param("projectId") int projectId, @Param("projectName") String projectName);

你可以更新你的JPARepository,如下所示。

@Query("select u.status from UserLogin u where u.userId = ?1 or u.email = ?1 or u.mobile = ?1")
public UserStatus findByUserIdOrEmailOrMobile(String loginId);

UserStatus为Enum

public enum UserStatus
{
    New,
    Active,
    Deactived,
    Suspended,
    Locked
}