最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java實(shí)體映射工具M(jìn)apStruct使用方法詳解

 更新時(shí)間:2021年11月08日 09:36:39   作者:AnakinSky  
MapStruct是用于代碼中JavaBean對象之間的轉(zhuǎn)換,例如DO轉(zhuǎn)換為DTO,DTO轉(zhuǎn)換為VO,或Entity轉(zhuǎn)換為VO等場景,這篇文章主要給大家介紹了關(guān)于Java實(shí)體映射工具M(jìn)apStruct使用的相關(guān)資料,需要的朋友可以參考下

1.序

通常在后端開發(fā)中經(jīng)常不直接返回實(shí)體Entity類,經(jīng)過處理轉(zhuǎn)換返回前端,前端提交過來的對象也需要經(jīng)過轉(zhuǎn)換Entity實(shí)體才做存儲(chǔ);通常使用的BeanUtils.copyProperties方法也比較粗暴,不僅效率低下(使用反射)而且僅映射相同名的屬性,多數(shù)情況下還需要手動(dòng)編寫對應(yīng)的轉(zhuǎn)換方法實(shí)現(xiàn)。

插件MapStruct以接口方法結(jié)合注解優(yōu)雅實(shí)現(xiàn)對象轉(zhuǎn)換,MapStruct生成器生成代碼以更貼近原生的Setter、Getter方法處理屬性映射更為高效。

https://github.com/mapstruct/mapstruct/

https://github.com/mapstruct/mapstruct-examples

2.簡單用例

實(shí)體對象User

@Data
@AllArgsConstructor
public class User {
    private int id;
    private String name;
    private int age;
    private String address;
}

轉(zhuǎn)換對象UserVO

@Mapper
public interface UserConvert {
    UserConvert INSTANCE = Mappers.getMapper(UserConvert.class);
    @Mapping(source = "name", target = "userName")
    UserVO toVO(User entity);
}

轉(zhuǎn)換接口

@Test
public void contextLoads() {
    User user = new User(0, "Tester", 1, "上海市徐匯區(qū)");
    UserVO userVO = UserConvert.INSTANCE.toVO(user);
}

使用示例

@Test
public void contextLoads() {
    User user = new User(0, "Tester", 1, "上海市徐匯區(qū)");
    UserVO userVO = UserConvert.INSTANCE.toVO(user);
}

3.使用詳解

1)關(guān)于接口注解@Mapper幾種屬性用法詳解

uses 使用其他手動(dòng)編寫的或者其他Mapper接口覆寫相關(guān)的轉(zhuǎn)換方法,不能循環(huán)引用

@Mapper(uses=DateMapper.class)

imports 引用相關(guān)類,允許通過mapping.expression()、mapping.defaultExpression()直接使用這些類型

@Mapper(imports = DateUtil.class)
public interface UserConvert {
    UserConvert INSTANCE = Mappers.getMapper(UserConvert.class);
    @Mappings({
            @Mapping(source = "name", target = "userName"),
            // 以指定方法轉(zhuǎn)換屬性,這里如果不使用imports,則需要寫全引用類包路徑
            @Mapping(target = "birthday", expression = "java(DateUtil.formatDate(entity.getBirthday()))")
    })
    UserVO toVO(User entity);
}

unmappedSourcePolicy、unmappedTargetPolicy 針對源類型/目標(biāo)類型中未映射屬性的反饋策略

typeConversionPolicy 針對有損轉(zhuǎn)換的反饋策略,例如Long轉(zhuǎn)Integer

反饋策略主要有三種:IGNORE,默認(rèn)值,忽略未映射的字段。WARN,警告。ERROR,報(bào)錯(cuò)

@Mapper(unmappedSourcePolicy = ReportingPolicy.ERROR)

componentModel 指定生成映射器實(shí)例的模式,主要有四種:

default,不主動(dòng)生成實(shí)例,通常以Mappers.getMapper(Class)實(shí)例化。

cdi,以CDI標(biāo)準(zhǔn)實(shí)例化映射器,使用@Inject注入相關(guān)實(shí)例,

spring,Spring Bean方式實(shí)例化,

jsr330,jsr330標(biāo)準(zhǔn)實(shí)例化

@Mapper(componentModel = "spring")
public interface UserConvert {
    @Mapping(source = "name", target = "userName")
    UserVO toVO(User entity);
}
@Autowired
private UserConvert userConvert;

implementationName 指定實(shí)現(xiàn)類名稱,映射生成器接口會(huì)自動(dòng)生成實(shí)現(xiàn)類<CLASS_NAME>Impl,使用此屬性可避免類沖突

implementationPackage 指定實(shí)現(xiàn)類包路徑

config 指定配置類,由指定的@MapperConfig配置類,config導(dǎo)入相關(guān)配置

配置類

@MapperConfig(
        uses = DateUtil.class,
        unmappedSourcePolicy = ReportingPolicy.WARN
)
public interface UserConfig {
}

導(dǎo)入配置類

@Mapper(componentModel = "spring", config = UserConfig.class)

collectionMappingStrategy 集合映射策略,這里注意集合映射時(shí),如果集合中的類型已有對應(yīng)轉(zhuǎn)換方法,集合轉(zhuǎn)換時(shí)會(huì)優(yōu)先使用

@Mappings({
        @Mapping(source = "name", target = "userName"),
        @Mapping(target = "birthday", expression = "java(DateUtil.formatDate(entity.getBirthday()))")
})
UserVO toVO(User entity);
List<UserVO> collectionCvt(List<User> entities);

GENERATED CODE 生成器生成代碼

public List<UserVO> collectionCvt(List<User> entities) {
    if (entities == null) {
        return null;
    } else {
        List<UserVO> list = new ArrayList(entities.size());
        Iterator var3 = entities.iterator();

        while(var3.hasNext()) {
            User user = (User)var3.next();
            // 集合轉(zhuǎn)換時(shí)優(yōu)先使用了已定義的toVO方法
            list.add(this.toVO(user));
        }

        return list;
    }
}

nullValueMappingStrategy null作為源值映射策略;RETURN_NULL默認(rèn)返回null, RETURN_DEFAULT返回默認(rèn)值,對于對象會(huì)通過構(gòu)造器自動(dòng)構(gòu)造對象返回,集合會(huì)返回空集合

當(dāng)值為RETURN_DEFAULT時(shí),如果映射規(guī)則中包含Mapping.expression、Mapping.constant必須手動(dòng)判空處理,否則NPE

@MapperConfig(nullValueMappingStrategy = NullValueMappingStrategy.RETURN_DEFAULT)

NullValuePropertyMappingStrategy null作為源屬性映射策略;SET_TO_NULL默認(rèn)返回null,SET_TO_DEFAULT返回默認(rèn)值,IGNORE 忽略該值,以目標(biāo)對象已存在的值為準(zhǔn)

MappingInheritanceStrategy 繼承方法級(jí)映射配置策略:EXPLICIT 顯示使用InheritConfiguration生效。

AUTO_INHERIT_FROM_CONFIG 自動(dòng)繼承正向轉(zhuǎn)換的配置。AUTO_INHERIT_REVERSE_FROM_CONFIG 自動(dòng)繼承反向轉(zhuǎn)換的配置。AUTO_INHERIT_ALL_FROM_CONFIG 都繼承

@MapperConfig(
        mappingInheritanceStrategy = MappingInheritanceStrategy.EXPLICIT
)
@InheritConfiguration
void cvtVO(User entity, @MappingTarget UserVO vo);

nullValueCheckStrategy 空值監(jiān)測策略

2) 其他方法級(jí)別注解

@InheritInverseConfiguration 反向轉(zhuǎn)換時(shí)繼承映射規(guī)則

@Mapping 配置類型屬性的映射規(guī)則;

dateFormat 格式化日期

@Mapping(target = "birthday", dateFormat = "yyyy-MM-dd HH:mm:ss")

numberFormat 數(shù)字格式化

@Mapping(target = "price", numberFormat = "$#.00")

constant 常量

@Mapping(target = "age", constant = "0")

總結(jié)

到此這篇關(guān)于Java實(shí)體映射工具M(jìn)apStruct使用的文章就介紹到這了,更多相關(guān)Java實(shí)體映射工具M(jìn)apStruct內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

轮台县| 德钦县| 合川市| 日喀则市| 辉南县| 穆棱市| 伊川县| 汝阳县| 海伦市| 莱西市| 蚌埠市| 辉南县| 施秉县| 乐清市| 望江县| 喀什市| 江川县| 苍山县| 观塘区| 瑞丽市| 黄山市| 天气| 清镇市| 太湖县| 玉树县| 陆川县| 文化| 济阳县| 乌兰县| 石楼县| 盐源县| 鄂尔多斯市| 周口市| 黔西| 南阳市| 伽师县| 辽宁省| 保山市| 乌兰浩特市| 丰镇市| 木兰县|