简体   繁体   English

未绑定命名参数:Spring Boot 中的 DATE_FORMAT 本机查询

[英]Named parameter not bound : DATE_FORMAT Native Query in Spring Boot

I am trying to get table data as list using select query between dateFrom and current date using MySQL Native Query in Spring Boot.我正在尝试在 Spring Boot 中使用 MySQL Native Query 使用 dateFrom 和当前日期之间的选择查询将表数据作为列表获取。 and MySQL database field datatype is String.和 MySQL 数据库字段数据类型是字符串。

Below is Query in Repository:以下是存储库中的查询:

@Query(value = "select * from Account where DATETIMESTAMP >= :dateFrom  AND DATETIMESTAMP < :DATE_FORMAT(curdate(), '%d/%m/%Y')", nativeQuery = true)
List<Account> findByDate(@Param("dateFrom") String dateFrom);

Getting below error for above query in Spring Boot:在 Spring Boot 中获取上述查询的以下错误:

 Named parameter not bound : DATE_FORMAT; nested exception is org.hibernate.QueryException: Named parameter not bound : DATE_FORMAT

Can anyone please help me to frame the query for the same.谁能帮我构建相同的查询。

If you're using JPQL & still getting this error then its possible you might have a space in between like如果您使用的是 JPQL 并且仍然收到此错误,那么您可能在两者之间有一个空格

where id = : myId

Should be应该

where id = :myId

I had the same error, but in my case I had a comparison operator '<>' joined on to my variable with no space in between.我有同样的错误,但在我的例子中,我有一个比较运算符“<>”连接到我的变量上,中间没有空格。 Adding a space between the variable and the operator fixed my issue.在变量和运算符之间添加一个空格解决了我的问题。 ie. IE。 changing :addressType<> to :addressType <> worked:addressType<>更改为:addressType <>有效

Remove the : from :DATE_FORMAT(curdate(), '%d/%m/%Y') .:DATE_FORMAT(curdate(), '%d/%m/%Y')删除: : is used for binding parameters in jpa query. :用于在 jpa 查询中绑定参数。 The addition : in front of DATE_FORMAT makes JPA think it as a parameter.添加:DATE_FORMAT前面使JPA将其视为参数。 So the final query should be所以最终的查询应该是

@Query(value = "select * from Account where DATETIMESTAMP >= :dateFrom  AND DATETIMESTAMP < DATE_FORMAT(curdate(), '%d/%m/%Y')", nativeQuery = true)
List<Account> findByDate(@Param("dateFrom") String dateFrom);

In any case, If you face this problem.无论如何,如果你遇到这个问题。 Before doing debugging or deep google search Check the following things在进行调试或深度谷歌搜索之前检查以下事项

  1. Method variable names and your query variables are correct or not.方法变量名称和您的查询变量是否正确。

  2. Query semicolon with your parameters使用您的参数查询分号

  3. If you renamed the table with short expressions, double-check them they were used correctly.如果您使用短表达式重命名了表,请仔细检查它们是否正确使用。

  4. Check your query syntax.检查您的查询语法。

Because most of the issues have come from the above minor mistakes.因为大部分问题都来自于上述的小错误。

Google brought me here for the search of Named parameter not bound谷歌把我带到这里来搜索未绑定的命名参数

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

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