简体   繁体   English

使用myBatis从数据库中获取blob作为byte []

[英]Fetching a blob from database as a byte[] using myBatis

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. 我在一个项目中使用spring MyBatis 1.2.0,我有一个查询从Oracle 11g数据库中的BLOB字段获取数据。 I want to retrieve the field as a byte array (byte[]), my Code is: 我想将字段检索为字节数组(byte []),我的代码是:

<select id="getResponse" resultType="_byte[]" parameterType="string">
   select blob_Data from Table where id = #{value,jdbcType=VARCHAR} AND ROWNUM = 1    
</select>

This is giving following error: 这会产生以下错误:

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)

Apart from this, I have also tried using resultMap: 除此之外,我也尝试过使用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>

and also specifying the javaType: 并指定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>

but with no luck, all give the same ClassCastException 但没有运气,所有人都给出了相同的ClassCastException

Could someone please tell me what I am doing wrong? 有人可以告诉我我做错了什么吗?

Try to specify a JDBC type in your result map: 尝试在结果映射中指定JDBC类型:

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

Here an example from Camunda BPM: 这是Camunda BPM的一个例子:

Mapping with result map "resourceResultMap", that contains a bytes property 使用结果映射“resourceResultMap”进行映射,其中包含bytes属性

Entity with bytes (byte[]) field 具有字节(byte [])字段的实体

Edit: 编辑:

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. 它建议使用BINARY作为JDBC类型或使用自定义类型处理程序,如在接受的答案中。

So, I got this to work by making the following changes in my code- 所以,我通过在我的代码中进行以下更改来实现此目的 -

I am using a resultMap and specified both javaType and jdbcType: 我使用的是resultMap并指定了javaType和jdbcType:

<resultMap id="responseMap" type="ResponseMessageModel">
      <result property="blobData" javaType="_byte[]" column="blob_Data" jdbcType="BLOB"/>
</resultMap>

<select id="getResponse" resultMap="responseMap" parameterType="string">
   select blob_Data from table where id = #{value,jdbcType=VARCHAR} AND ROWNUM = 1    
</select>

By doing this, I am able to successfully retrieve the BLOB value as a byte[]. 通过这样做,我能够成功地将BLOB值检索为byte []。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM