【问题标题】:How can ı get just specific variables in spring?我怎样才能在春天得到特定的变量?
【发布时间】:2019-06-05 08:43:40
【问题描述】:

例如,当我想获取它们的属性但不是全部时。例如,当我使用 findAll();功能 - 它显示所有内容,但我想例如 idname 有什么特殊功能或任何东西可以做到这一点?

认为我有一个这样的实体类。

     @Id
     private int api_id;

     @Column(name = "id")
     private int id;

     @Column(name = "name")
     private String name;

     @Column(name = "city")
     private String city;

     @Column(name = "founded_at")
     private int founded_at; //I declare as a integer because the variable consists of 4 integers.

     @Column(name = "web_page")
     private String web_page;

【问题讨论】:

    标签: rest api spring-mvc post


    【解决方案1】:

    您可以使用spring-data 的基于界面的投影从数据库中选择特定字段:

    interface NameAndIdOnly {
        int getId();
        String getName();
    }
    

    在您的存储库中:

    ...
    List<NameAndIdOnly> findAllWithNameAndId();
    ...
    

    这将是select id, name from &lt;table&gt;

    请阅读Projections了解更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多