我在一个项目中使用 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 的示例:
Mapping with result map "resourceResultMap", that contains a bytes property
Entity with bytes (byte[]) field
编辑:
如果不起作用,请查看以下question 。它建议使用 BINARY 作为 JDBC 类型或使用自定义类型处理程序,如已接受的答案中所示。
关于spring - 使用 myBatis 从数据库中以 byte[] 的形式获取 blob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28670078/