• Java实体映射工具 MapStruct


    package com.enjoyit.ocbp.mapstruct;
    
    import java.util.List;
    
    public interface ObjectMapper<SELF, TARGET> {
        SELF toSelf(TARGET source);
    
        TARGET toTarget(SELF SELF);
    
        List<SELF> toSelf(List<TARGET> sources);
    
        List<TARGET> toTarget(List<SELF> SELVES);
    }
    

    定义:

    package com.enjoyit.ocbp.data.mapper;
    
    import com.enjoyit.ocbp.data.entities.SaleInfoDO;
    import com.enjoyit.ocbp.mapstruct.ObjectMapper;
    import com.enjoyit.ocbp.model.dto.SaleInfo;
    import org.mapstruct.Mapper;
    import org.mapstruct.factory.Mappers;
    
    @Mapper
    public interface SaleInfoObjectMapper extends ObjectMapper<SaleInfoDO, SaleInfo> {
        SaleInfoObjectMapper INSTANCE = Mappers.getMapper(SaleInfoObjectMapper.class);
    }
    

    使用:

    SaleInfo对象 转为 SaleInfoDO对象
    private void submitOrder(PosSession session, OrderExchange exchange) {
            insertRecord(getSaleHeadMapper(), exchange.getSaleHead());
            exchange.refreshSlaveRecordList();
            insertRecords(getSaleGoodsMapper(), exchange.getSaleGoodsList());
            insertRecords(getSaleDiscountMapper(), exchange.getSaleDiscountList());
            insertRecords(getSalePayMapper(), exchange.getSalePayList());
            insertRecords(getSaleInfoDOMapper(), SaleInfoObjectMapper.INSTANCE.toSelf(exchange.getSaleInfoList()));
    }
    SaleInfoDO对象 转为 SaleInfo对象
        protected List<SaleInfo> selectSaleInfoList(SaleInfoDO saleInfoDO ){
            SaleInfoDOMapper mapper = getSaleInfoDOMapper();
            List<SaleInfoDO> saleInfoDOS = mapper.select(saleInfoDO);
            return SaleInfoObjectMapper.INSTANCE.toTarget(saleInfoDOS);
        }
    







  • 相关阅读:
    intellij idea for mac 2018 破解版
    Mac下Supervisor进程监控管理工具的安装与配置
    Mysql千万级大表优化策略
    php7实现基于openssl的加密解密方法
    openresty--centos7下开发环境安装
    webstorm下搭建编译less环境 以及设置压缩css
    七牛图片上传
    聊一聊PHP的依赖注入(DI) 和 控制反转(IoC)
    joomla! 3.X 开发系列教程
    JSON反序列化接口的问题
  • 原文地址:https://www.cnblogs.com/ding08/p/13656408.html
Copyright © 2020-2023  润新知