UnsatisfiedDependencyException: Error creating bean:Unsatisfied dependency expressed through field‘‘

在项目开发调优过程中遇到一个问题org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'categoryServiceImpl': Unsatisfied dependency expressed through field 'categoryMapper';

原本使用逐条删除的方法删除数据

业务层代码如下:

DishServiceImpl.java

/**
     * 批量删除菜品
     * @param ids
     */
    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {

        //判断当前菜品是否能够删除(起售中、关联套餐不能删除)
        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);
            if(dish.getStatus() == StatusConstant.ENABLE){
                //当前菜品起售中,不能删除
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
        if(setmealIds != null && setmealIds.size() > 0){
            //当前菜品关联了套餐不能删除
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //删除菜品表中的菜品数据

        for (Long id : ids) {
            dishMapper.deleteById(id);
            //删除关联菜品口味信息
            dishFlavorMapper.deleteByDishId(id);
        }

  

    }

持久层代码如下

DishMapper.java

    /**
     * 逐条删除菜品
     * @param id
     */
    @Delete("delete from dish where id = #{id}")
    void deleteById(Long id);

    

DishFlavorMapper.java

    /**
     * 根据菜品id删除口味
     * @param dishId
     */
    @Delete("delete from dish_flavor where dish_id = #{dishId}")
    void deleteByDishId(Long dishId);

但是在海量请求的情况下,这样效率低,数据库的压力也会很大,所以使用动态sql批量删除,更改好mapper方法和xml的sql语句

更改后业务层代码如下

    /**
     * 批量删除菜品
     * @param ids
     */
    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {

        //判断当前菜品是否能够删除(起售中、关联套餐不能删除)
        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);
            if(dish.getStatus() == StatusConstant.ENABLE){
                //当前菜品起售中,不能删除
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
        if(setmealIds != null && setmealIds.size() > 0){
            //当前菜品关联了套餐不能删除
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }



        //优化逐条删除,改为批量删除
        dishMapper.deleteById(ids);

        dishFlavorMapper.deleteByDishId(ids);

    }

持久层代码偷懒了,没有将前面的注解掉:

 DishMapper.java

    /**
     * 逐条删除菜品
     * @param id
     */
    @Delete("delete from dish where id = #{id}")
    void deleteById(Long id);

    
    /**
     * 优化逐条删除菜品,改为批量删除菜品
     * @param id
     */
    void deleteById(List<Long> id);

DishFlavorMapper.java

    /**
     * 根据菜品id删除口味
     * @param dishId
     */
    @Delete("delete from dish_flavor where dish_id = #{dishId}")
    void deleteByDishId(Long dishId);

    /**
     * 批量删除口味
     * @param dishIds
     */
    void deleteByDishId(List<Long> dishIds);

可以看到直接将list传到持久层,在持久层进行批量删除,但是持久层没有把前面逐条删除的语句注解掉,然后启动项目,就出现了上述错误。

what?一脸懵,这怎么回事

之前还好好的啊,怎么突然项目运行不出来了

查看报错位置就是在Mapper中,然后试着将同名方法注解掉,点击运行,ohhhhhhhhhhhhhhhh运行成功了,wk原来Mapper中方法不能重载,第一次遇到

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值