• MapStruct使用技巧


      在设计数据库时,往往会增加一个字段features varchar(1000),用来做扩展字段,避免改表结构的。尤其是针对给某一条记录进行”打标操作“。

    那么,在DO类转为域模型对象时就有一个转换问题。

      下面介绍使用mapstruct优雅的进行转换

      

    package com.chinaunicom.base.server.converter;
    
    import com.chinaunicom.base.domain.model.userspace.UserSpace;
    import com.chinaunicom.base.server.converter.worker.UserSpaceConvert;
    import com.chinaunicom.base.server.dao.datamodel.UserSpaceEntity;
    import org.mapstruct.Mapper;
    import org.mapstruct.Mapping;
    import org.mapstruct.Mappings;
    import org.mapstruct.factory.Mappers;
    
    import java.util.List;
    
    @Mapper(componentModel = "spring", uses = {UserSpaceConvert.class})
    public interface UserSpaceBaseConverter {
        UserSpaceBaseConverter INSTANCES = Mappers.getMapper(UserSpaceBaseConverter.class);
    
        @Mappings({
                @Mapping(source = "id", target = "id"),
                @Mapping(source = "employeeName", target = "employeeName"),
                @Mapping(source = "rootSpace", target = "rootSpace"),
                @Mapping(source = "createTime", target = "createTime"),
                @Mapping(source = "deleted", target = "deleted")
        })
        UserSpaceEntity to(UserSpace userSpace);
    
        @Mappings({
                @Mapping(source = "id", target = "id"),
                @Mapping(source = "employeeName", target = "employeeName"),
                @Mapping(source = "rootSpace", target = "rootSpace"),
                @Mapping(source = "createTime", target = "createTime"),
                @Mapping(source = "deleted", target = "deleted"),
                @Mapping(target = "age", expression = "java(com.chinaunicom.base.server.converter.worker.UserSpaceConvert.getAgeFromFeatures(userSpaceEntity.getFeatures()))")
        })
        UserSpace back(UserSpaceEntity userSpaceEntity);
    
        List<UserSpaceEntity> toList(List<UserSpace> source);
    
        List<UserSpace> backList(List<UserSpaceEntity> source);
    }

      UserSpaceEntity 

    public class UserSpaceEntity {
        private Integer id;
        private String employeeName;
        private String rootSpace;
        private Date createTime;
        private Integer deleted;//1 删除 0 未删除
        private String features;
    
    }

      UserSpace

    public class UserSpace implements Serializable {
    
        private Integer id;
    
        private String employeeName;
    
        private String rootSpace;
    
        private Date createTime;
    
        private Integer deleted = 0;
    
        private Integer age;
    }

      其中 UserSpaceEntity 的features是保存的JSON格式数据,数据库中也是JSON

    public class UserSpaceConvert {
    
        public static Integer getAgeFromFeatures(String json) {
            Map<String,Object> valueMap = JSON.parseObject(json, Map.class);
            if (valueMap.containsKey("age")) {
                return Integer.valueOf(valueMap.get("age").toString());
            }
    
            return null;
        }
    }
  • 相关阅读:
    用JS + WCF打造轻量级WebPart
    提高WCF服务并发能力的简单处理办法
    利用JQuery实现更简单的Ajax跨域请求
    WCF Testing Tool(转)
    [转贴]一个有趣的布局
    [转贴].net中上传视频并将各种视频文件转换成.flv格式
    IE5,IE6,IE7,IE8的css兼容性列表[转自MSDN]
    [转贴]Castle 开发系列文章
    ie6,ie7,ff 的css兼容hack写法
    ExtJs学习笔记(23)ScriptTagProxy+XTemplate+WCF跨域取数据
  • 原文地址:https://www.cnblogs.com/juniorMa/p/16092810.html
Copyright © 2020-2023  润新知