6
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

MapStructでBeanマッピング その2

Last updated at Posted at 2017-03-23

前回の続き
http://qiita.com/kentama/items/09a8573bd5d0831b470e

複数のBeanから一つのBeanを作成

Bean
public class Hoge {
    private String hoge1;
    private String hoge2;
    private String hoge3;
    private String hoge4;

    // constructor/getter/setter
}

public class Fuga {
    public String fuga1;
    public String fuga2;

    // constructor/getter/setter
}

public class Piyo {
    public String piyo1;
    public String piyo2;

    // constructor/getter/setter
}

Mapperの作成

@Mapping source属性でどのBeanのフィールドを使用するか指定する。

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

    @Mapping(target = "hoge1", source = "fuga.fuga1")
    @Mapping(target = "hoge2", source = "fuga.fuga2")
    @Mapping(target = "hoge3", source = "piyo.piyo1")
    @Mapping(target = "hoge4", source = "piyo.piyo2")
    public Hoge map(Fuga fuga, Piyo piyo);
}

実行

Fuga fuga = new Fuga("a", "b");
Piyo piyo = new Piyo("c", "d");
Hoge hoge = HogeMapper.INSTANCE.map(fuga, piyo);

System.out.println(hoge.getHoge1()); // a
System.out.println(hoge.getHoge2()); // b
System.out.println(hoge.getHoge3()); // c
System.out.println(hoge.getHoge4()); // d

作成済みのBeanを更新する

Mapperの作成

更新対象の引数に @MappingTarget を付与する。

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

    @Mapping(target = "hoge1", source = "fuga1")
    @Mapping(target = "hoge2", source = "fuga2")
    @Mapping(target = "hoge3", source = "fuga1")
    @Mapping(target = "hoge4", source = "fuga2")
    public void updateHogeFromFuga(Fuga fuga, @MappingTarget Hoge hoge);
}

## 実行

Fuga fuga = new Fuga("a", "b");
Hoge hoge = new Hoge("1", "2", "3", "4");
UpdateMapper.INSTANCE.updateHogeFromFuga(fuga, hoge);

System.out.println(hoge.getHoge1()); // a
System.out.println(hoge.getHoge2()); // b
System.out.println(hoge.getHoge3()); // a
System.out.println(hoge.getHoge4()); // b

カスタムメソッドの追加

defaultメソッドを使用した方法

defaultメソッドを定義し、独自のマッピングメソッドを追加することができる。

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

    default Bar fooToBar(Foo foo) {
        Bar bar = new Bar();
        bar.setBar1(foo.getFoo1());
        bar.setBar2(foo.getFoo2());
        return bar;
    }
}

抽象クラスを使用した方法

これまでインターフェイスクラスで作成してきたが、抽象クラスでも作成可能。

@Mapper
public abstract class FooBarMapper {
    public Bar fooToBar(Foo foo) {
        Bar bar = new Bar();
        bar.setBar1(foo.getFoo1());
        bar.setBar2(foo.getFoo2());
        return bar;
    }
}

利用する際には、インスタンスを明示的に作成する必要がある。

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

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

続きは次回。

6
2
0

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
6
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?