我在一个项目中使用 spring MyBatis 1.2.0,其中有一个查询从 Oracle 11g 数据库中的 BLOB 字段获取数据。我想以字节数组(byte[])的形式检索该字段,我的代码是:
<select id="getResponse" resultType="_byte[]" parameterType="string">
select blob_Data from Table where id = #{value,jdbcType=VARCHAR} AND ROWNUM = 1
</select>
这给出了以下错误:
java.lang.ClassCastException: [B incompatible with [Ljava.lang.Object;
at org.apache.ibatis.binding.MapperMethod.convertToArray(MapperMethod.java:136)
at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:119)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:58)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:43)
除此之外,我还尝试过使用 resultMap:
<resultMap id="responseMap" type="ResponseMessageModel">
<result property="blobData" column="blob_Data"/>
</resultMap>
<select id="getResponse" resultMap="responseMap" parameterType="string">
select blob_Data from table where id = #{value,jdbcType=VARCHAR} AND ROWNUM = 1
</select>
并指定 javaType:
<resultMap id="responseMap" type="ResponseMessageModel">
<result property="blobData" javaType="_byte[]" column="blob_Data"/>
</resultMap>
<select id="getResponse" resultMap="responseMap" parameterType="string">
select blob_Data from table where id = #{value,jdbcType=VARCHAR} AND ROWNUM = 1
</select>
但不幸的是,所有人都给出了相同的 ClassCastException
有人可以告诉我我做错了什么吗?
尝试在结果映射中指定 JDBC 类型:
<result property="blobData" column="blob_Data" jdbcType="BLOB"/>
以下是 Camunda BPM 的示例:
使用结果映射“resourceResultMap”进行映射,其中包含 bytes 属性 https://github.com/camunda/camunda-bpm-platform/blob/master/engine/src/main/resources/org/camunda/bpm/engine/impl/mapping/entity/Resource.xml
具有字节 (byte[]) 字段的实体 https://github.com/camunda/camunda-bpm-platform/blob/master/engine/src/main/java/org/camunda/bpm/engine/impl/persistence/entity/ResourceEntity.java
Edit:
如果还不行请看下面question https://stackoverflow.com/questions/12059872/how-to-select-a-blob-column-from-database-using-ibatis。它建议使用BINARY作为 JDBC 类型或使用自定义类型处理程序(如接受的答案中所示)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)