16、SpringBoot------整合MapStruct 开发工具:STS 前言: 实例:

前言:

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

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

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

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

实例:

1.导入依赖:

 1                 <!-- https://mvnrepository.com/artifact/org.mapstruct/mapstruct-jdk8 -->
 2         <dependency>
 3             <groupId>org.mapstruct</groupId>
 4             <artifactId>mapstruct-jdk8</artifactId>
 5             <version>1.0.0.Final</version>
 6         </dependency>
 7         <!-- https://mvnrepository.com/artifact/org.mapstruct/mapstruct-processor -->
 8         <dependency>
 9             <groupId>org.mapstruct</groupId>
10             <artifactId>mapstruct-processor</artifactId>
11             <version>1.0.0.Final</version>
12         </dependency>        

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     @Autowired
18     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

前言:

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

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

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

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

实例:

1.导入依赖:

 1                 <!-- https://mvnrepository.com/artifact/org.mapstruct/mapstruct-jdk8 -->
 2         <dependency>
 3             <groupId>org.mapstruct</groupId>
 4             <artifactId>mapstruct-jdk8</artifactId>
 5             <version>1.0.0.Final</version>
 6         </dependency>
 7         <!-- https://mvnrepository.com/artifact/org.mapstruct/mapstruct-processor -->
 8         <dependency>
 9             <groupId>org.mapstruct</groupId>
10             <artifactId>mapstruct-processor</artifactId>
11             <version>1.0.0.Final</version>
12         </dependency>        

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     @Autowired
18     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项目,就生成实现类了。