MyBatisはCLOB型フィールドをどのように処理しますか?
MyBatisでは、CLOBフィールドをResultMapとTypeHandlerを使って処理します。
まず、ResultMapでCLOB型のフィールドの処理方法を定義する必要があります。ResultMapはタグで定義し、各フィールドのマッピング関係はタグで定義します。CLOB型のフィールドの場合は、タグでそのハンドラを指定します。
例えば、CLOBフィールドを含むエンティティクラスUserがあって、descriptionという名前のCLOBフィールドがある場合。
public class User {
private Long id;
private String name;
private String description;
// 省略getter和setter方法
}
MyBatisのマッパーXMLファイルでは、CLOBフィールドを処理するためにResultMapを定義できます。
<resultMap id="userResultMap" type="User">
<id property="id" column="id" />
<result property="name" column="name" />
<result property="description" column="description" jdbcType="CLOB">
<typeHandler handler="org.apache.ibatis.type.ClobTypeHandler" />
</result>
</resultMap>
上記の例では、タグは、CLOB フィールドのハンドラを org.apache.ibatis.type.ClobTypeHandler に指定しています
そして、定義した ResultMap を検索クエリで使用します
<select id="getUser" resultMap="userResultMap">
SELECT id, name, description
FROM user
WHERE id = #{id}
</select>
MyBatisでは、クエリ結果のCLOBフィールドは自動的にJavaオブジェクトに変換され、CLOBフィールドのデータを問題なく格納、アクセスできます。