简体   繁体   English

如何在不带撇号的情况下将String参数传递给@Query

[英]How do I pass a String parameter into @Query without apostrophes

What I need is to pass the mysql schema as a parameter into the @Query, as the example shows: 我需要的是将mysql模式作为参数传递到@Query中,如示例所示:

@Query
(value = " SELECT id, name FROM " + schema + ".constructions "
,nativeQuery = true)
List<Object> findAllConstructions(String schema);

but this code doesn't work because "schema cannot be resolved to a variable". 但是此代码不起作用,因为“模式无法解析为变量”。

The problem is that when I try to pass the parameter correctly, like this: 问题是,当我尝试正确传递参数时,如下所示:

@Query
(value = " SELECT id, name FROM ?.constructions "
,nativeQuery = true)
List<Object> findAllConstructions(String schema);

or this: 或这个:

@Query
(value = " SELECT id, name FROM :schema .constructions "
,nativeQuery = true)
List<Object> findAllConstructions(@Param("schema") String schema);

the SQL generated has syntax errors, because the FROM clause looks like this: 生成的SQL语法错误,因为FROM子句如下所示:

FROM 'db_schema'.constructions

when it should be: 什么时候应该是:

FROM db_schema.constructions

Could anyone help me? 有人可以帮我吗?


EDIT 1 : I couldn't find any way (in MySQL) to SELECT a specific schema name as this question suggests for table names. 编辑1 :我找不到任何方法(在MySQL中)选择一个特定的架构名称,因为这个问题建议使用表名称。

EDIT 2 : I solved my issue by creating an alternative connection and setting a statement like this: 编辑2 :我通过创建替代连接并设置如下语句解决了我的问题:

ResultSet rs = connection.createStatement().executeQuery(" SELECT id, name FROM " 
+ schema + ".constructions ");

I'll leave the question open because my solution doesn't really answer the question, so if anyone finds an answer for the question, it's still open. 我将问题保留为开放状态,因为我的解决方案并未真正回答该问题,因此,如果有人找到该问题的答案,它仍然是开放的。

尝试使用表达语言

@Query(value = " SELECT id, name FROM #{#schema}.constructions " ,nativeQuery = true)

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

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