LoginSignup
9

More than 5 years have passed since last update.

MapStructでBeanマッピング その1

Last updated at Posted at 2017-03-14

MapStruct 1.1.0を使用

MapStructの詳細は公式サイトを参照
http://mapstruct.org/

同じフィールドを持つBean同士のマッピング

Bean
public class ProfileForm {
    private String name;
    private int age;
    private String location;

    // constructor/getter/setter
}

public class Profile {
    private String name;
    private int age;
    private String location;

    // constructor/getter/setter
}

Mapperの作成

@Mapper
public interface ProfileMapper {
    ProfileMapper INSTANCE = Mappers.getMapper(ProfileMapper.class);

    public Profile profileFormToProfile(ProfileForm form);
}

実行

ProfileForm form = new ProfileForm("マイケル・スコフィールド", 30, "アメリカ合衆国");
Profile profile = ProfileMapper.INSTANCE.profileFormToProfile(form);

System.out.println(profile.getName()); // マイケル・スコフィールド
System.out.println(profile.getAge());  // 30
System.out.println(profile.getLocation()); // アメリカ合衆国

フィールドの一部をマッピング対象外にする

フィールドの一部をマッピング対象外にするには、 @Mapping アノテーションを利用する。
target属性に対象外にするフィールドと、ignore属性に true を設定する。

@Mapper
public interface ProfileMapper {
    ProfileMapper INSTANCE = Mappers.getMapper(ProfileMapper.class);

    @Mapping(target = "name", ignore = true)
    public Profile profileFormToProfile(ProfileForm form);
}

リスト同士のマッピング

Mapperの修正

@Mapper
public interface ProfileMapper {
    ProfileMapper INSTANCE = Mappers.getMapper(ProfileMapper.class);

    public Profile profileFormToProfile(ProfileForm form);
    // 追加
    public List<Profile> profileFormToProfile(List<ProfileForm> forms);
}

実行

List<ProfileForm> forms = Arrays.asList(new ProfileForm("マイケル・スコフィールド", 30, "アメリカ合衆国")
                                      , new ProfileForm("リンカーン・バローズ", 34, "アメリカ合衆国"));
List<Profile> profiles = ProfileMapper.INSTANCE.profileFormToProfile(forms);

System.out.println(profiles.get(1).getName()); // リンカーン・バローズ
System.out.println(profiles.get(1).getAge());  // 34
System.out.println(profiles.get(1).getLocation()); // アメリカ合衆国

異なるフィールドを持つBean同士のマッピング

Bean
public class Foo {
    private String foo1;
    private String foo2;

    // constructor/getter/setter
}

public class Bar {
    private String bar1;
    private String bar2;

    // constructor/getter/setter
}

Mapperの作成


@Mapper
public interface FooBarMapper {
    FooBarMapper INSTANCE = Mappers.getMapper(FooBarMapper.class);

    @Mapping(target = "bar1", source = "foo1")
    @Mapping(target = "bar2", source = "foo2")
    public Bar fooToBar(Foo foo);
}

@Mapping アノテーションでマッピングさせたいフィールド(target)と、マッピングするフィールド(source)を指定する。

実行

Foo foo = new Foo("あいうえお", "かきくけこ");
Bar bar = FooBarMapper.INSTANCE.fooToBar(foo);

System.out.println(bar.getBar1()); // あいうえお
System.out.println(bar.getBar2()); // かきくけこ

他にもマッピングのための機能があるが、それらは次回。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9