MapStruct文档(八)——高级映射选项

目录

7.1、默认值

7.2、表达式

7.2、确定映射结果的具体类型

7.4、NULL映射控制

7.4.1、源对象NULL映射

7.4.3、在更新时源对象属性NULL映射

7.4.3、何时NULL检查


7.1、默认值

可以给@Mapping的属性defaultValue设置一个值,若果源字段为空,那么目标字段就为此默认值。


@Mapper(uses = BaseMapper.class)
public interface TestMapper {

    @Mapping(target = "name", source = "name", defaultValue = "noName")
    TestBO toBO(TestPO testPO);

}
 
@Component
public class TestMapperImpl implements TestMapper {

    @Override
    public TestBO toBO(TestPO testPO) {
        if ( testPO == null ) {
            return null;
        }

        TestBO testBO = new TestBO();

        if ( testPO.getName() != null ) {
            testBO.setName( testPO.getName() );
        }expression
        else {
            testBO.setName( "noName" );
        }
        testBO.setId( testPO.getId() );
        if ( testPO.getPrice() != null ) {
            testBO.setPrice( testPO.getPrice().toString() );
        }

        return testBO;
    }
}

7.2、表达式

可以设置@Mappingexpression的值来插入一段java代码


@Mapper(uses = BaseMapper.class)
public interface TestMapper {

    @Mapping(target = "name", expression = "java(testPO.getName() + \"BO\")")
    TestBO toBO(TestPO testPO);

}
 
@Component
public class TestMapperImpl implements TestMapper {

    @Override
    public TestBO toBO(TestPO testPO) {
        if ( testPO == null ) {
            return null;
        }

        TestBO testBO = new TestBO();

        testBO.setId( testPO.getId() );
        if ( testPO.getPrice() != null ) {
            testBO.setPrice( testPO.getPrice().toString() );
        }
        if ( testPO.getCreateTime() != null ) {
            testBO.setCreateTime( new SimpleDateFormat().format( testPO.getCreateTime() ) );
        }

        testBO.setName( testPO.getName() + "BO" );

        return testBO;
    }
}
 
 

testBO.setName( testPO.getName() + "BO" )就是使用了插入的代码。

 

还可以调用用@Mapper#imports导入的类中的方法,当然也包括导入类的构造方法。


public class StringBOUtils {

    public static String toBOString(String poString) {
        return poString + "BO";
    }
}
 
@Mapper(uses = BaseMapper.class, imports = {StringBOUtils.class})
public interface TestMapper {

    @Mapping(target = "name", expression = "java(StringBOUtils.toBOString(testPO.getName()))")
    TestBO toBO(TestPO testPO);

}
 
@Component
public class TestMapperImpl implements TestMapper {

    @Override
    public TestBO toBO(TestPO testPO) {
        if ( testPO == null ) {
            return null;
        }

        TestBO testBO = new TestBO();

        testBO.setId( testPO.getId() );
        if ( testPO.getPrice() != null ) {
            testBO.setPrice( testPO.getPrice().toString() );
        }
        if ( testPO.getCreateTime() != null ) {
            testBO.setCreateTime( new SimpleDateFormat().format( testPO.getCreateTime() ) );
        }

        testBO.setName( StringBOUtils.toBOString(testPO.getName()) );

        return testBO;
    }
}

 

还可以设置@MappingdefaultExpression设置默认表达式,在source的值为null时,调用此默认表达式代码。


@Mapper(uses = BaseMapper.class, imports = {StringBOUtils.class})
public interface TestMapper {

    @Mapping(target = "name", source = "name", defaultExpression  = "java(StringBOUtils.toBOString(testPO.getName()))")
    TestBO toBO(TestPO testPO);

}


@Component
public class TestMapperImpl implements TestMapper {

    @Override
    public TestBO toBO(TestPO testPO) {
        if ( testPO == null ) {
            return null;
        }

        TestBO testBO = new TestBO();

        if ( testPO.getName() != null ) {
            testBO.setName( testPO.getName() );
        }
        else {
            testBO.setName( StringBOUtils.toBOString(testPO.getName()) );
        }
        testBO.setId( testPO.getId() );
        if ( testPO.getPrice() != null ) {
            testBO.setPrice( testPO.getPrice().toString() );
        }
        if ( testPO.getCreateTime() != null ) {
            testBO.setCreateTime( new SimpleDateFormat().format( testPO.getCreateTime() ) );
        }

        return testBO;
    }
}

7.2、确定映射结果的具体类型

若果映射的结果是父类,又存在不同子类的映射方法,编译会报错。

此时需要使用@Mapping@BeanMapping的resultType指定映射结果的具体类型。


@Mapper
public class BaseMapper {


    public TestSixBO createSixBO() {
        return new TestSixBO();
    }

    public TestEightBO createEightBO() {
        return new TestEightBO();
    }

}


@Mapper(uses = {BaseMapper.class})
public interface TestMapper {

    @BeanMapping(resultType = TestEightBO.class)
    BaseBO toBO(TestFourPO testFourPO);

}


@Component
public class TestMapperImpl implements TestMapper {

    @Autowired
    private BaseMapper baseMapper;

    @Override
    public BaseBO toBO(TestFourPO testFourPO) {
        if ( testFourPO == null ) {
            return null;
        }

        TestEightBO baseBO = baseMapper.createEightBO();

        baseBO.setId( testFourPO.getId() );
        baseBO.setName( testFourPO.getName() );
        baseBO.setCreateTime( testFourPO.getCreateTime() );

        return baseBO;
    }
}

7.4、NULL映射控制

7.4.1、源对象NULL映射

当映射的source对象为null时可以通过@BeanMapping@IterableMapping@MapMapping(优先级最高),@Mappe@MappingConfig(优先级最低)的nullValueMappingStrategy策略来控制NULL值的映射结果。

策略值有:NullValueMappingStrategy.RETURN_NULL(默认,源为null,目标直接返回null)、NullValueMappingStrategy.RETURN_DEFAULT(返回一个对象,除填充的常量和表达式,其他字段属性为空;集合返回一个对象,size为0)。


@Mapper
public interface TestMapper {

    @IterableMapping(nullValueMappingStrategy = NullValueMappingStrategy.RETURN_DEFAULT)
    List<TestThreeBO> toBO(List<TestFivePO> testFivePO);

}
 
@Component
public class TestMapperImpl implements TestMapper {

    @Override
    public List<TestThreeBO> toBO(List<TestFivePO> testFivePO) {
        if ( testFivePO == null ) {
            return new ArrayList<TestThreeBO>();
        }

        List<TestThreeBO> list = new ArrayList<TestThreeBO>( testFivePO.size() );
        for ( TestFivePO testFivePO1 : testFivePO ) {
            list.add( testFivePOToTestThreeBO( testFivePO1 ) );
        }

        return list;
    }

    protected TestBO testPOToTestBO(TestPO testPO) {
        if ( testPO == null ) {
            return null;
        }

        TestBO testBO = new TestBO();

        testBO.setId( testPO.getId() );
        testBO.setName( testPO.getName() );
        if ( testPO.getPrice() != null ) {
            testBO.setPrice( testPO.getPrice().toString() );
        }
        if ( testPO.getCreateTime() != null ) {
            testBO.setCreateTime( new SimpleDateFormat().format( testPO.getCreateTime() ) );
        }

        return testBO;
    }

    protected TestThreeBO testFivePOToTestThreeBO(TestFivePO testFivePO) {
        if ( testFivePO == null ) {
            return null;
        }

        TestThreeBO testThreeBO = new TestThreeBO();

        testThreeBO.setTest( testPOToTestBO( testFivePO.getTest() ) );

        return testThreeBO;
    }
}

集合使用了NullValueMappingStrategy.RETURN_DEFAULT策略所以源为NULL时返回return new ArrayList<TestThreeBO>();bean对象使用默认策略所以源为NULL时返回return null

 

bean对象使用NullValueMappingStrategy.RETURN_DEFAULT策略,并且知道常量和表达式。


@Mapper
public interface TestMapper {

    @BeanMapping(nullValueMappingStrategy = NullValueMappingStrategy.RETURN_DEFAULT)
    @Mapping(target = "id", constant = "0L")
    @Mapping(target = "name", expression = "java(testPO.getName() + \"BO\")")
    TestBO toBO(TestPO testPO);

}
 
@Component
public class TestMapperImpl implements TestMapper {

    @Override
    public TestBO toBO(TestPO testPO) {

        TestBO testBO = new TestBO();

        if ( testPO != null ) {
            if ( testPO.getPrice() != null ) {
                testBO.setPrice( testPO.getPrice().toString() );
            }
            else {
                testBO.setPrice( testPO.getPrice() + "BO" );
            }
            if ( testPO.getCreateTime() != null ) {
                testBO.setCreateTime( new SimpleDateFormat().format( testPO.getCreateTime() ) );
            }
        }
        testBO.setId( (long) 0L );
        testBO.setName( testPO.getName() + "BO" );

        return testBO;
    }
}

7.4.3、在更新时源对象属性NULL映射

在使用@MappingTarget更新目标时,可以设置@Mapping@BeanMapping(优先级最高),@Mapper@MappingConfig(优先级最底)的nullValuePropertyMappingStrategy属性值,用于设置当源对象属性为null时,如何设置目标属性的值。nullValuePropertyMappingStrategy的值有:

NullValuePropertyMappingStrategy.SET_TO_DEFAULT:源属性为null,目标属性会被赋予特定的默认值。List被赋予ArrayList,Map被赋予HashMap,数组就是空数组,String是“”,基本类型或包装类是0或false,对象是空的构造方法。

NullValuePropertyMappingStrategy.IGNORE:源属性为null,忽略目标属性的设值。

NullValuePropertyMappingStrategy.SET_TO_NULL:默认,源属性是null,目标属性也是null。


@Mapper
public interface TestMapper {

    @Mapping(target = "name", nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.SET_TO_DEFAULT)
    @Mapping(target = "price", nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
    void toBO(TestPO testPO, @MappingTarget TestBO testBO);

}
 
@Component
public class TestMapperImpl implements TestMapper {

    @Override
    public void toBO(TestPO testPO, TestBO testBO) {
        if ( testPO == null ) {
            return;
        }

        if ( testPO.getName() != null ) {
            testBO.setName( testPO.getName() );
        }
        else {
            testBO.setName( "" );
        }
        if ( testPO.getPrice() != null ) {
            testBO.setPrice( testPO.getPrice().toString() );
        }
        testBO.setId( testPO.getId() );
        if ( testPO.getCreateTime() != null ) {
            testBO.setCreateTime( new SimpleDateFormat().format( testPO.getCreateTime() ) );
        }
        else {
            testBO.setCreateTime( null );
        }
    }
}

7.4.3、何时NULL检查

在使用@MappingTarget更新目标时,可以设置@Mapping@BeanMapping(优先级最高),@Mapper@MappingConfig(优先级最底)的nullValuePropertyMappingStrategy属性值,设置何时对源属性值进行null检查。nullValuePropertyMappingStrategy可选:

ON_IMPLICIT_CONVERSION:默认;包装类赋予基本类型、直接setter对象类型、类型转换后进行setter。

ALWAYS:总是进行null值检查。


@Mapper
public interface TestMapper {

    @Mapping(target = "id", nullValueCheckStrategy = ALWAYS)
    TestPO toBO(TestBO testBO);

}
 
@Component
public class TestMapperImpl implements TestMapper {

    @Override
    public TestPO toBO(TestBO testBO) {
        if ( testBO == null ) {
            return null;
        }

        TestPO testPO = new TestPO();

        if ( testBO.getId() != null ) {
            testPO.setId( testBO.getId() );
        }
        testPO.setName( testBO.getName() );
        if ( testBO.getPrice() != null ) {
            testPO.setPrice( new BigDecimal( testBO.getPrice() ) );
        }
        try {
            if ( testBO.getCreateTime() != null ) {
                testPO.setCreateTime( new SimpleDateFormat().parse( testBO.getCreateTime() ) );
            }
        }
        catch ( ParseException e ) {
            throw new RuntimeException( e );
        }

        return testPO;
    }
}

if ( testBO.getId() != null ) id进行了null检查。

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值