博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
16、SpringBoot------整合MapStruct
阅读量:7143 次
发布时间:2019-06-28

本文共 2563 字,大约阅读时间需要 8 分钟。

开发工具:STS

前言:

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

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

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

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

实例:

1.导入依赖:

1                 
2
3
org.mapstruct
4
mapstruct-jdk8
5
1.0.0.Final
6
7
8
9
org.mapstruct
10
mapstruct-processor
11
1.0.0.Final
12

 

2.添加po

1 package com.xm.timeHotel.pojo; 2  3 import lombok.Data; 4  5 @Data 6 public class User { 7      8     private String username; 9     private String password;10     private String iconUrl;11 12 }

 

3.添加vo

1 package com.xm.timeHotel.controller.dto; 2  3 import lombok.Data; 4  5 @Data 6 public class UserDto { 7      8     private String username; 9     private String iconUrl;10 11 }

 

4.添加mapper接口

1 package com.xm.timeHotel.controller.dto; 2  3 import org.mapstruct.Mapper; 4 import org.mapstruct.factory.Mappers; 5  6 import com.xm.timeHotel.pojo.User; 7  8 @Mapper(componentModel="spring") 9 public interface UserDtoMapper {10     11     12     public User dtoToUser(UserDto userDto) ;13     public UserDto userToDto(User user);14 15 }

5.添加controller

1 package com.xm.timeHotel.controller; 2  3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.web.bind.annotation.GetMapping; 5 import org.springframework.web.bind.annotation.PathVariable; 6 import org.springframework.web.bind.annotation.PostMapping; 7 import org.springframework.web.bind.annotation.RequestBody; 8 import org.springframework.web.bind.annotation.RestController; 9 10 import com.xm.timeHotel.controller.dto.UserDtoMapper;11 import com.xm.timeHotel.controller.dto.UserDto;12 import com.xm.timeHotel.pojo.User;13 14 @RestController("/user")15 public class UserController {16     17     @Autowired18     private UserDtoMapper userDtoMapper;19     20     21     @PostMapping("/userDto")22     public void getUser(UserDto userDto) {23         User user = userDtoMapper.dtoToUser(userDto);24         System.out.println(user);25     }26     27     @PostMapping("/user")28     public void getUserDto(User user) {29         UserDto userDto = userDtoMapper.userToDto(user);30         System.out.println(user);31     }32 33 }

 

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

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

最后发现:

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

转载于:https://www.cnblogs.com/TimerHotel/p/springboot16.html

你可能感兴趣的文章
部署FTP服务器
查看>>
使用ASIHTTPRequest时,需要手动导入的类库
查看>>
python_15
查看>>
high_voltage
查看>>
memcached参数中文解释
查看>>
论程序员的社会地位
查看>>
coreseek 自定义词库(四)数据过滤
查看>>
java版本切换笔记
查看>>
ExtJs学习网站
查看>>
关系型数据库之Mysql编译安装及数据库基础(一)
查看>>
C++ Builder 2010实用技巧
查看>>
linux下samba共享服务器介绍与配置
查看>>
三步配置SSH 免密码登录
查看>>
iptables的规则整理使用
查看>>
touch命令
查看>>
php 和mysql 时间转换时间戳的一点问题
查看>>
mt2523 How to configure MT2523 HDK charging curren
查看>>
Active Directory Domain Service
查看>>
OS启动说明
查看>>
ActiveMQ 学习笔记系列
查看>>