@ Shermeen Можете ли вы поделиться настроенным кодом?
Итак, мы проверяем это. Тем не менее я добавляю этот пример, пожалуйста, проверьте его.
Структура
EntityMapper. java
public interface EntityMapper<D, E> {
E toEntity(D dto);
D toDTO(E entity);
List<E> toEntity(List<D> dtoList);
List<D> toDTO(List<E> entityList);
}
UserMapper. java
import com.ecommerce.auth_service.domain.external_db.User;
import com.ecommerce.auth_service.domain.external_db.dto.UserDTO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import java.util.UUID;
@Mapper(componentModel = "spring", uses = {UserMapper.class})
public interface UserMapper extends EntityMapper<UserDTO, User> {
@Mapping(source = "user_id", target = "user")
default User fromId(UUID id) {
if (id == null)
return null;
User user = new User();
user.setUserId(id);
return user;
}
}
UserMapperImpl. java
import com.ecommerce.auth_service.domain.external_db.User;
import com.ecommerce.auth_service.domain.external_db.dto.UserDTO;
import com.ecommerce.auth_service.domain.external_db.mapper.UserMapper;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@Component
public class UserMapperImpl implements UserMapper {
@Override
public User toEntity(UserDTO dto) {
if (dto == null)
return null;
User user = new User();
user.setUserId(dto.getUserId());
user.setUsername(dto.getUsername());
user.setEmail(dto.getEmail());
user.setPassword(dto.getPassword());
user.setPhoneNo(dto.getPhoneNumber());
user.setStatus(dto.getStatus());
user.setStore(dto.getStoreId());
user.setAuthority(dto.getAuthority());
user.setPrivileges(dto.getAuthority().getPrivileges());
return user;
}
@Override
public UserDTO toDTO(User entity) {
if (entity == null) {
return null;
}
UserDTO userDTO = new UserDTO();
userDTO.setUserId(entity.getUserId());
userDTO.setUsername(entity.getUsername());
userDTO.setEmail(entity.getEmail());
userDTO.setPassword(entity.getPassword());
userDTO.setPhoneNumber(entity.getPhoneNo());
userDTO.setStatus(entity.getStatus());
userDTO.setStoreId(entity.getStore());
entity.getAuthority().setPrivileges(entity.getPrivileges());
userDTO.setAuthority(entity.getAuthority());
userDTO.setCreatedBy(entity.getCreatedBy());
userDTO.setCreatedDate(entity.getCreatedDate());
userDTO.setModifiedBy(entity.getModifiedBy());
userDTO.setModifiedDate(entity.getModifiedDate());
return userDTO;
}
@Override
public List<User> toEntity(List<UserDTO> dtoList) {
if (dtoList == null || dtoList.isEmpty())
return null;
List<User> userList = new ArrayList<>(dtoList.size());
for (UserDTO userDTO : dtoList) {
userList.add(toEntity(userDTO));
}
return userList;
}
@Override
public List<UserDTO> toDTO(List<User> entityList) {
if (entityList == null)
return null;
List<UserDTO> userDTOList = new ArrayList<>(entityList.size());
for (User user : entityList) {
userDTOList.add(toDTO(user));
}
return userDTOList;
}
}