我得到豆没有找到错误的Map。它只是用户实体的工作,然后我添加了食品实体(所有相同的用户)现在它显示错误。
- 说明:**
com. example. springmysqlelastic. service. impl. FoodService中构造函数的参数1需要类型为"com. example. springmysqlelastic. mapper. UserAndFoodMapper"的Bean,但找不到该Bean。
- 操作:**
考虑在配置中定义一个类型为"com.example.springmysqlelastic.mapper. UserAndFoodMapper"的bean。
- 用户和食物Map器. 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());
}
}
1条答案
按热度按时间mbyulnm01#
向Map器类添加@Component注解。