在本文中,您將找到有關(guān)如何高效使用 MapStruct、Lombok 和 Spring Boot 的代碼示例和說明。
介紹
????????當(dāng)您實(shí)現(xiàn)任何規(guī)模的服務(wù)時(shí),您通常需要將數(shù)據(jù)從一種結(jié)構(gòu)移動(dòng)到另一種結(jié)構(gòu)。通常,這是在不同邏輯層使用的相同數(shù)據(jù) - 在業(yè)務(wù)邏輯、數(shù)據(jù)庫(kù)級(jí)別或用于傳輸?shù)角岸藨?yīng)用程序的控制器級(jí)別。
????????要傳輸這些數(shù)據(jù),您必須重復(fù)大量樣板文件。真累。我想提請(qǐng)您注意一個(gè)可以幫助您節(jié)省精力的圖書館。認(rèn)識(shí) MapStructure!
使用這個(gè)庫(kù),您只能指定結(jié)構(gòu)映射方案。其實(shí)現(xiàn)將由圖書館自行收集。
在哪里可以找到它
最新版本可以在Maven 中央存儲(chǔ)庫(kù)中找到。
您可以將其添加到您的pom.xml:
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.5.5.Final</version>
</dependency>
您需要在插件中添加注釋處理器:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.5.5.Final</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
如果您使用 Gradle,可以將其添加到您的build.gradle
:
implementation "org.mapstruct:mapstruct:${mapstructVersion}"
annotationProcessor "org.mapstruct:mapstruct-processor:${mapstructVersion}"
怎么運(yùn)行的
我們以一些數(shù)據(jù)類為例:
public class CatEntity {
? ? private Long id;
private String name;
private String color;
// getters and setters
}
public class CatDto {
private Long id;
private String name;
private String color;
// getters and setters
}
這就是我們需要為其實(shí)現(xiàn)編寫的所有代碼:
@Mapper
public interface CatMapper {
? ? CatEntity toEntity(CatDto dto);
CatDto toDto(CatEntity entity);
}
該接口的實(shí)現(xiàn)將由 MapStruct 本身創(chuàng)建:
@Generated
public class?CatMapperImpl implements?CatMapper {
@Override
? ??public?CatEntity toEntity(CatDto dto) {
if?( dto == null?) {
return null;
}
CatEntity catEntity = new?CatEntity();
catEntity.setId( dto.getId() );
catEntity.setName( dto.getName() );
catEntity.setColor( dto.getColor() );
return?catEntity;
}
@Override
? ??public?CatDto toDto(CatEntity entity) {
if?( entity == null?) {
return null;
}
CatDto catDto = new?CatDto();
catDto.setId( entity.getId() );
catDto.setName( entity.getName() );
catDto.setColor( entity.getColor() );
return?catDto;
}
}
如何將 MapStruct 與 Java 記錄一起使用
在Java 14中,添加了記錄類。MapStruct 也可以處理它們:
public class CatEntity {
? ?private Long id;
? ?private String name;
? ?private String color;
? ?// getters and setters
}
public record CatRecord(
? ?Long id,
? ?String name,
? ?String color
) {
}
如果我們創(chuàng)建一個(gè)映射接口:
@Mapper
public interface?CatRecordMapper {
CatEntity toEntity(CatRecord record);
CatRecord toRecord(CatEntity entity);
}
然后 Mapstruct 將生成如下實(shí)現(xiàn):
@Generated
public class?CatRecordMapperImpl implements?CatRecordMapper {
@Override
? ??public?CatEntity toEntity(CatRecord record) {
if?( record == null?) {
return null;
}
CatEntity catEntity = new?CatEntity();
catEntity.setId( record.id() );
catEntity.setName( record.name() );
catEntity.setColor( record.color() );
return?catEntity;
}
@Override
? ??public?CatRecord toRecord(CatEntity entity) {
if?( entity == null?) {
return null;
}
Long id = null;
String name = null;
String color = null;
id = entity.getId();
name = entity.getName();
color = entity.getColor();
CatRecord catRecord = new?CatRecord( id, name, color );
return?catRecord;
}
}
如何將 MapStruct 與 Project Lombok 結(jié)合使用
????????在Java世界中,有一個(gè)廣為人知的大型庫(kù)——Project Lombok。它還可以減少開發(fā)人員必須編寫的樣板代碼。有關(guān)該庫(kù)的更多詳細(xì)信息,您可以在官方網(wǎng)站上找到。
要將此庫(kù)添加到您的項(xiàng)目中,您需要將其添加到 pom.xml 中:
<dependency>
????????<groupId>org.projectlombok</groupId>
????????<artifactId>lombok</artifactId>
????????<version>1.18.28</version>
????????<scope>provided</scope>
</dependency>
?而且,您需要將其添加到注釋處理器中:
<annotationProcessorPaths>
????<path>
????????<groupId>org.projectlombok</groupId>
????????<artifactId>lombok</artifactId>
????????<version>1.18.28</version>
????</path>
</annotationProcessorPaths>
對(duì)于 Gradle 來說,稍微簡(jiǎn)單一些。只需將其添加到 ?build.gradle
implementation 'org.projectlombok:lombok:1.18.28'
annotationProcessor "org.projectlombok:lombok:1.18.28"
并且邁出了重要的一步!要將 Project Lombok 與 MapStruct 集成,您需要添加綁定庫(kù):
annotationProcessor 'org.projectlombok:lombok-mapstruct-binding:0.2.0'
對(duì)于我們的示例,使用@Data
注釋就足夠了,它添加了 getter 和 setter。
@Data
public class?CatDto {
private?Long id;
private?String name;
private?String color;
}
@Data
public class?CatEntity {
private?Long id;
private?String name;
private?String color;
}
對(duì)于同一個(gè)mapper接口,它會(huì)生成以下實(shí)現(xiàn):
public interface CatMapper {
CatEntity toEntity(CatDto dto);
CatDto toDto(CatEntity entity);
}
@Generated
public class?CatMapperImpl implements?CatMapper {
@Override
? ??public?CatEntity toEntity(CatDto dto) {
if?( dto == null?) {
return null;
}
CatEntity catEntity = new?CatEntity();
catEntity.setId( dto.getId() );
catEntity.setName( dto.getName() );
catEntity.setColor( dto.getColor() );
return?catEntity;
}
@Override
? ??public?CatDto toDto(CatEntity entity) {
if?( entity == null?) {
return null;
}
CatDto catDto = new?CatDto();
catDto.setId( entity.getId() );
catDto.setName( entity.getName() );
catDto.setColor( entity.getColor() );
return?catDto;
}
}
如何將 Spring Boot 邏輯添加到 Mapper 中
有時(shí)需要從Spring的bean中檢索一些字段。假設(shè)我們不在數(shù)據(jù)庫(kù)中存儲(chǔ)權(quán)重信息,這在 Entity 中是無法訪問的。相反,我們有一些 Spring 的服務(wù)來提供此信息。
@Data
public class?CatDto {
private?Long id;
private?String name;
private?String color;
private?Integer weight;
}
@Data
public class?CatEntity {
private?Long id;
private?String name;
private?String color;
}
And there is service that provides weight information:
@Service
public class?CatWeightProvider {
public?Integer getWeight(String name) {
// some logic for retrieving weight info
? ? ? ??return?5;
}
}
要使用此 bean 檢索映射器接口內(nèi)的權(quán)重信息,應(yīng)將其替換為具有描述所有附加邏輯的方法的抽象類。
@Mapper(componentModel = "spring")
public abstract class?CatMapper {
@Autowired
private CatWeightProvider provider;
@Mapping(target = "weight", source = "entity.name", qualifiedByName = "retrieveWeight")
public abstract CatDto toDto(CatEntity entity);
@Named("retrieveWeight")
protected Integer retrieveWeight(String name) {
return provider.getWeight(name);
}
}
在這種情況下,MapStruct將生成這個(gè)抽象類的實(shí)現(xiàn):
@Generated
@Component
public class?CatMapperImpl extends?CatMapper {
@Override
? ??public?CatDto toDto(CatEntity entity) {
if?( entity == null?) {
return null;
}
CatDto catDto = new?CatDto();
catDto.setWeight( retrieveWeight( entity.getName() ) );
catDto.setId( entity.getId() );
catDto.setName( entity.getName() );
catDto.setColor( entity.getColor() );
return?catDto;
}
}
如何忽略字段并映射具有不同名稱的字段
例如,數(shù)據(jù)庫(kù)實(shí)體中的字段和 dto 中的字段具有不同的名稱。?
例如,我們需要忽略 dto 層中的字段權(quán)重。
@Data
public class?CatDto {
private?String name;
private?String color;
private?Integer weight;
}
@Data
public class?CatEntity {
private?Long idInDatabase;
private?String nameInDatabase;
private?String colorInDatabase;
}
可以通過以下參數(shù)來完成:
@Mapper
public interface?CatMapper {
@Mapping(target = "weight", ignore = true)
@Mapping(target = "name", source = "entity.nameInDatabase")
@Mapping(target = "color", source = "entity.colorInDatabase")
CatDto toDto(CatEntity entity);
}
因此,MapStruct 的實(shí)現(xiàn)將是:文章來源:http://www.zghlxwxcb.cn/news/detail-632794.html
@Generated
public class?CatMapperImpl implements?CatMapper {
@Override
? ??public?CatDto toDto(CatEntity entity) {
if?( entity == null?) {
return null;
}
CatDto catDto = new?CatDto();
catDto.setName( entity.getNameInDatabase() );
catDto.setColor( entity.getColorInDatabase() );
return?catDto;
}
}
結(jié)論
????????我們已經(jīng)介紹了開發(fā)多層應(yīng)用程序時(shí)出現(xiàn)的最流行的場(chǎng)景。因此,Project Lombok 和 MapStruct 庫(kù)的結(jié)合可以顯著節(jié)省開發(fā)人員在樣板文件上的時(shí)間和精力。
感謝您的關(guān)注!文章來源地址http://www.zghlxwxcb.cn/news/detail-632794.html
到了這里,關(guān)于在 Spring Boot 應(yīng)用程序中將 MapStruct 與 Lombok 結(jié)合使用的方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!