最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java中ModelMapper?的高級(jí)使用

 更新時(shí)間:2022年02月21日 11:57:17   作者:lytao123  
本文主要介紹了Java中ModelMapper?的高級(jí)使用,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

ModelMapper 高級(jí)使用

  ModelMapper 是一個(gè) Object To Object 的工具,類似于 MapStruct又不同于 MapStruct。主要原因是 ModelMapper 是利用反射的原理實(shí)現(xiàn)的 Object To Object。

  ModelMapper 官方API : http://modelmapper.org/user-manual/property-mapping/

使用實(shí)例

本實(shí)例實(shí)現(xiàn)了條件映射、嵌套映射(對(duì)象中有對(duì)象映射)、自定義屬性映射、List 集合映射(對(duì)象中有集合映射)、Map集合映射(對(duì)象中有集合映射)、忽略映射,默認(rèn)值設(shè)置(ModelMapper 的默認(rèn)值設(shè)置時(shí)一不小心就會(huì)入坑,如果直接設(shè)置默認(rèn)值,當(dāng)再賦值轉(zhuǎn)換時(shí),默認(rèn)值會(huì)覆蓋賦值的值,所以設(shè)置默認(rèn)值需要結(jié)合條件映射)等。
驗(yàn)證了對(duì)象屬性為集合,集合中還有集合能夠使用 ModelMapper 進(jìn)行轉(zhuǎn)換。不足點(diǎn)是這個(gè)實(shí)例中沒(méi)有驗(yàn)證有繼承關(guān)系時(shí)的映射(使用 modelMapper.includeBase(父類1, 父類2) 方法),多個(gè)屬性映射為一個(gè)屬性或一個(gè)屬性映射為多個(gè)屬性(使用 PropertyMap 轉(zhuǎn)換器)。

  • 使用條件映射設(shè)置默認(rèn)值。當(dāng) age/createTime 沒(méi)有值時(shí)設(shè)置默認(rèn)值為18/當(dāng)前時(shí)間,有值時(shí)不設(shè)置默認(rèn)值;
  • 嵌套映射,自定義屬性映射。Source 的 sourceSon 成員變量 映射到 Destination 的 destinationSon 成員變量;
  • List集合映射。Source 的 listSon 成員變量 映射到 Destination 的 sonList 成員變量;
  • Map集合映射。Source 的 mapSon 成員變量 映射到 Destination 的 sonMap 成員變量;
  • 忽略映射。忽略 Destination 的 excessParam 成員變量,如果不忽略將驗(yàn)證不過(guò),報(bào) org.modelmapper.MappingException: ModelMapper mapping errors;

實(shí)體類

(1)BaseClass

@Getter
@Setter
public 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
@Setter
public class SouSubClass {

? ? private String sonId;
? ? private String sonName;
? ? private List<BaseClass> grandSons;

? ? public SouSubClass() {

? ? }

? ? public SouSubClass(String sonId, String sonName) {
? ? ? ? this.sonId = sonId;
? ? ? ? this.sonName = sonName;
? ? }
}

(3)DestSubClass

@Getter
@Setter
public class DestSubClass {

? ? private String dsonId;
? ? private String sonName;
? ? private String excessParam;
? ? private List<BaseClass> grandSons;

? ? public DestSubClass(){

? ? }

? ? public DestSubClass(String dsonId, String sonName) {
? ? ? ? this.dsonId = dsonId;
? ? ? ? this.sonName = sonName;
? ? }
}

(4)Source

@Getter
@Setter
public class Source {
? ? private String id;
? ? private String name;
? ? private Integer age;
? ? private SouSubClass sourceSon;
? ? private List<SouSubClass> listSon;
? ? private Map<Integer, SouSubClass> 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
@Setter
public class Destination {
? ? private Long id;
? ? private String name;
? ? private Integer age;
? ? private DestSubClass destinationSon;
? ? private List<DestSubClass> sonList;
? ? private Map<Integer, DestSubClass> sonMap;
? ? private String excessParam;
? ? private Date createTime;

? ? public Destination() {

? ? }

? ? public Destination(Long id, String name) {
? ? ? ? this.id = id;
? ? ? ? this.name = name;
? ? }
}

ModelMapper 配置類

/**
?* 描述:ModelMapper 配置
?*/
@Configuration
public class ModelMapperConfig {

? ? @Bean
? ? @Scope("singleton")
? ? public ModelMapper getModelMapper() {
? ? ? ? ModelMapper modelMapper = new ModelMapper();
? ? ? ? //默認(rèn)為standard模式,設(shè)置為strict模式
? ? ? ? modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);

? ? ? ? // 類型映射代碼
? ? ? ? sourceSonToDestinationSon(modelMapper);
? ? ? ? sourceToDestination(modelMapper);

? ? ? ? //驗(yàn)證映射
? ? ? ? modelMapper.validate();

? ? ? ? // 配置代碼
? ? ? ? return modelMapper;
? ? }

? ? /**
? ? ?* 描述:聲明 Source 類轉(zhuǎn) Destination 類的 Mapper
? ? ?* @param modelMapper
? ? ?* @Date ?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<Integer, Integer>) context -> {
? ? ? ? ? ? ? ? ? ? ? ? if (context.getSource() == null) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? return 18;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? return context.getSource();
? ? ? ? ? ? ? ? ? ? }).map(Source::getAge, Destination::setAge))
? ? ? ? ? ? ? ? .addMappings(mapper -> mapper.using((Converter<Date, Date>) 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 類

public interface TestService {

? ? Destination testSourceToDestination(Source source);

? ? List<Destination> testSourceToDestinationList(List<Source> sources);
}
@Service
public class TestServiceImpl implements TestService {

? ? @Autowired
? ? private ModelMapper modelMapper;

? ? @Override
? ? public Destination testSourceToDestination(Source source) {
? ? ? ? Destination destination = modelMapper.map(source, Destination.class);

? ? ? ? return destination; ?// a 處
? ? }

? ? @Override
? ? public List<Destination> testSourceToDestinationList(List<Source> sources) {
? ? ? ? Type type = new TypeToken<List<Destination>>(){}.getType();
? ? ? ? List<Destination> destinations = modelMapper.map(sources, type);

? ? ? ? return destinations; // b 處
? ? }
}

測(cè)試類

@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestApplication.class)
public class TestServiceImplTest {

? ? @Autowired
? ? private TestService testService;

? ? private static Source source1 = new Source("a", "發(fā)生的", 24);
? ? private static Source source2 = new Source("b", "發(fā)生的");

? ? private static List<Source> sources = new ArrayList<>();

? ? static {
? ? ? ? List<BaseClass> baseClasses1 = new ArrayList<>();
? ? ? ? baseClasses1.add(new BaseClass("aa", "發(fā)生的111"));
? ? ? ? baseClasses1.add(new BaseClass("bb", "發(fā)生的222"));
? ? ? ? SouSubClass subClass1 = new SouSubClass("aaa", "發(fā)生的3333");
? ? ? ? subClass1.setGrandSons(baseClasses1);

? ? ? ? List<BaseClass> baseClasses2 = new ArrayList<>();
? ? ? ? baseClasses2.add(new BaseClass("cc", "國(guó)防觀"));
? ? ? ? baseClasses2.add(new BaseClass("dd", "國(guó)防觀"));
? ? ? ? SouSubClass subClass2 = new SouSubClass("ccc", "規(guī)范的大哥");
? ? ? ? subClass2.setGrandSons(baseClasses2);

? ? ? ? List<SouSubClass> sourceSonList = new ArrayList<>();
? ? ? ? sourceSonList.add(subClass1);
? ? ? ? sourceSonList.add(subClass2);

? ? ? ? Map<Integer, SouSubClass> 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);
? ? }
}

測(cè)試結(jié)果

在 ab 兩處打上斷點(diǎn),查看變量轉(zhuǎn)換前后的值,證實(shí)轉(zhuǎn)換成功。

到此這篇關(guān)于Java中ModelMapper 的高級(jí)使用的文章就介紹到這了,更多相關(guān)Java ModelMapper內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • spring帶bean和config如何通過(guò)main啟動(dòng)測(cè)試

    spring帶bean和config如何通過(guò)main啟動(dòng)測(cè)試

    這篇文章主要介紹了spring帶bean和config,通過(guò)main啟動(dòng)測(cè)試,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-07-07
  • Java中使用LocalDate根據(jù)日期來(lái)計(jì)算年齡的實(shí)現(xiàn)方法

    Java中使用LocalDate根據(jù)日期來(lái)計(jì)算年齡的實(shí)現(xiàn)方法

    這篇文章主要介紹了Java中使用LocalDate根據(jù)日期來(lái)計(jì)算年齡的實(shí)現(xiàn)方法,需要的朋友可以參考下
    2018-01-01
  • Springboot通過(guò)谷歌Kaptcha?組件生成圖形驗(yàn)證碼功能

    Springboot通過(guò)谷歌Kaptcha?組件生成圖形驗(yàn)證碼功能

    Kaptcha是谷歌開(kāi)源的一款簡(jiǎn)單實(shí)用的圖形驗(yàn)證碼組件。我個(gè)人推薦它的最大原因是容易上手,采用約定大于配置的方式,快速契合到項(xiàng)目中,這篇文章主要介紹了Springboot通過(guò)谷歌Kaptcha組件生成圖形驗(yàn)證碼的方法,需要的朋友可以參考下
    2023-05-05
  • Springboot項(xiàng)目的服務(wù)器部署與發(fā)布方式

    Springboot項(xiàng)目的服務(wù)器部署與發(fā)布方式

    本文記錄了將Springboot項(xiàng)目部署到服務(wù)器并發(fā)布的過(guò)程,包括在IDEA中打包、選擇服務(wù)器、連接服務(wù)器、安裝環(huán)境、上傳jar包、配置環(huán)境變量以及運(yùn)行項(xiàng)目等步驟
    2025-03-03
  • 詳解Java?ReentrantReadWriteLock讀寫鎖的原理與實(shí)現(xiàn)

    詳解Java?ReentrantReadWriteLock讀寫鎖的原理與實(shí)現(xiàn)

    ReentrantReadWriteLock讀寫鎖是使用AQS的集大成者,用了獨(dú)占模式和共享模式。本文和大家一起理解下ReentrantReadWriteLock讀寫鎖的實(shí)現(xiàn)原理,需要的可以了解一下
    2022-10-10
  • spring boot中nativeQuery的用法

    spring boot中nativeQuery的用法

    這篇文章主要介紹了spring boot中nativeQuery的作用,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-07-07
  • Java選擇排序和垃圾回收機(jī)制詳情

    Java選擇排序和垃圾回收機(jī)制詳情

    這篇文章主要介紹Java選擇排序和垃圾回收機(jī)制,創(chuàng)建對(duì)象就會(huì)占據(jù)內(nèi)存,如果程序在執(zhí)行過(guò)程中不能再使用某個(gè)對(duì)象,這個(gè)對(duì)象是徒耗內(nèi)存的垃圾,下面來(lái)看看文章具體內(nèi)容吧
    2021-10-10
  • Java網(wǎng)絡(luò)編程實(shí)現(xiàn)多線程聊天

    Java網(wǎng)絡(luò)編程實(shí)現(xiàn)多線程聊天

    這篇文章主要為大家詳細(xì)介紹了Java網(wǎng)絡(luò)編程實(shí)現(xiàn)多線程聊天,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • SpringBoot用實(shí)體接收Get請(qǐng)求傳遞過(guò)來(lái)的多個(gè)參數(shù)的兩種方式

    SpringBoot用實(shí)體接收Get請(qǐng)求傳遞過(guò)來(lái)的多個(gè)參數(shù)的兩種方式

    本文主要介紹SpringBoot用實(shí)體接收Get請(qǐng)求傳遞過(guò)來(lái)的多個(gè)參數(shù),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Java進(jìn)階核心之InputStream流深入講解

    Java進(jìn)階核心之InputStream流深入講解

    這篇文章主要給大家介紹了關(guān)于Java進(jìn)階核心之InputStream流的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02

最新評(píng)論

曲麻莱县| 德昌县| 焦作市| 九龙县| 遂溪县| 巴彦淖尔市| 华坪县| 敦煌市| 苏尼特左旗| 彭水| 岑巩县| 秦皇岛市| 潼南县| 濮阳县| 营口市| 怀集县| 昌图县| 仙居县| 凌海市| 公主岭市| 宁远县| 浦东新区| 交城县| 鄂温| 泽库县| 陵水| 阳东县| 黔东| 兴安县| 德清县| 秦皇岛市| 苏尼特右旗| 长海县| 临城县| 昭觉县| 萨嘎县| 安溪县| 甘德县| 临江市| 定西市| 吉木萨尔县|