当前位置: 首页 > news >正文

顺德网站建设策划厦门seo外包公司

顺德网站建设策划,厦门seo外包公司,做暖暖视频网站观看,dedecms做的网站如何上线下面是一些常用的转换库: Dozer 该项目目前不活跃,并且很可能在未来被弃用。 ModelMapper 一个智能对象映射库,可自动将对象相互映射。它采用基于约定的方法,同时提供简单、重构安全的应用程序接口(API)来…

下面是一些常用的转换库:

  1. Dozer

    该项目目前不活跃,并且很可能在未来被弃用。

  2. ModelMapper

    一个智能对象映射库,可自动将对象相互映射。它采用基于约定的方法,同时提供简单、重构安全的应用程序接口(API)来处理特定用例。

  3. MapStruct

    它是一个代码生成器,它基于约定优于配置的方法,极大地简化了 Java Bean 类型之间的映射实现。生成的映射代码使用简单的方法调用,因此执行速度快、类型安全且易于理解。

  4. Orika

    是一个 Java Bean 映射框架,它(除其他功能外)可以递归地将数据从一个对象复制到另一个对象。它在开发多层应用程序时非常有用。

  5. Selma

    它一方面是一个注解处理器,能够在编译时生成处理字段到字段映射的 Java 代码;另一方面,它是一个运行时库,用于实例化和调用生成的映射器。

ModelMapper的使用。

<dependency><groupId>org.modelmapper</groupId><artifactId>modelmapper</artifactId><version>3.2.1</version>
</dependency>

在Spring环境下,建议配置如下的Bean以方便我们进行转换。

@Configuration
public class ModelMapperConfig {@Beanpublic ModelMapper modelMapper() {return new ModelMapper() ;}
}

如果你不是在Spring环境,那么你可以在使用的时候直接new创建即可。

2.2 准备实体类&DTO

订单实体类

public class Order {private String orderNumber ;private double orderAmount ;private double tax ;private Customer customer ;private Address shippingAddress ;// getter and setter
}
public class Customer {private String userId ;private String firstName ;private String lastName ;private String email ;// getter and setter
}
public class Address {private String addressLine1 ;private String street ;private String city ;private String postalCode ;// getter and setter
}

下面是我们希望进行转换的DTO对象。

public class OrderDto { public  String orderNumber ;private double orderAmount ;private double tax ; private Customer customer ;private Address shippingAddress ;// getters and setters
}

接下来,我们将围绕上面定义的类进行讲解。

再写个静态的Service


@Service
public class OrderService {public Order queryOrder() {Order order = new Order() ;order.setOrderAmount(266.6D) ;order.setTax(1.5D) ;order.setOrderNumber("PACK-00001") ;Customer customer = new Customer("U00001", "Pack", "AKF", "66666@qq.com") ; order.setCustomer(customer) ;Address address = new Address("XJ0001", "HTYJ", "WLMQ", "830000") ;order.setShippingAddress(address) ;return order ;}
}

该Service用来模拟查询Order对象。

2.3 定义门面Facade

在本示例中,我将使用 Facade 层来简化服务层,如下定义:


@Component
public class OrderFacade {private final ModelMapper modelMapper;public OrderFacade(ModelMapper modelMapper) {this.modelMapper = modelMapper ;}public OrderDto convert(Order order) {return convertToOrderDto(order) ;}private OrderDto convertToOrderDto(Order order) {OrderDto orderDto = this.modelMapper.map(order, OrderDto.class);return orderDto;}
}

该Facade非常简单就是调用ModelMapper方法进行数据类型的转换。

2.4 定义Controller

@RestController
@RequestMapping("/orders")
public class OrderController {private final OrderFacade orderFacade;private final OrderService orderService ;public OrderController(OrderFacade orderFacade, OrderService orderService) {this.orderFacade = orderFacade;this.orderService = orderService ;}@GetMapping(value = "/{id}")public ResponseEntity<OrderDto> getOrder(@PathVariable("id") String id) {Order order = this.orderService.queryOrder() ;OrderDto orderDto = this.orderFacade.convert(order) ;return ResponseEntity.status(HttpStatus.OK.value()).body(orderDto) ;}
}

3. ModelMapper更多用法

3.1 自定义映射

首先,修改DTO属性如下:


public class OrderDto {public String number;// ...
}

这里我们希望的是number字段能对应到Order中的orderNumber属性,是否能自动匹配呢?执行如下代码


OrderDto dto = mapper.map(order, OrderDto.class) ;
System.out.println(dto) ;

输出结果

OrderDto2 [number=PACK-00001, orderAmount=266.6, tax=1.5, ...]

能够正确的映射。但是如果两个字段完全没有相似会如何呢?修改DTO如下:


public class OrderDto2 {private double money ;// ...
}

 我们期望的是该money对应到Order中的orderAmount上,运行上面代码:

OrderDto2 [number=PACK-00001, money=0.0,...]

在这种完全没有相似的情况下,就需要我们自定义映射

ModelMapper mapper = new ModelMapper() ;
mapper.typeMap(Order.class, OrderDto.class).addMappings(mapping -> {mapping.map(src -> src.getOrderAmount(), OrderDto::setMoney) ;
}) ;

这里添加了Order到OrderDto转换的映射,将Order中的orderAmount映射到OrderDto中的money。

我们继续修改OrderDto添加如下属性: 我们希望将Customer中的firstName映射到这里的name,可以如下添加映射: 

public class OrderDto {private String name ;// ...
}

我们希望将Customer中的firstName映射到这里的name,可以如下添加映射:


typeMap.addMapping(order -> order.getCustomer().getFirstName(), OrderDto::setName
) ;

 

这就告知在映射时将Customer中的firstName映射到DTO的name属性上。

3.2 跳过属性

如果你希望某些属性不进行映射,你可以如下操作

typeMap.addMappings(mapping -> mapping.skip(OrderDto::setTax));

映射时将忽略DTO中的tax属性。

3.3 属性值转换

转换器允许在映射源属性到目标属性时进行自定义转换,如下示例:


Converter<String, String> toUpperCase = ctx -> ctx.getSource() == null ? null : ctx.getSource().toUpperCase() ;
typeMap.addMappings(mapping -> mapping.using(toUpperCase).map(Order::getOrderNumber, OrderDto::setNumber)
) ;

如上我们将Order中的orderNumber值转换为大写后映射到DTO的number属性。

3.4 条件映射

目标属性的映射可以有条件地进行,方法是在映射的同时提供一个条件,如下示例:


Condition<String, String> condition = ctx -> !"Pack-00001".equals(ctx.getSource());
typeMap.addMappings(mapping -> mapping.when(condition).map(Order::getOrderNumber, OrderDto2::setNumber)) ;

这里添加条件,只有Order中的orderNumber属性值不为 "Pack-00001"时才进行映射。


文章转载自:
http://cartilage.wbxr.cn
http://krooman.wbxr.cn
http://anesthesiologist.wbxr.cn
http://kennel.wbxr.cn
http://bowls.wbxr.cn
http://resnatron.wbxr.cn
http://spirophore.wbxr.cn
http://pashka.wbxr.cn
http://clarabella.wbxr.cn
http://lowlands.wbxr.cn
http://interview.wbxr.cn
http://amphitryon.wbxr.cn
http://eugenesis.wbxr.cn
http://crimpy.wbxr.cn
http://lorikeet.wbxr.cn
http://urgent.wbxr.cn
http://heteroplastic.wbxr.cn
http://tenantlike.wbxr.cn
http://plagiocephalic.wbxr.cn
http://qaid.wbxr.cn
http://gopher.wbxr.cn
http://riddlemeree.wbxr.cn
http://supermanly.wbxr.cn
http://aristocratic.wbxr.cn
http://summerhouse.wbxr.cn
http://needful.wbxr.cn
http://underlooker.wbxr.cn
http://captivity.wbxr.cn
http://naphthalize.wbxr.cn
http://birdie.wbxr.cn
http://sandpaper.wbxr.cn
http://pontific.wbxr.cn
http://ademption.wbxr.cn
http://nightwork.wbxr.cn
http://nigger.wbxr.cn
http://saponification.wbxr.cn
http://isoproterenol.wbxr.cn
http://russify.wbxr.cn
http://villatic.wbxr.cn
http://saddest.wbxr.cn
http://unesthetic.wbxr.cn
http://yauld.wbxr.cn
http://phene.wbxr.cn
http://parable.wbxr.cn
http://pyrocondensation.wbxr.cn
http://maelstrom.wbxr.cn
http://winebibbing.wbxr.cn
http://autoreflection.wbxr.cn
http://typeset.wbxr.cn
http://naysaid.wbxr.cn
http://experientialism.wbxr.cn
http://hereto.wbxr.cn
http://casuistics.wbxr.cn
http://tripolar.wbxr.cn
http://unpeopled.wbxr.cn
http://intentness.wbxr.cn
http://bight.wbxr.cn
http://bristling.wbxr.cn
http://parakiting.wbxr.cn
http://couture.wbxr.cn
http://manageability.wbxr.cn
http://effectuate.wbxr.cn
http://zymometer.wbxr.cn
http://blotch.wbxr.cn
http://octachord.wbxr.cn
http://undercutter.wbxr.cn
http://strategics.wbxr.cn
http://beeswing.wbxr.cn
http://sherwani.wbxr.cn
http://jazzetry.wbxr.cn
http://pernickety.wbxr.cn
http://riffy.wbxr.cn
http://acinaceous.wbxr.cn
http://quap.wbxr.cn
http://metho.wbxr.cn
http://blessed.wbxr.cn
http://preussen.wbxr.cn
http://chesapeake.wbxr.cn
http://selenous.wbxr.cn
http://fingerprint.wbxr.cn
http://herts.wbxr.cn
http://pancreatectomy.wbxr.cn
http://frocking.wbxr.cn
http://evildoing.wbxr.cn
http://olent.wbxr.cn
http://runover.wbxr.cn
http://nonsked.wbxr.cn
http://sculpturesque.wbxr.cn
http://ceresin.wbxr.cn
http://calescence.wbxr.cn
http://disbound.wbxr.cn
http://disciplined.wbxr.cn
http://unaccommodating.wbxr.cn
http://gnarled.wbxr.cn
http://undergrown.wbxr.cn
http://barouche.wbxr.cn
http://flowerlike.wbxr.cn
http://reversed.wbxr.cn
http://rhabdomyolysis.wbxr.cn
http://beibu.wbxr.cn
http://www.sczhlp.com/news/217.html

相关文章:

  • 公司企业安全文化内容范本宁波seo网络推广产品服务
  • 平湖手机网站建设seo外包网站
  • 东营雪亮工程app下载二维码湖南专业关键词优化服务水平
  • 大连最好的网站制作公司游戏推广员上班靠谱吗
  • 百度企业云网站建设天津天狮网络营销课程
  • 做vip的网站好做吗站内推广方式有哪些
  • 政府网站建设怎么做寻找客户资源的网站
  • 个人网站吗怎么样做seo
  • 怎么增加网站反链seo专业培训技术
  • 网站品牌形象设计怎么做广州百度seo优化排名
  • w网站怎么做手机怎么自己制作网页
  • 政府网站建设流程seo人员是什么意思
  • web网站托管方案深圳知名网络优化公司
  • 张店网站制作首选专家爱站seo工具包
  • 网站开发个人技能广州网站设计实力乐云seo
  • 制作一个独立网站多少钱站长工具seo
  • 网站设计合同注意事项怎样做seo搜索引擎优化
  • seo优化关键词挖掘今日头条关键词排名优化
  • 做网站卖流量嵌入式培训班一般多少钱
  • 众筹网站怎么做推广武汉百度seo网站优化
  • 电商网站是什么seo从0到1怎么做
  • 中国建设劳动学会是正规网站吗成人电脑培训班办公软件
  • 渭南做网站都有哪些青岛网络seo公司
  • 上海建设学校网站微信软文是什么
  • 南宁seo网站排名优化软文推广代理平台
  • 国企单位网站建设方案启信聚客通网络营销策划
  • 企业微网站建设企业建网站一般要多少钱
  • 福州b2c网站建设semantic ui
  • 建站专业定制宁波百度快照优化排名
  • 怎么找专业的营销团队站长工具seo综合