私は多くのインスタンスフィールド(多くはオプションです)を持つ(Java)クラスを持っています。私はすべてのフィールド(クラス)を不変にしたいと思います。だから、クラスのインスタンスを構築するためにBuilderパターンを使用したいと思います。Builderパターンを使用してクラスのインスタンスを作成するようmyBatisを構成できますか?
ビルダーパターンを使用してクラスのインスタンスを作成するようmyBatisを設定できますか? myBatisにマップを返し、そのマップを使用してコード内のインスタンスを構築させることができます。しかし、私はJava Beansとコンストラクタを使ってインスタンスを作成する方法と同様に、このマッピングを設定する方法(またはいくつかの規約を使用する)を探しています。
<!-- this doesn't work but I think h3adache suggest that I could have the resultType
be com.example.model.CarFacts.Builder and use the Builder constructor. But I'm not sure how
I would call the methods (such previousOwner(String)) to populate optional params -->
<mapper namespace="com.example.persistence.CarFactsMapper">
<select id="selectCarFacts" resultType="com.example.model.CarFacts">
select *
from CarFacts
</select>
</mapper>
最後に、私はマッパーインタフェースを持っている:として私はマッパーを持っている、そして、
package com.example.model;
// domain model class with builder
public final class CarFacts {
private final double price;
private final double numDoors;
private final String make;
private final String model;
private final String previousOwner;
private final String description;
public static class Builder {
// required params
private final double price;
private final String make;
private final String model;
// optional params
private final String previousOwner;
private final String description;
private final double numDoors;
public Builder(double price, String make, String model) {
this.price = price;
this.make = make;
this.model = model;
}
public Builder previousOwner(String previousOwner) {
this.previousOwner = previousOwner;
return this;
}
// other methods for optional param
public CarFacts build() {
return new CarFacts(this);
}
}
private CarFacts(Builder builder) {
this.price = builder.price;
//etc.
}
}
:
編集は、ここで
は例です(例を含むように) :
package com.example.persistence.CarFactsMapper;
public interface CarFactsMapper{
List<CarFacts> selectCarFacts();
}
myBatisを介して静的ファクトリメソッドを使用してインスタンスを作成することもできます。例:
public final class Person {
private final String lastName;
private final String firstName;
private Person(String lastName, String firstName) {
this.lastName = lastName;
this.firstName = firstName;
}
public Person newInstance(String lastName, String firstName) {
return new Person(lastName, firstName);
}
}
具体的には、myBatisにnewInstance(String、String)を呼び出す方法はありますか?
これは質問に答えません。 –