Springboot + JPA:列出每种字段类型的所有字段名 [英] Springboot + JPA : List all fieldnames for each fieldtype
问题描述
我正在为项目使用Spring Boot + JPA.我想要表中每个type
的fieldName
列表.因此,通过使用
I am using Spring Boot + JPA for my project. I want a fieldName
list for each type
from my table. So I did this by using query with named parameters using JPQL
. I made two methods in the repository.
- 第一种方法:
此方法用于获取type
的不同列表.
- First Method:
This method is for getting distinct list oftype
.
@Query(value = "SELECT DISTINCT fc.type FROM FieldCapabilityModel fc")
List<String> findDistinctType();
- 第二种方法:
nbsp; bsp 此方法用于获取特定type
的名称列表.
- Second Method:
This method is for getting a list of names for particulartype
.
@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());
}
}
当我使用rest控制器调用时,我的API响应是:
When I called using the rest controller my API response is:
{
"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种以上的类型.尽管我想知道这是一种有效而强大的方法.
我的问题是:
I am getting the exact output as I want and also this is a simple data response I have more than 16 types. Albeit I want to know that this is an efficient and robust way.
My questions are:
我们可以为此使用其他任何查询吗?
如果可以,我们可以使用@NamedQuery
代替它吗?
该执行过程应采用哪种简化,健壮和高效的方法?
Can we use any other queries for this?
Can we use@NamedQuery
instead of this if yes then why?
What should be the reduced, robust, and efficient method for this execution?
如果我想对该解决方案进行任何改进,请给我建议,或者如果要比该解决方案更好的话,请提出一个新的解决方案.
Please give me suggestions if I want to improve anything into this solution or propose a new solution if that is better than this solution.
推荐答案
您可以获取所有&一次查询中的fieldName
You can fetch all type & fieldName in one query
@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();
然后汇总数据.创建一个类型为Map的键,其中Key为fieldNames
And then summarize data. Create a map of Type where Key is fieldNames
Map<String, List<String>> fieldNameByType = fcRepo.findAllTypeWithName()
.stream()
.collect(Collectors.groupingBy(Pair::getKey,
Collectors.mapping(Pair::getValue, Collectors.toList())));
然后,您可以使用此地图准备响应.
Then you can prepare your response using this map.
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());
这篇关于Springboot + JPA:列出每种字段类型的所有字段名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!