本文介绍了使用myBatis从数据库中将blob作为字节[]获取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个项目中使用spring MyBatis 1.2.0,在这里我有一个查询,该查询从Oracle 11g数据库中的BLOB字段获取数据.我想将字段检索为字节数组(byte []),我的代码是:

I am using spring MyBatis 1.2.0 in a project, where I have a query that gets data from a BLOB field in an Oracle 11g database. I want to retrieve the field as a byte array (byte[]), my Code is:

<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:

Apart from this, I have also tried using 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:

and also specifying the 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

but with no luck, all give the same ClassCastException

有人可以告诉我我在做什么错吗?

Could someone please tell me what I am doing wrong?

推荐答案

尝试在结果映射中指定JDBC类型:

Try to specify a JDBC type in your result map:

<result property="blobData" column="blob_Data" jdbcType="BLOB"/>

以下是Camunda BPM的示例:

Here an example from Camunda BPM:

映射包含字节属性的结果映射"resourceResultMap"

带字节(byte [])字段的实体

如果它不起作用,请查看以下.建议使用 BINARY 作为JDBC类型,或使用自定义类型处理程序,例如在接受的答案中.

If it is not working, please have a look at following question. It suggests to use BINARY as JDBC type or to use a custom type handler like in the accepted answer.

这篇关于使用myBatis从数据库中将blob作为字节[]获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 09:25