MyBatis 3.0.5 のマッピング機能を調べています。データベースは、組み込みモードで実行中の H2 (1.3.160) です。ユーザーマニュアルの助けを借りて、簡単な部品が機能するようになりました。しかし、バッキング ストレージとしてaSet
を使用する a をマッピングするのに苦労しています。HashMap
カスタム セットをフィールドとして持つカスタム コレクションの Java コードを次に示します (簡潔にするために簡略化しています)。
public class CustomCollection
{
@JsonProperty
private CustomSet<CustomItem> customItems;
public CustomCollection()
{
customItems = new CustomSet<CustomItem>();
}
// other stuff
}
これがCustomSet
コードです(これも簡略化されています)
public class CustomSet<E extends CustomItemInterface> extends AbstractSet<E>
{
private ConcurrentHashMap<String, E> items;
public CustomSet()
{
items = new ConcurrentHashMap<String, E>();
}
// other stuff
}
マッピング インターフェイスは次のとおりです。
public interface CustomCollectionMapper
{
CustomCollection select(@Param("somename") String s1, @Param("someothername") String s2);
}
これは、Mybatis フレームワークへの呼び出しを行うコードです。
SqlSessionFactory sqlSessionFactory = (SqlSessionFactory) servletContext.getAttribute("SqlSessionFactory");
SqlSession session = sqlSessionFactory.openSession();
CustomCollection coll = null;
try
{
CustomCollectionMapper mapper = session.getMapper(CustomCollectionMapper.class);
coll = mapper.select(param1, param2);
}
finally
{
session.close();
}
これまでのところ、マッピング XML を考え出すことができました。
<select id="select" resultMap="CustomCollectionMapping">
-- What goes here???
</select>
<resultMap type="com.example.CustomCollection" id="CustomCollectionMapping">
<association property="customItems" javaType="com.example.customSet">
<collection property="items" javaType="HashMap" ofType="com.example.CustomItem" select="selectCustomItems">
</collection>
</association>
</resultMap>
<select id="selectCustomItems" parameterType="map" resultType="com.example.CustomItem">
-- SQL query to return multiple CustomItem rows
</select>
さまざまな反復を通じて、「結果が多すぎます」エラー、その他のエラー、または何も発生しませんでした (マッパー呼び出しから null が返されました) が、必要な結果は得られませんでした。SQL コードはそれ自体で正常に動作し、簡単な select ステートメントで List を要求すると、行と ArrayList が返されます。私が抱えている問題は、ネストされたコレクション オブジェクトが適切に設定されていることです。
私は何度もマニュアルを読み、例を探しましたが、この目的のための正しいマッピング XML を思いつくことができませんでした。誰かが私を助けてくれるか、助けてくれる情報源を教えてくれれば幸いです。
すべての助けを前もって感謝します。