【问题标题】:Spring @Query Remove QuoteSpring @Query 删除引用
【发布时间】:2018-01-04 15:13:15
【问题描述】:


我想在 spring 查询中使用表名, 但是当我执行它时,出现以下错误:

"could not extract ResultSet; SQL [n/a]; nested exception is
 org.hibernate.exception.SQLGrammarException: could not extract ResultSet"


因为当参数是字符串参数时,spring 会自动添加引号 ..

@Query(value = "desc :name", nativeQuery = true)
List<RepositoryColumn> getColumnFromRepository(@Param("name") String name);

在其他情况下,我想要 "desc table_name" 而不是 "desc 'table_Name'"

有什么想法吗?

【问题讨论】:

  • 这与 Spring 无关,而是与参数替换如何与 JDBC 一起工作。您不能对此类查询使用参数绑定。
  • 你可以创建一个过程来EXECUTE IMMEDIATE这个查询

标签: java mysql spring hibernate spring-boot


【解决方案1】:
 SELECT *
 FROM information_schema.columns
 WHERE 
     table_name = :name 

尝试以另一种方式相同。您可以选择必要的列而不是*

【讨论】:

  • 谢谢!这是工作,但现在我怎样才能获得相当于Select * FROM table_name
  • 不可能。不支持使用表名作为参数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-28
相关资源
最近更新 更多