Continuation of the last time
http://qiita.com/kentama/items/09a8573bd5d0831b470e http://qiita.com/kentama/items/55c71ca28f0c5af8d4ed
Type conversion can be performed at the time of mapping like int-> String. At that time, the format can also be specified.
Bean
public class Hoge {
private String date;
private String number;
// constructor/getter/setter
}
public class Fuga {
private LocalDate date;
private int number;
// constructor/getter/setter
}
@Mapper
public interface ConversionMapper {
ConversionMapper INSTANCE = Mappers.getMapper(ConversionMapper.class);
@Mapping(target = "date", source = "date", dateFormat = "yyyy year MM month dd day") // (1)
@Mapping(target = "number", source = "number", numberFormat = "#,###") // (2)
Hoge convert(Fuga fuga);
}
(1) Specify the date format in the dateFormat
attribute.
(2) Specify the numerical format in the numberFormat
attribute.
Fuga fuga = new Fuga(LocalDate.of(2017, 4, 1), 1234567890);
Hoge hoge = ConversionMapper.INSTANCE.convert(fuga);
System.out.println(hoge.getDate()); //April 01, 2017
System.out.println(hoge.getNumber()); // 1,234,567,890
You can also type-convert a list by using @IterableMapping
.
This time, int-> String is converted and 3 digits are padded with zeros.
@Mapper
public interface ConversionMapper {
ConversionMapper INSTANCE = Mappers.getMapper(ConversionMapper.class);
@IterableMapping(numberFormat = "000")
List<String> zeroPadding(List<Integer> numbers);
}
List<String> numbers = ConversionMapper.INSTANCE.zeroPadding(Arrays.asList(1, 2, 56, 76, 863));
System.out.println(numbers); // [001, 002, 056, 076, 863]
You can use other Mapper from Mapper. You can specify a Mapper generated by MapStruct or a Mapper implemented independently.
Bean
public class Hoge {
private String date;
// constructor/getter/setter
}
public class Fuga {
private LocalDate date;
// constructor/getter/setter
}
This time, create your own Mapper for LocalDate-> String conversion and reuse it.
Original Mapper
public class LocalDateMapper {
public String asString(LocalDate date) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy year MM month dd day");
return date != null ? date.format(formatter) : null;
}
}
@Mapper(uses = LocalDateMapper.class) // (1)
public interface InvokingOtherMapper {
InvokingOtherMapper INSTANCE = Mappers.getMapper(InvokingOtherMapper.class);
Hoge fugaToHoge(Fuga fuga);
}
(1) Specify the Mapper to be reused in the ʻuses` attribute.
Fuga fuga = new Fuga(LocalDate.of(2017, 4, 1));
Hoge hoge = InvokingOtherMapper.INSTANCE.fugaToHoge(fuga);
System.out.println(hoge.getDate()); //April 01, 2017
You can add annotations to your own mapping logic so that Mapper can use your own mapping logic. Here, as an example, mapping to convert to uppercase and lowercase is performed.
Bean
public class Hoge {
private String hoge1;
private String hoge2;
// constructor/getter/setter
}
public class Fuga {
public String fuga1;
public String fuga2;
// constructor/getter/setter
}
public class Characters {
public String upperCase(String string) {
return (string == null) ? null : string.toUpperCase();
}
public String lowerCase(String string) {
return (string == null) ? null : string.toLowerCase();
}
}
@Qualifier
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.CLASS)
public @interface CharacterConverter {
}
@Qualifier
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.CLASS)
public @interface ToUpper {
}
@Qualifier
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.CLASS)
public @interface ToLower {
}
Add the created annotation to the mapping logic.
@CharacterConverter
public class Characters {
@ToUpper
public String upperCase(String string) {
return (string == null) ? null : string.toUpperCase();
}
@ToLower
public String lowerCase(String string) {
return (string == null) ? null : string.toLowerCase();
}
}
@Mapper(uses = Characters.class) // (1)
public interface HogeFugaMapper {
HogeFugaMapper INSTANCE = Mappers.getMapper(HogeFugaMapper.class);
@Mapping(target = "hoge1", source = "fuga1", qualifiedBy = { CharacterConverter.class, ToUpper.class }) // (2)
@Mapping(target = "hoge2", source = "fuga2", qualifiedBy = { CharacterConverter.class, ToLower.class }) // (3)
Hoge fugaToHoge(Fuga fuga);
}
(1) Specify the mapping logic class
(2) (3) Specify the class level of the mapping logic class and the annotation specified at the method level in the qualifiedBy
attribute.
Fuga fuga = new Fuga("abc", "XYZ");
Hoge hoge = HogeFugaMapper.INSTANCE.fugaToHoge(fuga);
System.out.println(hoge.getHoge1()); // ABC
System.out.println(hoge.getHoge2()); // xyz
Next time.
Recommended Posts