public class Source { private String name; private int age; // Getter and Setter methods } public class Destination { private String name; private int age; // Getter and Setter methods } @Mapper public interface MyMapper { MyMapper INSTANCE = Mappers.getMapper(MyMapper.class); Destination sourceToDestination(Source source); } Source source = new Source(); source.setName("John"); source.setAge(25); Destination destination = MyMapper.INSTANCE.sourceToDestination(source); public class Source { private int numOfProducts; // Getter and Setter methods } public class Destination { private String productCount; // Getter and Setter methods } @Mapper public interface MyMapper { MyMapper INSTANCE = Mappers.getMapper(MyMapper.class); @Mappings({ @Mapping(source = "numOfProducts", target = "productCount", qualifiedByName = "convertCount") }) Destination sourceToDestination(Source source); @Named("convertCount") default String convertCount(int numOfProducts) { return String.valueOf(numOfProducts); } } Source source = new Source(); source.setNumOfProducts(10); Destination destination = MyMapper.INSTANCE.sourceToDestination(source); public class Source { private String name; private int age; // Getter and Setter methods } public class Destination { private String name; // Getter and Setter methods } @Mapper public interface MyMapper { MyMapper INSTANCE = Mappers.getMapper(MyMapper.class); @Mappings({ @Mapping(source = "age", target = "age", ignore = true) }) Destination sourceToDestination(Source source); } Source source = new Source(); source.setName("John"); source.setAge(25); Destination destination = MyMapper.INSTANCE.sourceToDestination(source);


上一篇:
下一篇:
切换中文