当前位置:网站首页>Entity to Vo conversion
Entity to Vo conversion
2022-08-11 02:41:00 【Don't bully the young, middle-aged, old and poor】
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);
}
}
边栏推荐
- MySQL八股文背诵版(续)
- redis学习五redis的持久化RDB,fork,copyonwrite,AOF,RDB&AOF混合使用
- 3342:字符串操作 题解
- 备战“金九银十”,软件测试功能 / 数据库 /linux/ 接口 / 自动化 / 测试开发面试真题解析
- Multi-threaded ThreadPoolExecutor
- 英伟达 GPU 架构简史
- MySQL权限控制、分区表、快速复制表
- 0 in the figure, etc. LeetCode565. Array nesting
- 《人生若如初见》命运多舛,人物饱满,朱亚文角色反差太惊喜
- MySQL - an SQL in MySQL is how to be performed?
猜你喜欢

数据的存储(下)——浮点型在内存中的存储

Deep Learning - Second Time

How to solve the problem of Tomcat booting and crashing

【C 数据存储详解】(1)——深度剖析整形数据在内存中的存储

ROS源代码阅读(1)

DOM树的遍历-----修改样式,选择元素,创建和删除节点

Future Trends in Vulnerability Management Programs

gRPC闭包调度器

Detailed explanation of new features of ES advanced function syntax
OpenHarmony啃论文俱乐部-啃论文心得
随机推荐
阿里亿级并发册 + 机器学习算法 + 面试册 + 优化册 + 代码册 笔记!!!
Research on the Application of Privacy Computing Fusion
google搜索技巧——程序员推荐
2022制冷与空调设备运行操作考试试题模拟考试平台操作
comp3331-9331-21t1-midterm复习
数据存储全方案----详解持久化技术
Vulnhub靶机:GEMINI INC_ 2
TRCX:掺杂过程分析
四大组件---ContentResolver
数据的存储(下)——浮点型在内存中的存储
117. 本地开发好的 SAP UI5 应用部署到 ABAP 服务器时,中文字符变成乱码的原因分析和解决方案
ES进阶 数组功能语法新特性详解
如何开展性能测试,你知道吗?
nvidia-smi详解
WeChat public account background management
惨遭面试官吊打高并发系统设计,回来学习 2400 小时后成功复仇
Section 4-6 of the first week of the second lesson: Appreciation of medical prognosis cases + homework analysis
Inter-process communication method (2) Named pipe
CSAPP Data Lab
2022茶艺师(中级)考试试题及模拟考试