开发者社区> 问答> 正文

mybatis插入返回自增主键?报错

<insert id="addCart" parameterType="map">
        <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
            SELECT LAST_INSERT_ID()
        </selectKey>
        insert into
        cart
        (shop_no, goods_no, user_id, num, status, create_time, update_time, source)
        VALUES
        (#{shopNo}, #{goodsNo}, ${userId}, ${num}, ${status}, #{now}, #{now}, ${source})
</insert>


Integer addCart(@Param("shopNo") String shopNo, @Param("goodsNo") String goodsNo,
                @Param("userId") Integer userId, @Param("num") int num, @Param("status") int status, @Param("now") Date now, @Param("source") int source);

插入数据没问题,但是总是返回1。

<insert id="addCart" parameterType="map" useGeneratedKeys="true" keyProperty="id">
        insert into
        cart
        (shop_no, goods_no, user_id, num, status, create_time, update_time, source)
        VALUES
        (#{shopNo}, #{goodsNo}, ${userId}, ${num}, ${status}, #{now}, #{now}, ${source})
</insert>

Integer addCart(@Param("shopNo") String shopNo, @Param("goodsNo") String goodsNo,
                @Param("userId") Integer userId, @Param("num") int num, @Param("status") int status, @Param("now") Date now, @Param("source") int source);

或

@Options(useGeneratedKeys = true, keyProperty="cart.id", keyColumn="GENERATED_KEY")
    Integer addCart(@Param("shopNo") String shopNo, @Param("goodsNo") String goodsNo,
                @Param("userId") Integer userId, @Param("num") int num, @Param("status") int status, @Param("now") Date now, @Param("source") int source);

或

@Options(useGeneratedKeys = true, keyProperty="cart.id" ,keyColumn="id")
    Integer addCart(@Param("shopNo") String shopNo, @Param("goodsNo") String goodsNo,
                @Param("userId") Integer userId, @Param("num") int num, @Param("status") int status, @Param("now") Date now, @Param("source") int source);

插入数据没问题,但是得不到返回的自增主键id值。

nested exception is org.apache.ibatis.executor.ExecutorException: Error getting generated key or setting result to parameter object. Cause: org.apache.ibatis.binding.BindingException: Parameter 'id' not found. Available parameters are [goodsNo, param7, param5, param6, num, source, param3, userId, param4, param1, param2, now, shopNo, status]

 

@Options(useGeneratedKeys = true, keyProperty="cart.id", keyColumn="id")
    Integer addCart(String shopNo, String goodsNo,
                    Integer userId, int num, int status, Date now, int source);

则插入失败,且报错。

nested exception is org.apache.ibatis.binding.BindingException: Parameter 'userId' not found. Available parameters are [param7, param5, param6, param3, param4, param1, param2, 0, 1, 2, 3, 4, 5, 6]

 

 

展开
收起
爱吃鱼的程序员 2020-06-08 11:44:33 802 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    Cause:org.apache.ibatis.binding.BindingException:Parameter'id'notfound.Availableparametersare[goodsNo,param7,param5,param6,num,source,param3,userId,param4,param1,param2,now,shopNo,status]

     

    异常在此。

    废话

    你前面就是XX.id 你后面直接ID?告诉你了 id not found 

     

    mybatis改成cart.id也是一样的错

    参数用对象,insert/update语句返回的是影响的条数,不是你要的主键

    意思是mybatis不能返回自增主键值?我换成对象也是错
    <insertid="insertAndGetId"useGeneratedKeys="true"keyProperty="userId"parameterType="com.chenzhou.mybatis.User">insertintouser(userName,password,comment)values(#{userName},#{password},#{comment})</insert>
    publicclassUser{privateintuserId;privateStringuserName;privateStringpassword;privateStringcomment;//setterandgetter}

     

    我用getId也是错回复 @王静0716:主键会set到user的userId上,而不是返回值我就是这样写的啊
    <insertid="addCartAndGetId"parameterType="com.wj.pojo.Cart"useGeneratedKeys="true"keyProperty="cart.id">insertintocart(shop_no,goods_no,user_id,num,status,create_time,update_time,source)VALUES(#{cart.shopNo},#{cart.goodsNo},${cart.userId},${cart.num},${cart.status},#{cart.createTime},#{cart.updateTime},${cart.source})</insert>intaddCartAndGetId(@Param("cart")Cartcart);Cartcart=newCart();cart.setShopNo(goods.getShopNo());cart.setGoodsNo(goodsNo);cart.setUserId(userId);cart.setNum(buyNum);cart.setStatus(CartStatus.addCart.ordinal());cart.setCreateTime(now);cart.setUpdateTime(now);cart.setSource(source);//cartId=cartMapper.insertSelective(cart);orderDao.addCartAndGetId(cart);cartId=cart.getId();这样写就对了。

     

    <tabletableName="rc_template"><generatedKeycolumn="ID"sqlStatement="MySql"identity="true"/></table>

    generatorconfig文件配置。亲测有效。http://blog.csdn.net/u011403655/article/details/50696341

    2020-06-08 11:44:44
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Java Spring Boot开发实战系列课程【第6讲】:Spring Boot 2.0实战MyBatis与优化(Java面试题) 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载