Spring Boot Sping Boot ,...Service中构造函数的参数1需要类型为“...Mapper”的Bean,但找不到该Bean

olmpazwi  于 2023-03-02  发布在  Spring
关注(0)|答案(1)|浏览(204)

我得到没有找到错误的Map。它只是用户实体的工作,然后我添加了食品实体(所有相同的用户)现在它显示错误。

    • 说明:**

com. example. springmysqlelastic. service. impl. FoodService中构造函数的参数1需要类型为"com. example. springmysqlelastic. mapper. UserAndFoodMapper"的Bean,但找不到该Bean。

    • 操作:**

考虑在配置中定义一个类型为"com.example.springmysqlelastic.mapper. UserAndFoodMapper"的bean。

    • 用户和食物Map器. java**
  1. package com.example.springmysqlelastic.mapper;
  2. import com.example.springmysqlelastic.model.Food;
  3. import com.example.springmysqlelastic.model.FoodModel;
  4. import com.example.springmysqlelastic.model.User;
  5. import com.example.springmysqlelastic.model.UserModel;
  6. import com.example.springmysqlelastic.model.dto.FoodDTO;
  7. import com.example.springmysqlelastic.model.dto.UserDTO;
  8. import org.mapstruct.Mapper;
  9. import java.util.List;
  10. @Mapper(componentModel = "spring")
  11. public interface UserAndFoodMapper {
  12. UserDTO toUserDTO(User user);
  13. List<UserDTO> toUserDtos(List<User> users);
  14. User toUser(UserDTO userDTO);
  15. List<User> toUsers(List<UserDTO> userDTOS);
  16. UserModel toUserModel(User user);
  17. FoodDTO toFoodDTO(Food food);
  18. List<FoodDTO> toFoodDtos(List<Food> foods);
  19. Food toFood(FoodDTO foodDTO);
  20. List<Food> toFoods(List<FoodDTO> foodDTOS);
  21. FoodModel toFoodModel(Food food);
  22. }
    • 食品服务. java**
  1. package com.example.springmysqlelastic.service.impl;
  2. import com.example.springmysqlelastic.mapper.FoodMapper;
  3. import com.example.springmysqlelastic.mapper.UserAndFoodMapper;
  4. import com.example.springmysqlelastic.model.Food;
  5. import com.example.springmysqlelastic.model.dto.FoodDTO;
  6. import com.example.springmysqlelastic.repo.IFoodDAO;
  7. import com.example.springmysqlelastic.service.IFoodService;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.stereotype.Service;
  10. import java.util.List;
  11. @Service
  12. public class FoodService implements IFoodService {
  13. private IFoodDAO foodDAO;
  14. private UserAndFoodMapper foodMapper;
  15. //private final FoodMapper foodMapper;
  16. @Autowired
  17. public FoodService(IFoodDAO foodDAO, UserAndFoodMapper foodMapper) {
  18. this.foodDAO = foodDAO;
  19. this.foodMapper = foodMapper;
  20. }
  21. @Override
  22. public FoodDTO save(FoodDTO foodDTO) {
  23. Food food = this.foodDAO.save(this.foodMapper.toFood(foodDTO));
  24. return this.foodMapper.toFoodDTO(food);
  25. }
  26. @Override
  27. public FoodDTO findById(Long id) {
  28. return this.foodMapper.toFoodDTO(this.foodDAO.findById(id).orElse(null));
  29. }
  30. @Override
  31. public List<FoodDTO> findAll() {
  32. return this.foodMapper.toFoodDtos(this.foodDAO.findAll());
  33. }
  34. }
mbyulnm0

mbyulnm01#

向Map器类添加@Component注解。

相关问题