MapStruct
1.pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>lab-55</artifactId>
<groupId>cn.iocoder.springboot.labs</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>lab-55-mapstruct-demo-lombok</artifactId>
<properties>
<java.version>1.8</java.version>
<mapstruct.version>1.3.1.Final</mapstruct.version>
<lombok.version>1.18.12</lombok.version>
</properties>
<dependencies>
<!-- 引入 mapstruct 依赖 -->
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${mapstruct.version}</version>
</dependency>
<!-- 引入 lombok 依赖 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<annotationProcessorPaths>
<!-- 引入 mapstruct-processor -->
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
</path>
<!-- 引入 lombok-processor -->
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.新建类 User、UserDTO
@Data
public class User {
private Integer userId;
private String userAge;
private String userName;
// runtime
private int age;
@Data
public class UserDTO {
private Integer userId;
private String userAge;
private String userName;
// runtime
private int age;
}
3.核心转化接口 UserConvert
@Mapper
public interface UserConvert {
UserConvert INSTANCE = Mappers.getMapper(UserConvert.class);
@Mappings({
@Mapping(source = "userId", target = "userId"),
@Mapping(source = "userAge", target = "userAge", ignore = true)
})
UserDTO convert(User user);
}
4.进阶用法
指定你对应字段映射的方法,方法可以自定义
@Mapper(uses = JacksonUtil.class)
public interface UserConvert {
UserConvert INSTANCE = Mappers.getMapper(UserConvert.class);
@Mappings({
@Mapping(source = "userId", target = "userId"),
@Mapping(source = "userAge", target = "userAge", ignore = true),
@Mapping(source = "userName", target = "userName", qualifiedByName = "toJsonString"),
})
UserDTO convert(User user);
}
工具类 JacksonUtil
public class JacksonUtil {
private static Logger log = LoggerFactory.getLogger("JacksonUtil");
private static ObjectMapper mapper = new ObjectMapper();
/**
* 对象转json
*
* @param object
* @return
*/
public static String toJsonString(Object object) {
try {
return mapper.writeValueAsString(object);
} catch (JsonProcessingException e) {
log.error("...Err...Jackson转换字符串(String)过程失败:::", e);
e.printStackTrace();
}
return null;
}
/**
* json转对象
*
* @param json
* @param clazz
* @param <T>
* @return
*/
public static <T> T jsonToBean(String json, Class<T> clazz) throws IOException {
try {
return mapper.readValue(json, clazz);
} catch (JsonProcessingException e) {
log.error("...Err...Jackson转换对象(Object)过程失败:::", e);
e.printStackTrace();
}
return null;
}
/**
* 字符串转换为List
*
* @param listStr
* @param typeReference new TypeReference<List<Object>>() {}
* @param <T>
* @return
*/
public static <T> T jsonToObjByTypeRf(String listStr, TypeReference<T> typeReference) throws IOException {
try {
return mapper.readValue(listStr, typeReference);
} catch (JsonProcessingException e) {
log.error("...Err...Jackson转换Object过程失败:::", e);
e.printStackTrace();
}
return null;
}
}