Invalid bound statement (not found)

Invalid bound statement (not found)

https://img1.sycdn.imooc.com/climg/62bbc0d2092fde7d18380389.jpg
cart.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.imooc.mall.mapper.CartMapper">
    <select id="selectProductList" parameterType="Integer" resultType="com.imooc.mall.vo.CartVO">
        SELECT
            a.id as id,
            a.product_id as productId,
            a.user_id as userId,
            a.quantity as quantity,
            a.selected as selected,
            b.price as price,
            b.name AS productName,
            b.image AS productImage
        FROM
            imooc_mall_cart a
            LEFT JOIN imooc_mall_product b ON a.product_id = b.id
        WHERE
            a.user_id = #{userId}
            AND b.STATUS = 1
    </select>
</mapper>

CartMapper.java

@Repository
public interface CartMapper extends BaseMapper<Cart> {
    List<CartVO> selectProductList(@Param("userId") Integer userId);
}

CartServiceImpl.java

@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private ProductMapper productMapper;

    @Override
    public List<CartVO> list(Integer userId) {
        List<CartVO> cartVOS = cartMapper.selectProductList(userId);
        for (int i = 0; i < cartVOS.size(); i++) {
            CartVO cartVO =  cartVOS.get(i);
            cartVO.setTotalPrice(cartVO.getPrice() * cartVO.getQuantity());
        }
        return cartVOS;
    }
    @Override
    public List<CartVO> add(Integer productId, Integer count) {
        validProduct(productId, count);
        QueryWrapper<Cart> qw = new QueryWrapper<>();
        qw.eq("product_id", productId);
        qw.eq("user_id", UserFilter.loginUser.getId());
        Cart cart = cartMapper.selectOne(qw);
        if (cart == null) {
            cart = new Cart();
            cart.setProductId(productId);
            cart.setUserId(UserFilter.loginUser.getId());
            cart.setQuantity(count);
            cart.setSelected(Constant.Cart.SELECTED);
            cartMapper.insert(cart);
        } else {
            cart.setQuantity(cart.getQuantity() + count);
            cartMapper.updateById(cart);
        }
        return this.list(UserFilter.loginUser.getId());

    }

    private void validProduct(Integer productId,Integer count) {
        QueryWrapper<Product> qw = new QueryWrapper<>();
        qw.eq("id", productId);
        Product product = productMapper.selectOne(qw);
        if (product == null || product.getStatus().equals(Constant.SaleStatus.NOT_SALE)) {
            throw new ImoocMallException(ImoocMallExceptionEnum.NOT_SALE);
        }
        if (count > product.getStock()) {
            throw new ImoocMallException(ImoocMallExceptionEnum.NOT_ENOUGH);
        }
    }
}

没搞懂,问题出那了
用Mybatis-plus内置的查询是可以的,自定义查询就报错

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕小小 2022-06-29 17:03:09

同学你好,建议检查下Mybatis的xml数据库查询文件的namespace属性和Dao层接口的全路径值是否一致。

https://img1.sycdn.imooc.com//climg/62bc1539092fbd3605170352.jpg

祝学习愉快~


  • 提问者 慕妹8830657 #1

    是一致的  用Mybatis内置方法是可以查询的

    https://img1.sycdn.imooc.com//climg/62bc628709f3ced816200625.jpg

    2022-06-29 22:32:44
  • 提问者 慕妹8830657 #2

    问题已解决,利用@Select 注解

    public interface CartMapper extends BaseMapper<Cart> {
        @Select( "SELECT" +
                "            a.id as id," +
                "            a.product_id as productId," +
                "            a.user_id as userId," +
                "            a.quantity as quantity," +
                "            a.selected as selected," +
                "            b.price as price," +
                "            b.name AS productName," +
                "            b.image AS productImage" +
                "        FROM" +
                "            imooc_mall_cart a" +
                "            LEFT JOIN imooc_mall_product b ON a.product_id = b.id" +
                "        WHERE" +
                "            a.user_id = #{userId}" +
                "            AND b.STATUS = 1" )
        List<CartVO> selectProductList(@Param("userId") Integer userId);
    
    }


    2022-06-30 10:19:22
  • 好帮手慕小小 回复 提问者 慕妹8830657 #3

    同学你好,使用注解方式解决了问题,很棒!继续加油!

    上述异常提示为绑定异常,出现上述异常的原因有很多,同学可从如下几点排查下:

    1)查看target目录下是否存在对应的mapper.xml文件。若不存在则建议删除target文件夹,清除IDEA缓存,在重新构建build项目。

    https://img1.sycdn.imooc.com//climg/62bd40a30981c82c02830215.jpg

    2)xml文件中namespace是否正确;

        xml文件中id是否和mapper接口中方法名一致,parameterType是否一致。

    注:同学可以在IDEA插件库中下载Free MyBatis Tool,会自动进行校验。

    3)application启动类上使用的@MapperScan注解中的路径是否正确https://img1.sycdn.imooc.com//climg/62bd44270908d13605380221.jpg

    4)文件类型是否正确,是否是xml文件,例如:

    https://img1.sycdn.imooc.com//climg/62bd4493096a444b02610153.jpg

    祝学习愉快~

    2022-06-30 14:38:48
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师