当前位置:网站首页>Entity到Vo的转换
Entity到Vo的转换
2022-08-11 02:14:00 【莫欺少中老年穷】
entity —》 VO
1、pom.xml 导入依赖
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId>
<version>1.2.0.CR1</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.2.0.CR1</version>
<scope>provided</scope>
</dependency>
2、自定义接口
package com.southwind.mapper;
import com.southwind.entity.Category;
import com.southwind.entity.Product;
import com.southwind.vo.OrderVO;
import com.southwind.vo.ProductVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;
import java.util.List;
@Mapper
public interface ProductMapper {
ProductMapper mapper = Mappers.getMapper(ProductMapper.class);
@Mappings({
@Mapping(source = "productId",target = "id"),
@Mapping(source = "productName",target = "name"),
@Mapping(source = "productPrice",target = "price"),
@Mapping(source = "productStatus",target = "status",qualifiedByName = "convertStatus")
})
ProductVO pojo2vo(Product product);
@Named("convertStatus")
default Boolean convertStatus(Integer status){
if(status == 1)return true;
return false;
}
List<ProductVO> list2vo(List<Product> list);
@Mappings({
@Mapping(source = "product.productName",target = "product"),
@Mapping(source = "category.categoryName",target = "category")
})
OrderVO createOrder(Product product, Category category);
}
3、测试类
package com.southwind.mapper;
import com.southwind.entity.Category;
import com.southwind.entity.Product;
import com.southwind.vo.OrderVO;
import com.southwind.vo.ProductVO;
import org.junit.Test;
import org.springframework.beans.BeanUtils;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
public class VOTest {
@Test
public void test() {
Product product = new Product(1,"电脑", new BigDecimal(6000),1);
ProductVO productVO = ProductMapper.mapper.pojo2vo(product);
System.out.println(productVO);
}
@Test
public void list(){
List<Product> list = new ArrayList<>();
for (int i = 0; i < 3; i++) {
list.add(new Product(i,"电脑"+i,new BigDecimal(6000),1));
}
List<ProductVO> productVOS = ProductMapper.mapper.list2vo(list);
for (ProductVO productVO : productVOS) {
System.out.println(productVO);
}
}
@Test
public void order(){
Product product = new Product(1, "电脑", new BigDecimal(6000),1);
Category category = new Category("电子产品");
OrderVO order = ProductMapper.mapper.createOrder(product, category);
System.out.println(order);
}
}
边栏推荐
- Ora - 00001 in violation of the only constraint
- 0图中等 LeetCode565. 数组嵌套
- 自动生成数据库设计文档利器
- Mysq_Note4
- Fatal error in launcher: Unable to create process using xxx --logdir logs(tensorboard使用)
- 软件测试面试题:软件测试的过程的V模型,说出它的缺点?
- MySQL基础篇【第一篇】| 数据库概述及数据准备、常用命令、查看表结构步骤
- 本周四晚19:00知识赋能第六期第5课丨OpenHarmony WiFi子系统
- TRCX:掺杂过程分析
- 如何解决高度塌陷
猜你喜欢
漏洞管理计划的未来趋势
OpenHarmony啃论文俱乐部-啃论文心得
MySQL - an SQL in MySQL is how to be performed?
【备战“金九银十”】2022年软件测试面试题最新汇总
英伟达 GPU 架构简史
redis学习五redis的持久化RDB,fork,copyonwrite,AOF,RDB&AOF混合使用
进程间通信方式(2)有名管道
Js prototype and prototype chain and prototype inheritance
Detailed explanation of common methods of filtering matrix (array) elements in Matlab
Inter-process communication method (2) Named pipe
随机推荐
花甲的思考
学军中学推理社2017届招新试题
软件测试面试题:什么是α测试,β测试?
3342: String manipulation problem solving
TRCX: doping process analysis
gRPC闭包调度器
如何实现FPGA的可重复性设计
0 in the figure, etc. LeetCode565. Array nesting
四大组件---ContentResolver
How to solve the problem of Tomcat booting and crashing
YTU 2418: C语言习题 矩阵元素变换
ARM开发(四)新手小白如何阅读芯片手册,裸机驱动开发步骤以及纯汇编实现点灯,汇编结合c点灯,c实现点灯
一言(ヒトコト)Hitokoto API
自动生成数据库设计文档利器
Engineering Design of Single-sided PCB Routing Impedance
OptiFDTD应用:用于光纤入波导耦合的硅纳米锥仿真
SyntaxError: invalid syntax
14.cuBLAS开发指南中文版--cuBLAS中的Level-1函数nrm2()和rot()
This Thursday evening at 19:00, Lesson 5 of the sixth phase of knowledge empowerment丨OpenHarmony WiFi subsystem
Mysq_Note4