开发工具:STS

前言:

  前端提交往后端的数据,一部分是不需要存入数据库当中的;

  后端从数据库中取出的数据,一部分是不可以交给用户的;

  那么,po面向的是DB,vo面向的是客户端,

  mapstruct就提供了vo与po自动转换的一种方式。

实例:

1.导入依赖:

                 <!-- https://mvnrepository.com/artifact/org.mapstruct/mapstruct-jdk8 -->
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId>
<version>1.0.0.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mapstruct/mapstruct-processor -->
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.0.0.Final</version>
</dependency>

2.添加po

 package com.xm.timeHotel.pojo;

 import lombok.Data;

 @Data
public class User { private String username;
private String password;
private String iconUrl; }

3.添加vo

 package com.xm.timeHotel.controller.dto;

 import lombok.Data;

 @Data
public class UserDto { private String username;
private String iconUrl; }

4.添加mapper接口

 package com.xm.timeHotel.controller.dto;

 import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers; import com.xm.timeHotel.pojo.User; @Mapper(componentModel="spring")
public interface UserDtoMapper { public User dtoToUser(UserDto userDto) ;
public UserDto userToDto(User user); }

5.添加controller

 package com.xm.timeHotel.controller;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController; import com.xm.timeHotel.controller.dto.UserDtoMapper;
import com.xm.timeHotel.controller.dto.UserDto;
import com.xm.timeHotel.pojo.User; @RestController("/user")
public class UserController { @Autowired
private UserDtoMapper userDtoMapper; @PostMapping("/userDto")
public void getUser(UserDto userDto) {
User user = userDtoMapper.dtoToUser(userDto);
System.out.println(user);
} @PostMapping("/user")
public void getUserDto(User user) {
UserDto userDto = userDtoMapper.userToDto(user);
System.out.println(user);
} }

6.eclipse生成不了mapper接口实现类的解决方案:

我直接运行项目的时候,发现一直未找到UserDtoMapper实例,从网上查找了好多种方案,都不能生效。

最后发现:

运行mvn clean项目,再mvn install项目,就生成实现类了。

最新文章

  1. Ajax工作流程
  2. 转 Netflix OSS、Spring Cloud还是Kubernetes? 都要吧!
  3. 简单C程序生成的汇编代码分析
  4. CUBRID学习笔记 48查询优化
  5. ProcExp和TaskMgr的列对比
  6. Fluent Nhibernate and Stored Procedures
  7. uva514(trail)(模拟栈)
  8. HOG参数简介及Hog特征维数的计算(转)
  9. java获取计算机硬件参数
  10. Oracle视图基础
  11. http://codeforces.com/contest/610/problem/D
  12. javascript预览图片——IT轮子系列(九)
  13. linux----------linux下安装rar和unrar命令
  14. nginx限制单个IP的最大连接数量限制下载速度
  15. /bin/sh^M:bad interpreter:
  16. Python 入门网络爬虫之精华版
  17. datacolumn 表达式 除数为0
  18. Results the mutual for the first time(alpha阶段总结)
  19. SpringMVC整合ActiveMQ
  20. Xilinx中解决高扇出的方法

热门文章

  1. js监听dom元素内容变化
  2. 3d Max 2010安装失败怎样卸载3dsmax?错误提示某些产品无法安装
  3. Integer代码分析
  4. JEECMS站群管理系统-- 自定义标签及使用自己创建的表的实现过程
  5. jquery截取、判断字符串的长度,中英文都可
  6. 非关系型数据库(NOSQL)-Redis
  7. Spring文件上传Demo
  8. HTML标签_2
  9. em和rem的区别
  10. 【起航计划 029】2015 起航计划 Android APIDemo的魔鬼步伐 28 App-&gt;Preferences-&gt;Default Values 偏好默认值