栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java

方法一:前提是属性名一致,如果两个类属性名不一致,赋值不上了

// 将实体类Entity属性赋值给EntityVo
BeanUtils.copyProperties(Entity,EntityVo);

方法二:如果两个类的属性值不同,可以用下面的赋值,
除了单个对象,集合对象也是可以的。
还可以两个对象合并成一个对象。
1、POM依赖

    
            org.mapstruct
            mapstruct-jdk8
            1.2.0.CR1
        
        
            org.mapstruct
            mapstruct-processor
            1.2.0.CR1
        

2 、自定义接口

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;

@Mapper
// 注意导入的是 mapstruct包
public interface EntityMapper {
    EntityMapper mapper = Mappers.getMapper(EntityMapper.class);
	// 映射关系注解
    @Mappings({
            @Mapping(source="entytyId",target = "id"),
            @Mapping(source="entytyName",target = "name"),
            @Mapping(source="entytyAge",target = "age")
    })
    EntityVo Entity2Vo(Entity entity);
}

3、调用接口进行赋值

EntityVo entityVo = EntityMapper .mapper.Entity2Vo(entity);
System.out.println(entityVo );
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/981173.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号