Error querying database. Cause: java.sql.SQLSyntaxErrorException: You have an error in your SQL syn

今天遇到两次这个报错,都是SQL语句有细微的差错导致

第一次:少了“,”

 

原来的SQL

 <update id="updateBlog" parameterType="map" >
        update blog
        <set>
            <if test="title!=null">
                title =#{title}
            </if>
            <if test="author != null">
                author =#{author}
            </if>
        </set>
        where id = #{id}
    </update>

title后面少了一个“ ,”

修改后的SQL

<update id="updateBlog" parameterType="map" >
        update blog
        <set>
            <if test="title!=null">
                title =#{title},
            </if>
            <if test="author != null">
                author =#{author}
            </if>
        </set>
        where id = #{id}
    </update>

成功

 第二次:少了空格

我是想通过Foreach把

select * from blog where 1=1 and (id=1 or id=2 or id=3)

转成动态SQL,

<select id="queryBlogForeach" parameterType="map" resultType="com.li.pojo.Blog">
        select * from blog
        <where>
            <foreach collection="ids" item="id" open="and(" close=")" separator="or">
                id=#{id}
            </foreach>
        </where>
    </select>

在起始的open 把"and ("的空格去掉了,同样报这个错,修改后成功

<select id="queryBlogForeach" parameterType="map" resultType="com.li.pojo.Blog">
        select * from blog
        <where>
            <foreach collection="ids" item="id" open="and (" close=")" separator="or">
                id=#{id}
            </foreach>
        </where>
    </select>

翻译一下这个报错

其实就是SQL语句出错,每个人错的情况不同,仔细检查SQL语句是不是少了或多了什么符号。 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

荔枝味啊~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值