完全一致
指的是entity与DTO之间参数名、参数类型、参数个数全部相同的情况
entity代码
package com.hxd.simple.domain.entity;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* @author hxd
*/
public class User implements Serializable {
/**
* 用户id
*/
private Integer id;
/**
* 姓名
*/
private String name;
/**
* 创建时间
*/
private LocalDateTime createTime;
/**
* 序列化id
*/
private static final long serialVersionUID = 1L;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public LocalDateTime getCreateTime() {
return createTime;
}
public void setCreateTime(LocalDateTime createTime) {
this.createTime = createTime;
}
public User() {
}
public User(Integer id, String name, LocalDateTime createTime) {
this.id = id;
this.name = name;
this.createTime = createTime;
}
}
DTO代码
package com.hxd.simple.domain.dto.response;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* @author hxd
*/
public class UserDTO implements Serializable {
/**
* 用户id
*/
private Integer id;
/**
* 用户名
*/
private String name;
/**
* 创建时间
*/
private LocalDateTime createTime;
/**
* 序列化id
*/
private static final long serialVersionUID = 1L;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public LocalDateTime getCreateTime() {
return createTime;
}
public void setCreateTime(LocalDateTime createTime) {
this.createTime = createTime;
}
public static long getSerialVersionUID() {
return serialVersionUID;
}
}
mapstruct代码
package com.hxd.simple.domain.mapstruct;
import com.hxd.simple.domain.dto.response.UserDTO;
import com.hxd.simple.domain.entity.User;
import org.mapstruct.Mapper;
import java.util.List;
@Mapper(componentModel = "spring")
public interface UserMapStruct {
/**
* 实体类转DTO
* @param user 实体类
* @return DTO对象
*/
UserDTO toResponseDTO(User user);
/**
* 将实体类集合转换成DTO集合
* @param source 实体类集合
* @return DTO集合
*/
List toResponseListDTO(List source);
}
使用mapstruct实现entity转DTO
package com.hxd.simple.controller;
import com.hxd.simple.domain.dto.response.UserDTO;
import com.hxd.simple.domain.entity.User;
import com.hxd.simple.domain.mapstruct.UserMapStruct;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/user")
public class UserController {
@Resource
private UserMapStruct userMapStruct;
@GetMapping("/getUser")
public UserDTO getUser() {
User user = new User();
user.setId(1111);
user.setName("张三");
user.setCreateTime(LocalDateTime.now());
return userMapStruct.toResponseDTO(user);
}
@GetMapping("/getUserList")
public List getUserList() {
List userList = new ArrayList<>();
User user1 = new User(1001, "zs", LocalDateTime.now());
User user2 = new User(1002, "ls", LocalDateTime.now());
User user3 = new User(1003, "ww", LocalDateTime.now());
User user4 = new User(1004, "zl", LocalDateTime.now());
userList.add(user1);
userList.add(user2);
userList.add(user3);
userList.add(user4);
return userMapStruct.toResponseListDTO(userList);
}
}
使用mapstruct实现entity转DTO
package com.hxd.simple.controller;
import com.hxd.simple.domain.dto.response.UserDTO;
import com.hxd.simple.domain.entity.User;
import com.hxd.simple.domain.mapstruct.UserMapStruct;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/user")
public class UserController {
@Resource
private UserMapStruct userMapStruct;
@GetMapping("/getUser")
public UserDTO getUser() {
User user = new User();
user.setId(1111);
user.setName("张三");
user.setCreateTime(LocalDateTime.now());
return userMapStruct.toResponseDTO(user);
}
@GetMapping("/getUserList")
public List getUserList() {
List userList = new ArrayList<>();
User user1 = new User(1001, "zs", LocalDateTime.now());
User user2 = new User(1002, "ls", LocalDateTime.now());
User user3 = new User(1003, "ww", LocalDateTime.now());
User user4 = new User(1004, "zl", LocalDateTime.now());
userList.add(user1);
userList.add(user2);
userList.add(user3);
userList.add(user4);
return userMapStruct.toResponseListDTO(userList);
}
}
总结:
1.entity与DTO完全一致时,mapstruct接口比较简单
2.mapstruct的使用与常规的Spring的bean一样,关键是@Mapper注解的componentModel = "spring",此注解会在生成的mapstruct接口的实现类上加@Component注解
3.mapstruct生成的class文件如下:
package com.hxd.simple.domain.mapstruct;
import com.hxd.simple.domain.dto.response.UserDTO;
import com.hxd.simple.domain.entity.User;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.springframework.stereotype.Component;
@Component
public class UserMapStructImpl implements UserMapStruct {
public UserMapStructImpl() {
}
public UserDTO toResponseDTO(User user) {
if (user == null) {
return null;
} else {
UserDTO userDTO = new UserDTO();
userDTO.setId(user.getId());
userDTO.setName(user.getName());
userDTO.setCreateTime(user.getCreateTime());
return userDTO;
}
}
public List toResponseListDTO(List source) {
if (source == null) {
return null;
} else {
List list = new ArrayList(source.size());
Iterator var3 = source.iterator();
while(var3.hasNext()) {
User user = (User)var3.next();
list.add(this.toResponseDTO(user));
}
return list;
}
}
}
4.要想实现List转List,必须先实现单个entity转DTO