sql中could not extract ResultSet 问题~

ResultSet org.hibernate.exception.SQLGrammarException: could not extract ResultSet 这个问题..一般是自己的sql写的有问题。
在ide里边使用原生的sql时,要格外注意~下边说一个在springboot jpa中遇到的问题。
这个是正确的sql:

@Query(value = "select * from `tb_announcement_mgmt`"
  + " where `title`=:title and `is_del`=0 "
  + "and (case when :areaCode=0 then 1=1 else `area_code`=:areaCode end) limit 10", nativeQuery = true)

两个错误的sql:

case1:
@Query(value = "select * from `tb_announcement_mgmt`"
  + " where `title`=:title and `is_del`=0"
  + "and (case when :areaCode=0 then 1=1 else `area_code`=:areaCode end) limit 10", nativeQuery = true)

case2:
@Query(value = "select * from `tb_announcement_mgmt`"
  + " where `title`=:title and `is_del`=0 "
  + "and (case when :areaCode=0 then 1=1 "
  +"else `area_code`=:areaCode end) limit 10", nativeQuery = true)

emmmm…乍看起来是没有问题的~~但是.他就是有错的

第一个错误的地方在于:
” where title=:title and is_del=0”+ “and 在0和end之间 没有空格!! 。。。在执行sql的时候,ide将字符串拼成一个完整的sql,0和end之间没有空格,那么拼接的时候就会成为 where title=:title and is_del=0and (case 这样的格式,所以就报错了.
第二个错误的地方在于:
“and (case when :areaCode=0 then 1=1 “+”else area_code=:areaCode end) 这句中case else 是一个完整的语句,在一个括号中,是不能分开的..所以要写成
“and (case when :areaCode=0 then 1=1 else area_code=:areaCode end) 才是正确的。

~~有的问题就是要自己多注意了。

你可能感兴趣的:(sql,jpa,mysql)