嗨,我试图实现的是得到返回的结果页的结果进入dto和请求作为分页。我该怎么做?
我找到了一些方法,但我不确定,也不舒服。我每一次尝试querydsl的方式,但它有“计数”的限制,当使用聚合函数时,像sum()和group by with multiple column right now将在我的存储库中用@query()注解编写nativequery,如下所示:
@Query(name = "find_stock_total", nativeQuery = true)
Page<StockTotalResponseDto> findStockTotalRsMap(Pageable pageable);
然后在我的实体类中,我必须编写@namedNativeRequesties并用@sqlresultsetmappingsMap它,下面是我如何编写它的:
@NamedNativeQueries({
@NamedNativeQuery(
name = "find_stock_total",
query = "SELECT product_id, product_code, sum(qty) FROM "book_stock"
where warehouse_code = 'GBKTJKT1' and product_code in('MCM-508','TL-101') and branch_code = 'JKT' and branch_id = '1'
GROUP BY product_id, product_code",
resultSetMapping = "stock_total_response_dto"
),
@NamedNativeQuery(
name = "find_stock_total.count",
query = "select count(*) from (
SELECT product_id, product_code, sum(qty) FROM "book_stock"
where warehouse_code = 'GBKTJKT1' and product_code in('MCM-508','TL-101') and branch_code = 'JKT' and branch_id = '1'
GROUP BY product_id, product_code
) as count"
)
})
@SqlResultSetMappings({
@SqlResultSetMapping(
name = "stock_total_response_dto",
classes = @ConstructorResult(
targetClass = StockTotalResponseDto.class,
columns = {
@ColumnResult(name = "product_id", type = String.class),
@ColumnResult(name = "product_code", type = String.class),
@ColumnResult(name = "sum", type = BigInteger.class)
}
)
),
@SqlResultSetMapping(
name = "stock_total_response_dto.count",
columns = @ColumnResult(name = "count")
)
})
@Entity
我将不得不在@entity类中编写sql字符串,我不能使用pageable提供的排序,我必须编写额外的count查询。我想使用分页排序,来排序我的数据。有没有其他更好的方式或者现代的方式来写这样的东西?也许有人可以提供“标准”的方式,或者有另一种jpa方式?或者比这更好的?
1条答案
按热度按时间6jygbczu1#
您应该检查一下blazepersistence提供了什么,它可以在jpa/hibernate之上工作,并且还集成了springdatajpa,允许您使用它作为springdatajpa正常工作的替代品。
它正确地支持聚合函数(也支持count查询),支持键集分页等等。查看有关spring数据集成的文档:https://persistence.blazebit.com/documentation/entity-view/manual/en_us/#spring-数据集成