Spring Data:JPA 存储库 findAll() 返回 *Map 而不是 List? [英] Spring Data: JPA repository findAll() to return *Map instead of List?
本文介绍了Spring Data:JPA 存储库 findAll() 返回 *Map 而不是 List?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个看起来像这样的 Spring Data JPA 存储库接口:
I have a Spring Data JPA repository interface that looks something like this:
@Repository
public interface DBReportRepository extends JpaRepository<TransactionModel, Long> {
List<TransactionModel> findAll();
List<TransactionModel> findByClientId(Long id);
}
除了要返回 HashMap
类型的集合之外,是否有其他解决方法?我查看了 Spring Data 类,除了 List<> 返回值之外找不到任何其他内容.
Is there a workaround to make the same but for a Collection to be returned of type HashMap<K, V>
? I've looked through the Spring Data classes and couldn't find anything other than List<> return values.
推荐答案
我不认为您会找到一个更简单的解决方案,即创建一个简单的单线来将您的结果转换为地图.使用 java 8 lambdas 既简单又快速:
I dont think you will find an easier solution as to create a simple one liner to convert your result to a map. It is simple and fast with java 8 lambdas:
Map<Long, Transaction> transactionMap = transactionList.stream()
.collect(Collectors.toMap(Transaction::getId, Function.identity()));
这篇关于Spring Data:JPA 存储库 findAll() 返回 *Map 而不是 List?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文