简体   繁体   English

我们如何在 CrudRepository 中实现自定义查找方法以仅从数据库中获取特定列?

[英]How can we implement custom find methods in CrudRepository to get only specific columns from a database?

I have to execute a SQL query in my repository:我必须在我的存储库中执行 SQL 查询:

public interface UserRequestResponseRepository extends JpaRepository<UserRequestResponse, Integer> {
    //public static final String FIND_QUERY = "select user.u_httpstatus ,user.u_queryparam from UserRequestResponse user";
    public static final String FIND_QUERY = 
            "select new com.abc.datacollection.entity.UserRequestResponse(user.u_httpstatus ,user.u_queryparam) from UserRequestResponse user";
    @Query(value = FIND_QUERY)
    public List<UserProjection> getAllRequestResponseRecords();
}

where UserProjection is a projection that I defined:其中 UserProjection 是我定义的投影:

public interface UserProjection {
    String getU_httpstatus();
    String getU_queryparam();
}

The class userRequestResponse has more fields than u_httpstatus and u_queryparam, but I want only these 2 fields in my response. class userRequestResponse 的字段比 u_httpstatus 和 u_queryparam 多,但我的响应中只需要这两个字段。

public @ResponseBody List<UserRequestResponse> getAllRequestResponseRecords() {
    return userRequestResponseRepository.findAll() ;
}

how do I modify the above code (findAll()) to get results from my custom query and not the results from the default CrudRepository findAll() (which returns all the fields).如何修改上述代码 (findAll()) 以从我的自定义查询中获取结果,而不是从默认 CrudRepository findAll() (返回所有字段)中获取结果。

First you don't need to add a @Query to make projections work.首先,您不需要添加@Query来进行预测。 Just having the UserProjection as the return type of the method in repository should be enough.只需将UserProjection作为存储库中方法的返回类型就足够了。 More about this here更多关于这里

Secondly, you can just have the following method in your repository as a projection-based findAll method;其次,您可以在存储库中使用以下方法作为基于投影的 findAll 方法;

public List<UserProjection> findAllProjectedBy();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何实现CrudRepository的自定义方法? - How to implement custom methods for CrudRepository? 如何在 Spring 中仅实现 CrudRepository 的特定方法? - How to implement only specific method of CrudRepository in Spring? 如何在翻新中实施自定义缓存(仅适用于特定方法) - How to implement custom Caching in Retrofit (only for specific methods) 为什么我们不需要在Spring Boot应用程序中实现CrudRepository方法? - Why do we not need to implement CrudRepository methods in spring boot applications? "如何使用 CrudRepository 从数据库中获取“现在时间”?" - How to get 'now time' from database using CrudRepository? 我们如何利用自定义的@Qualifier 方法? - How can we utilize the custom @Qualifier methods? Spring boot 2.0如何实现CrudRepository的自定义方法? - How to implement custom method of CrudRepository in Spring boot 2.0? 如何仅从 json 中获取我们想要的特定数据 - how to get only specific data we want from json 如何从数据库到另一布局获取特定行的所有列? - How can I get all columns for a specific row from the database to another layout? 如果我们在接口中添加方法,我们如何在多个类中实现方法 - how can we implement methods in multiples classes if we add methods in interface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM