我有4个Java类,如下所述。手动创建bean映射器非常耗时。该库可以自动生成bean映射器类。
public class UserDao {
private int id;
private String name;
private List<AddressDao> address;
}
public class AddressDao {
public String city;
public String Country;
}
public class UserDto {
private int userID;
private String userName;
private List<AddressDto> userAddress;
}
public class AddressDto {
private String userCity;
private String userCountry;
}
如何将用户地址变量与UserD映射到地址变量。
@Mappings({
@Mapping(source ="id" ,target="userID"),
@Mapping(source ="name" ,target="userName"),
@Mapping(source ="userdao.address.city" ,target="userAddress.userCity"),
@Mapping(source ="userdao.address.country" ,target="userAddress.userCountry")
})
public UserDto usertouserDto(UserDao userdao);
public List<UserDto> usertoUserDtos(List<UserDao> userDao);
这种方式不起作用。
@@ Mapper(componentModel =“ spring”,使用= {AddressMapper.class})公用接口UserdtoMapper {
@Mapping(source ="id" ,target="userID")
@Mapping(source ="name" ,target="userName")
UserDto usertouserDto(UserDao userdao);
List<UserDto> usertoUserDtos(List<UserDao> userDao);
}
@@ Mapper(componentModel =“ spring”)公用接口AddressMapper {
@Mappings({
@Mapping(source ="city" ,target="userCity"),
@Mapping(source ="Country" ,target="userCountry")
})
AddressDto mappingAddressDto(AddressDao addressDao);
List<AddressDto> mappingAddressDto(List<AddressDao> addressDao);
}
使用解决了我的问题。