2013-06-11 61 views
2

我試圖從Oracle數據庫使用mybatis檢索BLOB列的內容。有一個表'Demo'包含BLOB類型的列'binfile'。我想選擇BLOB列並將其顯示爲字節數組/原始二進制數據。我正在使用Oracle瘦JDBC驅動程序。使用mybatis從Oracle數據庫中選擇BLOB列

在MyBatis的映射器查詢看起來是這樣的:

<mapper namespace="Oracle" > 
... 
<select id="SelectBinary" resultType="hashmap"> 
    SELECT binfile from mpdemo.Demo 
    </select> 
</mapper> 

如果我這樣做,結果我得到這個樣子的:

BINFILE: "[email protected]" 

如果我這樣做:

<select id="SelectBinaryDup" resultType="hashmap"> 
    SELECT utl_raw.cast_to_varchar2(dbms_lob.substr(binfile)) from mpdemo.Demo 
</select> 

我明顯得到一個錯誤,說原始變量說'PL/SQL:數值或數值錯誤:原始變量長度太lon g',因爲圖像遠遠超過100 kB,因爲SQL中的VARCHAR2變量只能支持2000字節。

有沒有解決方案?

我想寫一個存儲過程,它通過塊讀取BLOB列並將輸出寫入文件。但該文件將保存在數據庫服務器上,我無法檢索該文件。

回答

2

您可以直接使用BLOB,做import oracle.sql.BLOB;

例子:

BLOB blob = (BLOB)map.get("binfile"); 

//one way: as array 
byte[] bytes = blob.getBytes(1L, (int)blob.length()); 
System.out.println(new String(bytes)); //use for text data 
System.out.println(Arrays.toString(bytes)); 

//another way: as stream 
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("data.bin")); 
InputStream is = blob.binaryStreamValue(); 
int b = -1; 
while ((b = is.read()) != -1) { 
    bos.write(b); 
} 
bos.close(); 
+0

工程就像一個魅力。謝謝! – codewarrior

0

您是否嘗試過的領域映射到jdbcType爲LONGVARBINARY?

0

在我的情況下,我不得不實施custom BaseTypeHandler to support Oracle BLOB conversion to byte[] for Mybatis

  1. 將Oracle JDBC驅動程序添加到您的項目中,您也需要mybatis依賴關係。如果您正在使用Maven:

    <dependency> 
        <groupId>com.oracle</groupId> 
        <artifactId>ojdbc14</artifactId> 
        <version>10.2.0.3.0</version> 
    </dependency> 
    <dependency> 
        <groupId>org.mybatis</groupId> 
        <artifactId>mybatis-spring</artifactId> 
        <version>1.2.1</version> 
    </dependency> 
    <dependency> 
        <groupId>org.mybatis</groupId> 
        <artifactId>mybatis</artifactId> 
        <version>3.2.3</version> 
    </dependency> 
    
  2. 添加custom BaseTypeHandler for reading byte[] from Oracle BLOB類:

    @MappedTypes(byte[].class) 
    public class OracleBlobTypeHandler extends BaseTypeHandler<byte[]> { 
        @Override 
        public void setNonNullParameter(PreparedStatement preparedStatement, int i, byte[] bytes, JdbcType jdbcType) throws SQLException { 
         // see setBlobAsBytes method from https://jira.spring.io/secure/attachment/11851/OracleLobHandler.java 
         try { 
          if (bytes != null) { 
           //prepareLob 
           BLOB blob = BLOB.createTemporary(preparedStatement.getConnection(), true, BLOB.DURATION_SESSION); 
    
           //callback.populateLob 
           OutputStream os = blob.getBinaryOutputStream(); 
           try { 
            os.write(bytes); 
           } catch (Exception e) { 
            throw new SQLException(e); 
           } finally { 
            try { 
             os.close(); 
            } catch (Exception e) { 
             e.printStackTrace();//ignore 
            } 
           } 
           preparedStatement.setBlob(i, blob); 
          } else { 
           preparedStatement.setBlob(i, (Blob) null); 
          } 
         } catch (Exception e) { 
          throw new SQLException(e); 
         } 
        } 
    
        /** see getBlobAsBytes method from https://jira.spring.io/secure/attachment/11851/OracleLobHandler.java */ 
        private byte[] getBlobAsBytes(BLOB blob) throws SQLException { 
    
         //initializeResourcesBeforeRead 
         if(!blob.isTemporary()) { 
          blob.open(BLOB.MODE_READONLY); 
         } 
    
         //read 
         byte[] bytes = blob.getBytes(1L, (int)blob.length()); 
    
         //releaseResourcesAfterRead 
         if(blob.isTemporary()) { 
          blob.freeTemporary(); 
         } else if(blob.isOpen()) { 
          blob.close(); 
         } 
    
         return bytes; 
        } 
    
        @Override 
        public byte[] getNullableResult(ResultSet resultSet, String columnName) throws SQLException { 
         try { 
          //use a custom oracle.sql.BLOB 
          BLOB blob = (BLOB) resultSet.getBlob(columnName); 
          return getBlobAsBytes(blob); 
         } catch (Exception e) { 
          throw new SQLException(e); 
         } 
        } 
    
        @Override 
        public byte[] getNullableResult(ResultSet resultSet, int i) throws SQLException { 
         try { 
          //use a custom oracle.sql.BLOB 
          BLOB blob = (BLOB) resultSet.getBlob(i); 
          return getBlobAsBytes(blob); 
         } catch (Exception e) { 
          throw new SQLException(e); 
         } 
        } 
    
        @Override 
        public byte[] getNullableResult(CallableStatement callableStatement, int i) throws SQLException { 
         try { 
          //use a custom oracle.sql.BLOB 
          BLOB blob = (BLOB) callableStatement.getBlob(i); 
          return getBlobAsBytes(blob); 
         } catch (Exception e) { 
          throw new SQLException(e); 
         } 
        } 
    } 
    
  3. 添加類型處理器封裝MyBatis的配置。正如你所看到的,我使用的彈簧的MyBatis:

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
        <property name="dataSource" ref="dataSource" /> 
        <property name="typeHandlersPackage" value="package.where.customhandler.is" /> 
    </bean> 
    
  4. ,然後你可以從MyBatis的閱讀從Oracle BLOB的字節[]:

    public class Bean { 
        private byte[] file; 
    } 
    
    interface class Dao { 
        @Select("select file from some_table where id=#{id}") 
        Bean getBean(@Param("id") String id); 
    } 
    

我希望這將有所幫助。 這是一個很好的回答:https://stackoverflow.com/a/27522590/2692914

相關問題