首页 > 解决方案 > 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());
    }


}

标签: javaspringspring-boot

解决方案


推荐阅读