mapstruct转换实体类、List对象、IPage对象

第一种方式:
mybatis-plus依赖要用高版本,低版本没有convert方法(测的时候用3.0.5发现没有convert方法)

<dependency>
	<groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.3</version>
</dependency>

mapstruct依赖

<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct-jdk8</artifactId>
    <version>1.2.0.Final</version>
</dependency>

<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct-processor</artifactId>
    <version>1.2.0.Final</version>
</dependency>

实体类和List转换

@Mapper
public interface EmpConverter {

    EmpConverter INSTANCE = Mappers.getMapper(EmpConverter.class);

    void entityToDTO(Emp entity, @MappingTarget EmpDTO dto);

    void entityListToDTOList(List<Emp> entity, @MappingTarget List<EmpDTO> dto);
}

//这里直接调用就好了
List<Emp> emps = baseMapper.selectList(lambda);
ArrayList<EmpDTO> empDTOs = new ArrayList<>();
EmpConverter.INSTANCE.entityListToDTOList(emps, empDTOs);

IPage对象转换

public IPage<Emp> queryPage(IPage<Emp> iPage,String code) {
	IPage<Emp> empIPage = baseMapper.queryPage(iPage, code);
	IPage<EmpDTO> convert = empIPage.convert(this::getEmpDTO);
}

public EmpDTO getEmpDTO(Emp emp){
    EmpDTO empDTO = new EmpDTO();
    EmpConverter.INSTANCE.entityToDTO(emp, empDTO);
    return empDTO;
}

第二种方式:

<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct</artifactId>
    <version>1.5.3.Final</version>
</dependency>

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>io.fabric8</groupId>
                <artifactId>docker-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source> <!-- depending on your project -->
                    <target>1.8</target> <!-- depending on your project -->
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>1.18.22</version>
                        </path>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${org.mapstruct.version}</version>
                        </path>
                        <!-- other annotation processors -->
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
        </plugins>
    </build>
public interface BaseObjectConverter {

	@Mapper(componentModel = "spring")
	public interface HaircutStoreEntityToVo extends BaseObjectMapper<HaircutStore, HaircutStoreVo>{

	}
}
//page转换
public IPage<HaircutStoreVo> queryPage(HaircutStoreDTO haircutStoreDTO, Page page) {
    IPage<HaircutStore> haircutStorePage = baseMapper.queryPage(haircutStoreDTO, page);
    IPage<HaircutStoreVo> haircutStoreVoPage = haircutStorePage.convert(this::getHaircutStoreVo);
}
//实体类转换
 @Override
public List<HaircutStoreVo> queryList(HaircutStoreDTO haircutStoreDTO) {
    List<HaircutStore> haircutStores = baseMapper.queryList(haircutStoreDTO);
    List<HaircutStoreVo> haircutStoreVoList = haircutStoreEntityToVo.to(haircutStores);
    return haircutStoreVoList;
}
//list转换
public HaircutStoreVo getHaircutStoreVo(HaircutStore entity){
    HaircutStoreVo sysTenantVo = haircutStoreEntityToVo.to(entity);
    return sysTenantVo;
}

第二种方式有个缺点,每次改动实体类都需要编译,否则报错

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值