In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "what are the advanced uses of ModelMapper in Java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Advanced use of ModelMapper
ModelMapper is a tool for Object To Object, similar to MapStruct but different from MapStruct. The main reason is that ModelMapper is an Object To Object based on the principle of reflection.
Use an example
This example implements conditional mapping, nested mapping (there is object mapping in the object), custom attribute mapping, List set mapping (there is set mapping in the object), Map set mapping (set mapping in the object), ignore mapping, default value setting (ModelMapper default value setting will accidentally enter the pit, if you directly set the default value, when you assign a transformation, the default value will override the assigned value. So setting the default value needs to be combined with conditional mapping), and so on.
It is verified that the object property is a collection, and there are collections in the collection that can be converted using ModelMapper. The shortcoming is that there is no mapping in this example when the inheritance relationship is validated (using the modelMapper.includeBase (parent class 1, parent class 2) method), and multiple attributes are mapped to one attribute or to multiple attributes (using the PropertyMap converter).
Use conditional mapping to set default values. Set the default value to 18 / current time when age/createTime has no value, and do not set the default value when there is a value.
Nested mapping, custom attribute mapping. Mapping the sourceSon member variable of Source to the destinationSon member variable of Destination
List collection mapping. Mapping the listSon member variable of Source to the sonList member variable of Destination
Map collection mapping. Mapping the mapSon member variable of Source to the sonMap member variable of Destination
Ignore mapping. Ignore the excessParam member variable of Destination. If you don't ignore it, you will verify it. Report org.modelmapper.MappingException: ModelMapper mapping errors.
Entity class
(1) BaseClass
@ Getter@Setterpublic class BaseClass {private String id; private String name; public BaseClass () {} public BaseClass (String id, String name) {this.id = id; this.name = name;}}
(2) SouSubClass
@ Getter@Setterpublic class SouSubClass {private String sonId; private String sonName; private List grandSons; public SouSubClass () {} public SouSubClass (String sonId, String sonName) {this.sonId = sonId; this.sonName = sonName;}}
(3) DestSubClass
@ Getter@Setterpublic class DestSubClass {private String dsonId; private String sonName; private String excessParam; private List grandSons; public DestSubClass () {} public DestSubClass (String dsonId, String sonName) {this.dsonId = dsonId; this.sonName = sonName;}}
(4) Source
@ Getter@Setterpublic class Source {private String id; private String name; private Integer age; private SouSubClass sourceSon; private List listSon; private Map mapSon; private Date createTime; public Source () {} public Source (String id, String name) {this.id = id; this.name = name;} public Source (String id, String name, Integer age) {this.id = id; this.name = name This.age = age;}}
(5) Destination
@ Getter@Setterpublic class Destination {private Long id; private String name; private Integer age; private DestSubClass destinationSon; private List sonList; private Map sonMap; private String excessParam; private Date createTime; public Destination () {} public Destination (Long id, String name) {this.id = id; this.name = name }} ModelMapper configuration class / * description: ModelMapper configuration * / @ Configurationpublic class ModelMapperConfig {@ Bean @ Scope ("singleton") public ModelMapper getModelMapper () {ModelMapper modelMapper = new ModelMapper (); / / default to standard mode, set to strict mode modelMapper.getConfiguration (). SetMatchingStrategy (MatchingStrategies.STRICT); / / type mapping code sourceSonToDestinationSon (modelMapper); sourceToDestination (modelMapper) / / verify mapping modelMapper.validate (); / / configure code return modelMapper } / * description: declare Mapper * @ param modelMapper * @ Date of Source class to Destination class 2019-05-09 * / private void sourceSonToDestinationSon (ModelMapper modelMapper) {modelMapper.createTypeMap (SouSubClass.class, DestSubClass.class) .addMapping (SouSubClass::getSonId, DestSubClass::setDsonId) .addMapping (SouSubClass::getSonName) DestSubClass::setSonName) .addMappings (mapper-> mapper.skip (DestSubClass::setExcessParam)) } private void sourceToDestination (ModelMapper modelMapper) {modelMapper.createTypeMap (Source.class, Destination.class) .addMappings (mapper-> mapper.using ((Converter) context-> {if (context.getSource () = = null) {return 18;} return context.getSource () }) .map (Source::getAge, Destination::setAge) .addMappings (mapper-> mapper.using ((Converter) context-> {if (context.getSource () = = null) {return new Date ();} return context.getSource () }) .map (Source::getCreateTime, Destination::setCreateTime) .addMapping (Source::getSourceSon, Destination::setDestinationSon) .addMapping (Source::getListSon, Destination::setSonList) .addMapping (Source::getMapSon, Destination::setSonMap) .addMappings (mapper-> mapper.skip (Destination::setExcessParam)) }} ModelMapper Service class public interface TestService {Destination testSourceToDestination (Source source); List testSourceToDestinationList (List sources);} @ Servicepublic class TestServiceImpl implements TestService {@ Autowired private ModelMapper modelMapper; @ Override public Destination testSourceToDestination (Source source) {Destination destination = modelMapper.map (source, Destination.class); at return destination; / / a} @ Override public List testSourceToDestinationList (List sources) {Type type = new TypeToken () {} .getType () List destinations = modelMapper.map (sources, type); return destinations; / / b}} Test class @ RunWith (SpringRunner.class) @ SpringBootTest (classes = TestApplication.class) public class TestServiceImplTest {@ Autowired private TestService testService; private static Source source1 = new Source ("a", "happened", 24); private static Source source2 = new Source ("b", "happened"); private static List sources = new ArrayList () Static {List baseClasses1 = new ArrayList (); baseClasses1.add (new BaseClass ("aa", "aa"); baseClasses1.add (new BaseClass ("bb", "3333")); SouSubClass subClass1 = new SouSubClass ("aaa", "3333"); subClass1.setGrandSons (baseClasses1); List baseClasses2 = new ArrayList () BaseClasses2.add (new BaseClass ("cc", "National Defense Outlook"); baseClasses2.add (new BaseClass ("dd", "National Defense Outlook"); SouSubClass subClass2 = new SouSubClass ("ccc", "normative Big Brother"); subClass2.setGrandSons (baseClasses2); List sourceSonList = new ArrayList (); sourceSonList.add (subClass1); sourceSonList.add (subClass2); Map sourceSonMap = new HashMap () SourceSonMap.put (1, subClass1); sourceSonMap.put (2, subClass2); source1.setCreateTime (new Date (System.currentTimeMillis ()-98978609); source1.setSourceSon (subClass1); source1.setListSon (sourceSonList); source1.setMapSon (sourceSonMap); source2.setSourceSon (subClass1); source2.setListSon (sourceSonList); source2.setMapSon (sourceSonMap); sources.add (source1) Sources.add (source2);} @ Test public void testSourceToDestination () {testService.testSourceToDestination (source1); testService.testSourceToDestination (source2);} @ Test public void testSourceToDestinationList () {testService.testSourceToDestinationList (sources);}} Test results
Make breakpoints in both ab and check the values before and after the variable conversion to confirm that the conversion is successful.
This is the end of the content of "what are the advanced uses of ModelMapper in Java". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.