当前位置:网站首页>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);
}
}
边栏推荐
- Lianshengde W801 series 6-Analyze the Bluetooth communication source code of W801 from the perspective of WeChat applet (indicate method)
- 【oops-framework】模板项目【oops-game-kit】使用简介
- MySQL中的DDL常规操作总结
- OpenHarmony啃论文俱乐部-啃论文心得
- ora-00001违反唯一约束
- 软件测试面试题:软件测试的过程的V模型,说出它的缺点?
- [Detailed explanation of C data storage] (1) - in-depth analysis of the storage of shaping data in memory
- C# string与stream的相互转换
- Shengxin experiment record (part2)--tf.reduce_sum() usage introduction
- Fatal error in launcher: Unable to create process using xxx --logdir logs(tensorboard使用)
猜你喜欢
随机推荐
ASEMI整流桥GBJ5010参数,GBJ5010电压,GBJ5010电流
sql 使用到where和groupby时到底怎么建立索引?
想进阿里?先来搞懂一下分布式事务
数据库数据采集利器FlinkCDC
mysql 死锁 Deadlock found when trying to get lock; try restarting transaction
超声图像三维拼接-可视化选择,总体思路
Deep Learning [Chapter 2]
YTU 2418: C语言习题 矩阵元素变换
花甲的思考
MySQL - an SQL in MySQL is how to be performed?
qtcreator调试webkit
自动生成数据库设计文档利器
Mysq_Note4
SIT221 Data Structures and Algorithms课程辅导
项目构建工具-Gradle入门
async和await的理解和用法
FPGA学习专栏-串口通信(xinlinx)
Mysql_Note5
shell [stdin/stdout/stderr][重定向]
软件测试面试题:测试用例与测试脚本?