使用mybatis中出现异常:
nested exception is org.apache.ibatis.binding.BindingException: Parameter 'u
原因:
在整合mybatis开发的时候,有时候需要传入多个参数进行查询,当传入多个参数时使用不当会出现以上异常
是*mapper.xml映射没有得到传入的参数,这个时候需要在mapper变化传参,对比一下错误的和正确的传参方式
错误的:传递多个参数没有添加@Param 注解
/**
* 在插入或更新的时候,在查询数据库中是否有当前这个购物车商品的数据,得去查询才能确定
* @param userId
* @param goodsId
* @return
*/
ScenicCart selectCart(Long userId, Long goodsId);
/**
* 更新购物车某件商品的数量
* @param cartId
* @param num
* @param createBy
* @param date
* @return
*/
Integer updateCartNumber(Long cartId, Long num, tring createBy,Date date);
正确的:传递多个参数添加@Param 注解
import org.apache.ibatis.annotations.Param;
问题复现
controller层,从前端传入多个参数进行新增
/**
* 将商品添加到购物车
* @param goodsId
* @param amount
* @param userId
* @param createBy
* @return
*/
@SaCheckPermission("scenic:ScenicCart:add")
@Log(title = "购物车", businessType = BusinessType.INSERT)
@RequestMapping("/insertCart")
public R insertCart(Long goodsId, Long amount, Long userId, String createBy) {
iScenicCartService.insertCart(goodsId,amount,userId,createBy);
return R.ok();
}
secvice层
/**
* 将商品添加到购物车
* @param goodsId
* @param amount
* @param userId
* @param createBy
*/
void insertCart(Long goodsId, Long amount, Long userId, String createBy);
secviceImpl层
@Override
public void insertCart(Long goodsId, Long amount, Long userId, String createBy) {
//查询当前要添加到购物车的商品是否在表中存在
ScenicCart result = baseMapper.selectCart(userId, goodsId);
if (result == null) {
ScenicCart scenicCart = new ScenicCart();
scenicCart.setUserId(userId);
scenicCart.setGoodsId(goodsId);
scenicCart.setNum(amount);
ScenicGoods scenicGoods = scenicGoodsMapper.selectById(goodsId);
scenicCart.setPrice(scenicGoods.getGoodsPrice());
scenicCart.setCreateTime(new Date());
scenicCart.setCreateBy(createBy);
scenicCart.setUpdateBy(createBy);
scenicCart.setUpdateTime(new Date());
int insert = baseMapper.insert(scenicCart);
if (insert != 1) {
throw new ServiceException("插入数据是产业未知的异常!");
}
} else {//表示有这个商品,修改num值
Long num = result.getNum() + amount;
Integer integer = baseMapper.updateCartNumber(result.getCartId(), num, createBy, new Date());
if (integer != 1) {
throw new ServiceException("更新数据时产生未知的异常!");
}
}
}
mapper层:原因就在此处,没有加入@Param("")注解
/**
* 在插入或更新的时候,在查询数据库中是否有当前这个购物车商品的数据,得去查询才能确定
* @param userId
* @param goodsId
* @return
*/
ScenicCart selectCart(Long userId, Long goodsId);
/**
* 更新购物车某件商品的数量
* @param cartId
* @param num
* @param createBy
* @param date
* @return
*/
Integer updateCartNumber(Long cartId, Long num, tring createBy,Date date);
xml层
update scenic_cart set num = #{num} , update_by = #{createBy},
update_time = #{date} where cart_id = #{cartId}
mapper加入注解就可以了