<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]
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
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。