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) 才是正确的。

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

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值