在项目开发调优过程中遇到一个问题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中方法不能重载,第一次遇到