【问题标题】:Springboot + JPA : List all fieldnames for each fieldtypeSpring Boot + JPA:列出每种字段类型的所有字段名称
【发布时间】:2020-05-20 16:52:59
【问题描述】:

我正在为我的项目使用 Spring Boot + JPA。我想要我表中每个 typefieldName 列表。所以我通过使用JPQL 使用带有命名参数的查询 来做到这一点。我在存储库中做了两种方法。

  1. 第一种方法:
    此方法用于获取 type 的不同列表。
      @Query(value = "SELECT DISTINCT fc.type FROM FieldCapabilityModel fc")
      List<String> findDistinctType();
  1. 第二种方法:
    此方法用于获取特定type 的名称列表。
     @Query("SELECT fc.fieldName FROM FieldCapabilityModel fc where fc.type=:type")
     List<String> findByType(@Param("type) String type);

这是我的service 组件。

@Service
public class FieldCapabilityService {

    @Autowired
    FieldCapabilityRepo fcRepo;

    public List<Map<String, Object>> getAllFieldAndType() { 
                // First Method is called here  
        return fcRepo.findDistinctType().stream().map(type -> {
                Map<String, Object> resultMap = new HashMap<String, Object>();
                resultMap.put("type", type);
                // Second Method is called here
                resultMap.put("field", fcRepo.findByType(type));
                return resultMap;
            }).collect(Collectors.toList());            
    }
}

当我使用休息控制器调用时,我的 API 响应是:

{
  "code": 200,
  "message": "Data Fetched",
  "data": [
    {
      "field": [
        "name.lname",
        "city",
        "name.fname",
        "name",
        "id"
      ],
      "type": "text"
    },
    {
      "field": [
        "_ignored"
      ],
      "type": "_ignored"
    },
    {
      "field": [
        "_seq_no"
      ],
      "type": "_seq_no"
    }
  ]
}

我得到了我想要的准确输出,而且这是一个简单的数据响应,我有超过 16 种类型。尽管我想知道这是一种有效且稳健的方式。
我的问题是:

我们可以为此使用任何其他查询吗?
如果可以,我们可以使用 @NamedQuery 代替这个,那为什么?
什么应该是这种执行的精简、健壮和高效的方法吗?

如果我想改进此解决方案的任何内容,请给我建议,或者提出比此解决方案更好的新解决方案。

【问题讨论】:

    标签: java spring-boot jpa spring-data-jpa


    【解决方案1】:

    您可以在一个查询中获取所有类型和字段名称

    @Query(value = "SELECT NEW org.apache.commons.math3.util.Pair(fc.type as type, fc.fieldName as name) FROM FieldCapabilityModel fc")
      public List<Pair<Integer, Integer>> findAllTypeWithName();
    

    然后汇总数据。创建 Key 为 fieldNames 的 Type 映射

    Map<String, List<String>> fieldNameByType = fcRepo.findAllTypeWithName()
            .stream()
            .collect(Collectors.groupingBy(Pair::getKey, 
                    Collectors.mapping(Pair::getValue, Collectors.toList())));
    

    然后您可以使用此地图准备您的回复。

    return fieldNameByType.entrySet().stream().map(e -> {
                    Map<String, Object> resultMap = new HashMap<String, Object>();
                    resultMap.put("type", e.getKey());
                    resultMap.put("field", e.getValue());
                    return resultMap;
                }).collect(Collectors.toList()); 
    

    【讨论】:

    • 在哪里定义键和值?你能建议我如何为这个定义键值,比如Pair &lt;String, List&lt;String&gt;&gt; pair= new Pair &lt;&gt; ("", new ArryList&lt;String&gt;());
    • 我按照你说的总结了数据,但它给出了一个错误,Pair 没有定义getKeygetValue
    • 使用org.apache.commons.math3.util.Pair 可能你在这里使用不同的文档commons.apache.org/proper/commons-math/javadocs/api-3.4/org/…
    • 哦,我会再次检查和测试并通知您,否则这是一个好方法。
    • 它正在工作@Eklavya。我测试了这个解决方案,它正是我想要的。感谢您的解决方案。
    猜你喜欢
    • 2019-05-10
    • 2022-01-09
    • 2016-01-28
    • 2011-06-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-12
    相关资源
    最近更新 更多