java - Spring Boot,...Service 中构造函数的参数 1 需要找不到类型为“...Mapper”的 bean
问题描述
我在 Mapper 上遇到bean not found 错误。它仅适用于用户实体,然后我添加了食物实体(都与用户相同),现在它显示错误。
描述:
com.example.springmysqlelastic.service.impl.FoodService 中构造函数的参数 1 需要找不到类型为“com.example.springmysqlelastic.mapper.UserAndFoodMapper”的 bean。
行动:
考虑在你的配置中定义一个 'com.example.springmysqlelastic.mapper.UserAndFoodMapper' 类型的 bean。
UserAndFoodMapper.java
package com.example.springmysqlelastic.mapper;
import com.example.springmysqlelastic.model.Food;
import com.example.springmysqlelastic.model.FoodModel;
import com.example.springmysqlelastic.model.User;
import com.example.springmysqlelastic.model.UserModel;
import com.example.springmysqlelastic.model.dto.FoodDTO;
import com.example.springmysqlelastic.model.dto.UserDTO;
import org.mapstruct.Mapper;
import java.util.List;
@Mapper(componentModel = "spring")
public interface UserAndFoodMapper {
UserDTO toUserDTO(User user);
List<UserDTO> toUserDtos(List<User> users);
User toUser(UserDTO userDTO);
List<User> toUsers(List<UserDTO> userDTOS);
UserModel toUserModel(User user);
FoodDTO toFoodDTO(Food food);
List<FoodDTO> toFoodDtos(List<Food> foods);
Food toFood(FoodDTO foodDTO);
List<Food> toFoods(List<FoodDTO> foodDTOS);
FoodModel toFoodModel(Food food);
}
餐饮服务.java
package com.example.springmysqlelastic.service.impl;
import com.example.springmysqlelastic.mapper.FoodMapper;
import com.example.springmysqlelastic.mapper.UserAndFoodMapper;
import com.example.springmysqlelastic.model.Food;
import com.example.springmysqlelastic.model.dto.FoodDTO;
import com.example.springmysqlelastic.repo.IFoodDAO;
import com.example.springmysqlelastic.service.IFoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class FoodService implements IFoodService {
private IFoodDAO foodDAO;
private UserAndFoodMapper foodMapper;
//private final FoodMapper foodMapper;
@Autowired
public FoodService(IFoodDAO foodDAO, UserAndFoodMapper foodMapper) {
this.foodDAO = foodDAO;
this.foodMapper = foodMapper;
}
@Override
public FoodDTO save(FoodDTO foodDTO) {
Food food = this.foodDAO.save(this.foodMapper.toFood(foodDTO));
return this.foodMapper.toFoodDTO(food);
}
@Override
public FoodDTO findById(Long id) {
return this.foodMapper.toFoodDTO(this.foodDAO.findById(id).orElse(null));
}
@Override
public List<FoodDTO> findAll() {
return this.foodMapper.toFoodDtos(this.foodDAO.findAll());
}
}
解决方案
推荐阅读
- angular - 为什么阿波罗角查询返回空数组?
- c# - 2个组合框c#的相同SelectedIndexChanged函数
- c++ - 内存屏障对其他线程的数据可见性的影响
- sql - 与 group by 结合的平均值与子查询中的直接平均值有何不同?
- docker - 从 docker 文件创建 docker 映像的问题
- jenkins - OCP - Jenking 持久 - 旧版本?
- mysql - MySQL Set 0 if null (SUM CASE) 不起作用
- r - 总结以基于标准开始的当前观察结果
- python - Python:如何创建一个以 24 小时格式读取时间并在时间增加 1 秒后显示时间的程序?
- java - arraylist 的内容使用 println 打印。我希望我创建的对象具有类似的功能,而无需在 java 中使用 toString() 函数