简体   繁体   English

spring-data-jdbc 中的参数化顺序

[英]Parametrised order in spring-data-jdbc

I'm struggling with making order parametrised.我正在努力使订单参数化。 The best would be to have最好是有

... order by :orderColumn :orderDirection ...

Is it even possible to turn:甚至可以转:

    @Query("select * from Document where folderId = :folderId AND documentType = :documentType"
            + " order by created desc limit :limit")
    List<Document> findByFolderIdAndDocumentTypeOrderByWithLimit(Long folderId, String documentType,
                                                                     String orderColumn, Integer limit);

Into:进入:

    @Query("select * from Document where folderId = :folderId AND documentType = :documentType"
            + " order by :orderColumn desc limit :limit")
    List<Document> findByFolderIdAndDocumentTypeOrderByWithLimit(Long folderId, String documentType,
                                                                     String orderColumn, Integer limit);

I'm using spring-data-jdbc 1.1.3.REELASE version.我正在使用 spring-data-jdbc 1.1.3.REELASE 版本。 Even doing it for just column name would help me a lot.即使只为列名做这件事也会对我有很大帮助。

Use the PagingAndSortingRepository使用PagingAndSortingRepository

  @Query("select * from Document where folderId = :folderId AND documentType = :documentType")
    List<Document> findByFolderIdAndDocumentTypeOrderByWithLimit(Long folderId, String documentType, Pageable pageable);

And call the method:并调用该方法:

findByFolderIdAndDocumentTypeOrderByWithLimit(1,"Type", PageRequest.of(0, <limit>, Sort.by(<column name>).descending());

sume examples you can fine here举个例子,你可以在这里罚款

Replacing a column name with a bind parameter is not possible.用绑定参数替换列名是不可能的。 This is a limitation of JDBC, and possibly even of SQL.这是 JDBC 的限制,甚至可能是 SQL 的限制。

What you can do is use an expression that evaluates to the value of different columns based on a bind parameter.您可以做的是使用一个表达式,该表达式根据绑定参数计算不同列的值。 Something like就像是

... ORDER BY CASE
    WHEN :orderColumn = 'created' THEN created
    WHEN :orderColumn = 'updated' THEN updated
    WHEN :orderColumn = 'deleted' THEN deleted
    ELSE 1
END;

Note that this kind of constructed isn't well supported by query optimisers and therefore will not utilise any index that would otherwise apply.请注意,查询优化器不能很好地支持这种构造方式,因此不会使用任何原本适用的索引。

If you require better support by the optimiser you can always write a custom query that you create dynamically.如果您需要优化器的更好支持,您始终可以编写动态创建的自定义查询。 If you decide to do so, make sure you do not incorporate Strings from untrusted sources in your SQL statement to avoid SQL injection vulnerabilities.如果您决定这样做,请确保不要在 SQL 语句中包含来自不受信任来源的字符串,以避免 SQL 注入漏洞。

Until there will be version of spring-data-jdbc (as far as I know 2.XXRELEASE) supporting Pagable object I've decided for this particular case use NamedParameterJdbcTemplate and tail my own query.在有支持 Pagable 对象的spring-data-jdbc (据我所知 2.XXRELEASE)之前,我已经决定针对这种特殊情况使用NamedParameterJdbcTemplateNamedParameterJdbcTemplate我自己的查询。 I hope this or other answers here will help someone.我希望这里的这个或其他答案会对某人有所帮助。 Thank you for your help :)感谢您的帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM