新手,自己照着网上写的mybatis saveOrUpdate方法,报了如下错。求指点。

背景:用mybatis generator生成的,没有这个方法,就自己写。
报错: Cause: java.sql.SQLException: SQL String cannot be empty
mapper.xml代码:

 <insert id="saveOrUpdate" >
    <selectKey keyProperty="id" resultType="com.zw.productTracker.pojo.entity.Product" order="BEFORE">
      select count(id) as id from product where product_name = #{productName}
    </selectKey>
    <if test="id == 1">
      update product
      set product_desc = #{productDesc},votes = #{votes},
      launch_time = #{launchTime},maker = #{maker},img_url = #{imgUrl},product_url = #{productUrl}
      ,maker_url = #{makerUrl}
      where product_name = #{productName}
    </if>
    <if test="id == 0">
      insert into product (product_name, product_desc, votes,
      launch_time, maker, img_url,
      product_url, maker_url)
      values (#{productName,jdbcType=VARCHAR}, #{productDesc,jdbcType=VARCHAR}, #{votes,jdbcType=INTEGER},
      #{launchTime,jdbcType=TIMESTAMP}, #{maker,jdbcType=VARCHAR}, #{imgUrl,jdbcType=VARCHAR},
      #{productUrl,jdbcType=VARCHAR}, #{makerUrl,jdbcType=VARCHAR})
    </if>
  </insert>
阅读 4.3k
2 个回答

把这个生成的 SQL 打印到控制台看一下,从报错来看应该是空的

因为查出来的count可能是除了 0和1 之外的一个数 条件都没有命中所以没有sql可以执行

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题