0
  • 聊天消息
  • 系統(tǒng)消息
  • 評(píng)論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫(xiě)文章/發(fā)帖/加入社區(qū)
會(huì)員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

MapStruct是用來(lái)做什么的

Android編程精選 ? 來(lái)源:CSDN技術(shù)社區(qū) ? 作者:飛飛不 會(huì)飛 ? 2022-06-15 17:02 ? 次閱讀
首先來(lái)了解一下DTO,DTO簡(jiǎn)單的理解就是做數(shù)據(jù)傳輸對(duì)象的,類(lèi)似于VO,但是VO用于傳輸?shù)角岸恕?/span>

1.MapStruct是用來(lái)做什么的?

現(xiàn)在有這么個(gè)場(chǎng)景,從數(shù)據(jù)庫(kù)查詢(xún)出來(lái)了一個(gè)user對(duì)象(包含id,用戶(hù)名,密碼,手機(jī)號(hào),郵箱,角色這些字段)和一個(gè)對(duì)應(yīng)的角色對(duì)象role(包含id,角色名,角色描述這些字段),現(xiàn)在在controller需要用到user對(duì)象的id,用戶(hù)名,和角色對(duì)象的角色名三個(gè)屬性。

一種方式是直接把兩個(gè)對(duì)象傳遞到controller層,但是這樣會(huì)多出很多沒(méi)用的屬性。更通用的方式是需要用到的屬性封裝成一個(gè)類(lèi)(DTO),通過(guò)傳輸這個(gè)類(lèi)的實(shí)例來(lái)完成數(shù)據(jù)傳輸。

User.java

@AllArgsConstructor
@Data
publicclassUser{
privateLongid;
privateStringusername;
privateStringpassword;
privateStringphoneNum;
privateStringemail;
privateRolerole;
}

Role.java

@AllArgsConstructor
@Data
publicclassRole{
privateLongid;
privateStringroleName;
privateStringdescription;
}

UserRoleDto.java,這個(gè)類(lèi)就是封裝的類(lèi)

@Data
publicclassUserRoleDto{
/**
*用戶(hù)id
*/
privateLonguserId;
/**
*用戶(hù)名
*/
privateStringname;
/**
*角色名
*/
privateStringroleName;
}

測(cè)試類(lèi),模擬將user對(duì)象轉(zhuǎn)換成UserRoleDto對(duì)象

publicclassMainTest{
Useruser=null;

/**
*模擬從數(shù)據(jù)庫(kù)中查出user對(duì)象
*/
@Before
publicvoidbefore(){
Rolerole=newRole(2L,"administrator","超級(jí)管理員");
user=newUser(1L,"zhangsan","12345","17677778888","123@qq.com",role);
}

/**
*模擬把user對(duì)象轉(zhuǎn)換成UserRoleDto對(duì)象
*/
@Test
publicvoidtest1(){
UserRoleDtouserRoleDto=newUserRoleDto();
userRoleDto.setUserId(user.getId());
userRoleDto.setName(user.getUsername());
userRoleDto.setRoleName(user.getRole().getRoleName());
System.out.println(userRoleDto);
}
}

從上面代碼可以看出,通過(guò)getter、setter的方式把一個(gè)對(duì)象屬性值復(fù)制到另一個(gè)對(duì)象中去還是很麻煩的,尤其是當(dāng)屬性過(guò)多的時(shí)候。而MapStruct就是用于解決這種問(wèn)題的。

2.使用MapStruct解決上述問(wèn)題

這里我們沿用User.java、Role.java、UserRoleDto.java。

新建一個(gè)UserRoleMapper.java,這個(gè)來(lái)用來(lái)定義User.java、Role.java和UserRoleDto.java之間屬性對(duì)應(yīng)規(guī)則:

UserRoleMapper.java

importorg.mapstruct.Mapper;
importorg.mapstruct.Mapping;
importorg.mapstruct.Mappings;
importorg.mapstruct.factory.Mappers;

/**
*@Mapper定義這是一個(gè)MapStruct對(duì)象屬性轉(zhuǎn)換接口,在這個(gè)類(lèi)里面規(guī)定轉(zhuǎn)換規(guī)則
*在項(xiàng)目構(gòu)建時(shí),會(huì)自動(dòng)生成改接口的實(shí)現(xiàn)類(lèi),這個(gè)實(shí)現(xiàn)類(lèi)將實(shí)現(xiàn)對(duì)象屬性值復(fù)制
*/
@Mapper
publicinterfaceUserRoleMapper{

/**
*獲取該類(lèi)自動(dòng)生成的實(shí)現(xiàn)類(lèi)的實(shí)例
*接口中的屬性都是publicstaticfinal的方法都是publicabstract的
*/
UserRoleMapperINSTANCES=Mappers.getMapper(UserRoleMapper.class);

/**
*這個(gè)方法就是用于實(shí)現(xiàn)對(duì)象屬性復(fù)制的方法
*
*@Mapping用來(lái)定義屬性復(fù)制規(guī)則source指定源對(duì)象屬性target指定目標(biāo)對(duì)象屬性
*
*@paramuser這個(gè)參數(shù)就是源對(duì)象,也就是需要被復(fù)制的對(duì)象
*@return返回的是目標(biāo)對(duì)象,就是最終的結(jié)果對(duì)象
*/
@Mappings({
@Mapping(source="id",target="userId"),
@Mapping(source="username",target="name"),
@Mapping(source="role.roleName",target="roleName")
})
UserRoleDtotoUserRoleDto(Useruser);

}

在測(cè)試類(lèi)中測(cè)試:

通過(guò)上面的例子可以看出,使用MapStruct方便許多。

3.添加默認(rèn)方法

添加默認(rèn)方法是為了這個(gè)類(lèi)(接口)不只是為了做數(shù)據(jù)轉(zhuǎn)換用的,也可以做一些其他的事。

importorg.mapstruct.Mapper;
importorg.mapstruct.Mapping;
importorg.mapstruct.Mappings;
importorg.mapstruct.factory.Mappers;

/**
*@Mapper定義這是一個(gè)MapStruct對(duì)象屬性轉(zhuǎn)換接口,在這個(gè)類(lèi)里面規(guī)定轉(zhuǎn)換規(guī)則
*在項(xiàng)目構(gòu)建時(shí),會(huì)自動(dòng)生成改接口的實(shí)現(xiàn)類(lèi),這個(gè)實(shí)現(xiàn)類(lèi)將實(shí)現(xiàn)對(duì)象屬性值復(fù)制
*/
@Mapper
publicinterfaceUserRoleMapper{

/**
*獲取該類(lèi)自動(dòng)生成的實(shí)現(xiàn)類(lèi)的實(shí)例
*接口中的屬性都是publicstaticfinal的方法都是publicabstract的
*/
UserRoleMapperINSTANCES=Mappers.getMapper(UserRoleMapper.class);

/**
*這個(gè)方法就是用于實(shí)現(xiàn)對(duì)象屬性復(fù)制的方法
*
*@Mapping用來(lái)定義屬性復(fù)制規(guī)則source指定源對(duì)象屬性target指定目標(biāo)對(duì)象屬性
*
*@paramuser這個(gè)參數(shù)就是源對(duì)象,也就是需要被復(fù)制的對(duì)象
*@return返回的是目標(biāo)對(duì)象,就是最終的結(jié)果對(duì)象
*/
@Mappings({
@Mapping(source="id",target="userId"),
@Mapping(source="username",target="name"),
@Mapping(source="role.roleName",target="roleName")
})
UserRoleDtotoUserRoleDto(Useruser);

/**
*提供默認(rèn)方法,方法自己定義,這個(gè)方法是我隨便寫(xiě)的,不是要按照這個(gè)格式來(lái)的
*@return
*/
defaultUserRoleDtodefaultConvert(){
UserRoleDtouserRoleDto=newUserRoleDto();
userRoleDto.setUserId(0L);
userRoleDto.setName("None");
userRoleDto.setRoleName("None");
returnuserRoleDto;
}

}

測(cè)試代碼:

@Test
publicvoidtest3(){
UserRoleMapperuserRoleMapperInstances=UserRoleMapper.INSTANCES;
UserRoleDtouserRoleDto=userRoleMapperInstances.defaultConvert();
System.out.println(userRoleDto);
}

4. 可以使用abstract class來(lái)代替接口

mapper可以用接口來(lái)實(shí)現(xiàn),也可以完全由抽象來(lái)完全代替

importorg.mapstruct.Mapper;
importorg.mapstruct.Mapping;
importorg.mapstruct.Mappings;
importorg.mapstruct.factory.Mappers;

/**
*@Mapper定義這是一個(gè)MapStruct對(duì)象屬性轉(zhuǎn)換接口,在這個(gè)類(lèi)里面規(guī)定轉(zhuǎn)換規(guī)則
*在項(xiàng)目構(gòu)建時(shí),會(huì)自動(dòng)生成改接口的實(shí)現(xiàn)類(lèi),這個(gè)實(shí)現(xiàn)類(lèi)將實(shí)現(xiàn)對(duì)象屬性值復(fù)制
*/
@Mapper
publicabstractclassUserRoleMapper{

/**
*獲取該類(lèi)自動(dòng)生成的實(shí)現(xiàn)類(lèi)的實(shí)例
*接口中的屬性都是publicstaticfinal的方法都是publicabstract的
*/
publicstaticfinalUserRoleMapperINSTANCES=Mappers.getMapper(UserRoleMapper.class);

/**
*這個(gè)方法就是用于實(shí)現(xiàn)對(duì)象屬性復(fù)制的方法
*
*@Mapping用來(lái)定義屬性復(fù)制規(guī)則source指定源對(duì)象屬性target指定目標(biāo)對(duì)象屬性
*
*@paramuser這個(gè)參數(shù)就是源對(duì)象,也就是需要被復(fù)制的對(duì)象
*@return返回的是目標(biāo)對(duì)象,就是最終的結(jié)果對(duì)象
*/
@Mappings({
@Mapping(source="id",target="userId"),
@Mapping(source="username",target="name"),
@Mapping(source="role.roleName",target="roleName")
})
publicabstractUserRoleDtotoUserRoleDto(Useruser);

/**
*提供默認(rèn)方法,方法自己定義,這個(gè)方法是我隨便寫(xiě)的,不是要按照這個(gè)格式來(lái)的
*@return
*/
UserRoleDtodefaultConvert(){
UserRoleDtouserRoleDto=newUserRoleDto();
userRoleDto.setUserId(0L);
userRoleDto.setName("None");
userRoleDto.setRoleName("None");
returnuserRoleDto;
}

}

5.可以使用多個(gè)參數(shù)

可以綁定多個(gè)對(duì)象的屬性值到目標(biāo)對(duì)象中:

packagecom.mapstruct.demo;

importorg.mapstruct.Mapper;
importorg.mapstruct.Mapping;
importorg.mapstruct.Mappings;
importorg.mapstruct.factory.Mappers;

/**
*@Mapper定義這是一個(gè)MapStruct對(duì)象屬性轉(zhuǎn)換接口,在這個(gè)類(lèi)里面規(guī)定轉(zhuǎn)換規(guī)則
*在項(xiàng)目構(gòu)建時(shí),會(huì)自動(dòng)生成改接口的實(shí)現(xiàn)類(lèi),這個(gè)實(shí)現(xiàn)類(lèi)將實(shí)現(xiàn)對(duì)象屬性值復(fù)制
*/
@Mapper
publicinterfaceUserRoleMapper{

/**
*獲取該類(lèi)自動(dòng)生成的實(shí)現(xiàn)類(lèi)的實(shí)例
*接口中的屬性都是publicstaticfinal的方法都是publicabstract的
*/
UserRoleMapperINSTANCES=Mappers.getMapper(UserRoleMapper.class);

/**
*這個(gè)方法就是用于實(shí)現(xiàn)對(duì)象屬性復(fù)制的方法
*
*@Mapping用來(lái)定義屬性復(fù)制規(guī)則source指定源對(duì)象屬性target指定目標(biāo)對(duì)象屬性
*
*@paramuser這個(gè)參數(shù)就是源對(duì)象,也就是需要被復(fù)制的對(duì)象
*@return返回的是目標(biāo)對(duì)象,就是最終的結(jié)果對(duì)象
*/
@Mappings({
@Mapping(source="id",target="userId"),
@Mapping(source="username",target="name"),
@Mapping(source="role.roleName",target="roleName")
})
UserRoleDtotoUserRoleDto(Useruser);

/**
*多個(gè)參數(shù)中的值綁定
*@paramuser源1
*@paramrole源2
*@return從源1、2中提取出的結(jié)果
*/
@Mappings({
@Mapping(source="user.id",target="userId"),//把user中的id綁定到目標(biāo)對(duì)象的userId屬性中
@Mapping(source="user.username",target="name"),//把user中的username綁定到目標(biāo)對(duì)象的name屬性中
@Mapping(source="role.roleName",target="roleName")//把role對(duì)象的roleName屬性值綁定到目標(biāo)對(duì)象的roleName中
})
UserRoleDtotoUserRoleDto(Useruser,Rolerole);

對(duì)比兩個(gè)方法~

5.直接使用參數(shù)作為屬性值

packagecom.mapstruct.demo;

importorg.mapstruct.Mapper;
importorg.mapstruct.Mapping;
importorg.mapstruct.Mappings;
importorg.mapstruct.factory.Mappers;

/**
*@Mapper定義這是一個(gè)MapStruct對(duì)象屬性轉(zhuǎn)換接口,在這個(gè)類(lèi)里面規(guī)定轉(zhuǎn)換規(guī)則
*在項(xiàng)目構(gòu)建時(shí),會(huì)自動(dòng)生成改接口的實(shí)現(xiàn)類(lèi),這個(gè)實(shí)現(xiàn)類(lèi)將實(shí)現(xiàn)對(duì)象屬性值復(fù)制
*/
@Mapper
publicinterfaceUserRoleMapper{

/**
*獲取該類(lèi)自動(dòng)生成的實(shí)現(xiàn)類(lèi)的實(shí)例
*接口中的屬性都是publicstaticfinal的方法都是publicabstract的
*/
UserRoleMapperINSTANCES=Mappers.getMapper(UserRoleMapper.class);

/**
*直接使用參數(shù)作為值
*@paramuser
*@parammyRoleName
*@return
*/
@Mappings({
@Mapping(source="user.id",target="userId"),//把user中的id綁定到目標(biāo)對(duì)象的userId屬性中
@Mapping(source="user.username",target="name"),//把user中的username綁定到目標(biāo)對(duì)象的name屬性中
@Mapping(source="myRoleName",target="roleName")//把role對(duì)象的roleName屬性值綁定到目標(biāo)對(duì)象的roleName中
})
UserRoleDtouseParameter(Useruser,StringmyRoleName);

}

測(cè)試類(lèi):

publicclassTest1{
Rolerole=null;
Useruser=null;

@Before
publicvoidbefore(){
role=newRole(2L,"administrator","超級(jí)管理員");
user=newUser(1L,"zhangsan","12345","17677778888","123@qq.com",role);
}
@Test
publicvoidtest1(){
UserRoleMapperinstances=UserRoleMapper.INSTANCES;
UserRoleDtouserRoleDto=instances.useParameter(user,"myUserRole");
System.out.println(userRoleDto);
}
}

6.更新對(duì)象屬性

在之前的例子中UserRoleDto useParameter(User user, String myRoleName);都是通過(guò)類(lèi)似上面的方法來(lái)生成一個(gè)對(duì)象。而MapStruct提供了另外一種方式來(lái)更新一個(gè)對(duì)象中的屬性。@MappingTarget

publicinterfaceUserRoleMapper1{

UserRoleMapper1INSTANCES=Mappers.getMapper(UserRoleMapper1.class);

@Mappings({
@Mapping(source="userId",target="id"),
@Mapping(source="name",target="username"),
@Mapping(source="roleName",target="role.roleName")
})
voidupdateDto(UserRoleDtouserRoleDto,@MappingTargetUseruser);


@Mappings({
@Mapping(source="id",target="userId"),
@Mapping(source="username",target="name"),
@Mapping(source="role.roleName",target="roleName")
})
voidupdate(Useruser,@MappingTargetUserRoleDtouserRoleDto);

}

通過(guò)@MappingTarget來(lái)指定目標(biāo)類(lèi)是誰(shuí)(誰(shuí)的屬性需要被更新)。@Mapping還是用來(lái)定義屬性對(duì)應(yīng)規(guī)則。

以此為例說(shuō)明:

@Mappings({
@Mapping(source="id",target="userId"),
@Mapping(source="username",target="name"),
@Mapping(source="role.roleName",target="roleName")
})
voidupdate(Useruser,@MappingTargetUserRoleDtouserRoleDto);

@MappingTarget標(biāo)注的類(lèi)UserRoleDto 為目標(biāo)類(lèi),user類(lèi)為源類(lèi),調(diào)用此方法,會(huì)把源類(lèi)中的屬性更新到目標(biāo)類(lèi)中。更新規(guī)則還是由@Mapping指定。

7.沒(méi)有g(shù)etter/setter也能賦值

對(duì)于沒(méi)有g(shù)etter/setter的屬性也能實(shí)現(xiàn)賦值操作

publicclassCustomer{

privateLongid;
privateStringname;

//gettersandsetteromittedforbrevity
}

publicclassCustomerDto{

publicLongid;
publicStringcustomerName;
}

@Mapper
publicinterfaceCustomerMapper{

CustomerMapperINSTANCE=Mappers.getMapper(CustomerMapper.class);

@Mapping(source="customerName",target="name")
CustomertoCustomer(CustomerDtocustomerDto);

@InheritInverseConfiguration
CustomerDtofromCustomer(Customercustomer);
}

@Mapping(source = “customerName”, target = “name”)不是用來(lái)指定屬性映射的,如果兩個(gè)對(duì)象的屬性名相同是可以省略@Mapping的。

MapStruct生成的實(shí)現(xiàn)類(lèi):

@Generated(
value="org.mapstruct.ap.MappingProcessor",
date="2019-02-14T1521+0800",
comments="version:1.3.0.Final,compiler:javac,environment:Java1.8.0_181(OracleCorporation)"
)
publicclassCustomerMapperImplimplementsCustomerMapper{

@Override
publicCustomertoCustomer(CustomerDtocustomerDto){
if(customerDto==null){
returnnull;
}

Customercustomer=newCustomer();

customer.setName(customerDto.customerName);
customer.setId(customerDto.id);

returncustomer;
}

@Override
publicCustomerDtotoCustomerDto(Customercustomer){
if(customer==null){
returnnull;
}

CustomerDtocustomerDto=newCustomerDto();

customerDto.customerName=customer.getName();
customerDto.id=customer.getId();

returncustomerDto;
}
}

@InheritInverseConfiguration在這里的作用就是實(shí)現(xiàn)customerDto.customerName = customer.getName();功能的。如果沒(méi)有這個(gè)注解,toCustomerDto這個(gè)方法則不會(huì)有customerName 和name兩個(gè)屬性的對(duì)應(yīng)關(guān)系的。

8.使用Spring依賴(lài)注入

@Data
@NoArgsConstructor
@AllArgsConstructor
publicclassCustomer{
privateLongid;
privateStringname;
}

@Data
publicclassCustomerDto{
privateLongid;
privateStringcustomerName;
}

//這里主要是這個(gè)componentModel屬性,它的值就是當(dāng)前要使用的依賴(lài)注入的環(huán)境
@Mapper(componentModel="spring")
publicinterfaceCustomerMapper{

@Mapping(source="name",target="customerName")
CustomerDtotoCustomerDto(Customercustomer);
}

@Mapper(componentModel = “spring”),表示把當(dāng)前Mapper類(lèi)納入spring容器??梢栽谄渌?lèi)中直接注入了:

@SpringBootApplication
@RestController
publicclassDemoMapstructApplication{

//注入Mapper
@Autowired
privateCustomerMappermapper;

publicstaticvoidmain(String[]args){
SpringApplication.run(DemoMapstructApplication.class,args);
}

@GetMapping("/test")
publicStringtest(){
Customercustomer=newCustomer(1L,"zhangsan");
CustomerDtocustomerDto=mapper.toCustomerDto(customer);
returncustomerDto.toString();
}

}

看一下由mapstruct自動(dòng)生成的類(lèi)文件,會(huì)發(fā)現(xiàn)標(biāo)記了@Component注解。

@Generated(
value="org.mapstruct.ap.MappingProcessor",
date="2019-02-14T1517+0800",
comments="version:1.3.0.Final,compiler:javac,environment:Java1.8.0_181(OracleCorporation)"
)
@Component
publicclassCustomerMapperImplimplementsCustomerMapper{

@Override
publicCustomerDtotoCustomerDto(Customercustomer){
if(customer==null){
returnnull;
}

CustomerDtocustomerDto=newCustomerDto();

customerDto.setCustomerName(customer.getName());
customerDto.setId(customer.getId());

returncustomerDto;
}
}

9.自定義類(lèi)型轉(zhuǎn)換

有時(shí)候,在對(duì)象轉(zhuǎn)換的時(shí)候可能會(huì)出現(xiàn)這樣一個(gè)問(wèn)題,就是源對(duì)象中的類(lèi)型是Boolean類(lèi)型,而目標(biāo)對(duì)象類(lèi)型是String類(lèi)型,這種情況可以通過(guò)@Mapper的uses屬性來(lái)實(shí)現(xiàn):

@Data
@NoArgsConstructor
@AllArgsConstructor
publicclassCustomer{
privateLongid;
privateStringname;
privateBooleanisDisable;
}

@Data
publicclassCustomerDto{
privateLongid;
privateStringcustomerName;
privateStringdisable;
}

定義轉(zhuǎn)換規(guī)則的類(lèi):

publicclassBooleanStrFormat{
publicStringtoStr(BooleanisDisable){
if(isDisable){
return"Y";
}else{
return"N";
}
}
publicBooleantoBoolean(Stringstr){
if(str.equals("Y")){
returntrue;
}else{
returnfalse;
}
}
}

定義Mapper,@Mapper( uses = { BooleanStrFormat.class}),注意,這里的users屬性用于引用之前定義的轉(zhuǎn)換規(guī)則的類(lèi):

@Mapper(uses={BooleanStrFormat.class})
publicinterfaceCustomerMapper{

CustomerMapperINSTANCES=Mappers.getMapper(CustomerMapper.class);

@Mappings({
@Mapping(source="name",target="customerName"),
@Mapping(source="isDisable",target="disable")
})
CustomerDtotoCustomerDto(Customercustomer);
}

這樣子,Customer類(lèi)中的isDisable屬性的true就會(huì)轉(zhuǎn)變成CustomerDto中的disable屬性的yes。

MapStruct自動(dòng)生成的類(lèi)中的代碼:

@Generated(
value="org.mapstruct.ap.MappingProcessor",
date="2019-02-14T1618+0800",
comments="version:1.3.0.Final,compiler:javac,environment:Java1.8.0_181(OracleCorporation)"
)
publicclassCustomerMapperImplimplementsCustomerMapper{

//引用uses中指定的類(lèi)
privatefinalBooleanStrFormatbooleanStrFormat=newBooleanStrFormat();

@Override
publicCustomerDtotoCustomerDto(Customercustomer){
if(customer==null){
returnnull;
}

CustomerDtocustomerDto=newCustomerDto();
//轉(zhuǎn)換方式的使用
customerDto.setDisable(booleanStrFormat.toStr(customer.getIsDisable()));
customerDto.setCustomerName(customer.getName());
customerDto.setId(customer.getId());

returncustomerDto;
}
}

要注意的是,如果使用了例如像spring這樣的環(huán)境,Mapper引入uses類(lèi)實(shí)例的方式將是自動(dòng)注入,那么這個(gè)類(lèi)也應(yīng)該納入Spring容器:

CustomerMapper.java指定使用spring

@Mapper(componentModel="spring",uses={BooleanStrFormat.class})
publicinterfaceCustomerMapper{

CustomerMapperINSTANCES=Mappers.getMapper(CustomerMapper.class);

@Mappings({
@Mapping(source="name",target="customerName"),
@Mapping(source="isDisable",target="disable")
})
CustomerDtotoCustomerDto(Customercustomer);
}

轉(zhuǎn)換類(lèi)要加入Spring容器:

@Component
publicclassBooleanStrFormat{
publicStringtoStr(BooleanisDisable){
if(isDisable){
return"Y";
}else{
return"N";
}
}
publicBooleantoBoolean(Stringstr){
if(str.equals("Y")){
returntrue;
}else{
returnfalse;
}
}
}

MapStruct自動(dòng)生成的類(lèi):

@Generated(
value="org.mapstruct.ap.MappingProcessor",
date="2019-02-14T1635+0800",
comments="version:1.3.0.Final,compiler:javac,environment:Java1.8.0_181(OracleCorporation)"
)
@Component
publicclassCustomerMapperImplimplementsCustomerMapper{

//使用自動(dòng)注入的方式引入
@Autowired
privateBooleanStrFormatbooleanStrFormat;

@Override
publicCustomerDtotoCustomerDto(Customercustomer){
if(customer==null){
returnnull;
}

CustomerDtocustomerDto=newCustomerDto();

customerDto.setDisable(booleanStrFormat.toStr(customer.getIsDisable()));
customerDto.setCustomerName(customer.getName());
customerDto.setId(customer.getId());

returncustomerDto;
}
}

原文標(biāo)題:實(shí)體映射最強(qiáng)工具類(lèi):MapStruct 真香!

文章出處:【微信公眾號(hào):Android編程精選】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。

審核編輯:湯梓紅
聲明:本文內(nèi)容及配圖由入駐作者撰寫(xiě)或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場(chǎng)。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問(wèn)題,請(qǐng)聯(lián)系本站處理。 舉報(bào)投訴
  • 數(shù)據(jù)傳輸
    +關(guān)注

    關(guān)注

    9

    文章

    1698

    瀏覽量

    64211
  • JAVA
    +關(guān)注

    關(guān)注

    19

    文章

    2943

    瀏覽量

    104096
  • User
    +關(guān)注

    關(guān)注

    1

    文章

    27

    瀏覽量

    10761

原文標(biāo)題:實(shí)體映射最強(qiáng)工具類(lèi):MapStruct 真香!

文章出處:【微信號(hào):AndroidPush,微信公眾號(hào):Android編程精選】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。

收藏 人收藏

    評(píng)論

    相關(guān)推薦

    請(qǐng)問(wèn)Linear Lab Tool具體是做什么的?

    請(qǐng)問(wèn)Linear Lab Tool具體是做什么的呢?
    發(fā)表于 01-05 12:52

    請(qǐng)問(wèn)培訓(xùn)生是做什么的

    技術(shù)員培訓(xùn)生工程師培訓(xùn)生這些職位是做什么的·····以后怎么發(fā)展······
    發(fā)表于 02-18 01:24

    請(qǐng)問(wèn)IIO軟件是做什么的?

    IIO軟件是做什么的?剛接觸AD9361,SD卡中的系統(tǒng)帶的軟件,可我看了半天,不知道具體該怎么設(shè)置波形,還有設(shè)置好的波形是輸出波形,還是輸入波形???
    發(fā)表于 08-03 06:58

    請(qǐng)問(wèn)sot-223封裝的第四管腳是用來(lái)做什么的?

    sot-223的第四管腳是用來(lái)做什么的?上面標(biāo)有TAB IS VOUT或是TAB IS GND是什么意思?
    發(fā)表于 09-25 11:35

    bootloader程序是用來(lái)做什么的?與startup程序有區(qū)別么?

    想請(qǐng)教一個(gè)問(wèn)題,MCU中的bootloader程序是做什么的?與在KEIL中寫(xiě)的startup程序有區(qū)別么?另外ISP升級(jí)又與bootloader有什么關(guān)系?求大俠賜教
    發(fā)表于 01-08 10:55

    Native是用來(lái)做什么的?

    PDK中的mos管類(lèi)型中的Native是用來(lái)做什么的?
    發(fā)表于 01-18 06:46

    存儲(chǔ)器是用來(lái)做什么的?系統(tǒng)總線是什么

    存儲(chǔ)器是用來(lái)做什么的?系統(tǒng)總線是什么?有何功能?CPU中央處理器是什么?有何功能?
    發(fā)表于 01-21 07:24

    RK3399Pro開(kāi)發(fā)板是用來(lái)做什么的

    RK3399Pro開(kāi)發(fā)板是用來(lái)做什么的?RK3399Pro開(kāi)發(fā)板報(bào)考哪些組成部分呢?有何應(yīng)用?
    發(fā)表于 02-15 07:55

    sot-223的第四管腳是用來(lái)做什么的?

    sot-223的第四管腳是用來(lái)做什么的? 上面標(biāo)有TAB IS VOUT或是TAB IS GND是什么意思?
    發(fā)表于 11-22 07:25

    半導(dǎo)體公司是做什么的

    半導(dǎo)體指常溫下導(dǎo)電性能介于導(dǎo)體與絕緣體之間的材料。半導(dǎo)體在收音機(jī)、電視機(jī)以及測(cè)溫上有著廣泛的應(yīng)用。如二極管就是采用半導(dǎo)體制作的器件。本文主要詳細(xì)介紹了半導(dǎo)體公司是做什么的
    的頭像 發(fā)表于 09-27 11:10 ?4w次閱讀

    程序員到底是做什么的

    很多人問(wèn)程序員是是做什么的?或者問(wèn)IT是做什么的?對(duì)于非IT行業(yè)的人很難有時(shí)間慢慢解釋清楚,下面我結(jié)合自己的理解談一談吧。
    的頭像 發(fā)表于 02-12 16:17 ?9209次閱讀

    Mirru 應(yīng)用是用來(lái)做什么的 為什么要使用手部追蹤

    Mirru 應(yīng)用是用來(lái)做什么的? Mirru 是一款正在開(kāi)發(fā)中的免費(fèi)開(kāi)源 Android 應(yīng)用,可以通過(guò)追蹤手部 (Hand Tracking) 來(lái)控制機(jī)械手。通過(guò)這款應(yīng)用,用戶(hù)可以將其真手的抓握
    的頭像 發(fā)表于 08-12 15:04 ?1823次閱讀

    鈮酸鋰是用來(lái)做什么的

    歡迎大家來(lái)到小K的實(shí)驗(yàn)室。本期小K的實(shí)驗(yàn)室迎來(lái)了一位叫鈮酸鋰的客人。 你聽(tīng)說(shuō)過(guò)鈮酸鋰嗎? 你知道鈮酸鋰是用來(lái)做什么的嗎? 你知道鈮酸鋰能讓你成為人生贏家嗎? 且聽(tīng)小K給您好好介紹這位“客人”。 小K
    的頭像 發(fā)表于 03-19 10:40 ?6419次閱讀

    ip地址是做什么的

    如果你現(xiàn)在正在看我的這篇文章,那說(shuō)明你已經(jīng)連接上了互聯(lián)網(wǎng)。說(shuō)到互聯(lián)網(wǎng),你一定聽(tīng)說(shuō) ip 地址這個(gè)概念,你知道 ip 地址是做什么的嗎?與之而來(lái)的還有公網(wǎng) ip ,私網(wǎng) ip ,你知道有什么區(qū)別嗎?
    的頭像 發(fā)表于 09-16 10:03 ?3091次閱讀

    總線是用來(lái)做什么的?

    昨天一位網(wǎng)友表示自己搞不懂總線是做什么的,網(wǎng)上的標(biāo)準(zhǔn)答案也晦澀難懂,讓我用比較通俗的話來(lái)解釋一下,下面我用例子說(shuō)明說(shuō)明。 我們知道,汽車(chē)上有很多的電子設(shè)備,它們就是通過(guò)總線連接的。總線,是一條公共
    的頭像 發(fā)表于 04-05 14:05 ?774次閱讀