2

我正在尝试使用 mybatis 从 Oracle 数据库中检索 BLOB 列的内容。有一个表 'Demo' 包含一个 BLOB 类型的列 'binfile'。我想选择 BLOB 列并将其显示为字节数组/原始二进制数据。我正在使用 Oracle 瘦 JDBC 驱动程序。

mybatis mapper 中的查询是这样的:

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

如果我这样做,我得到的结果如下所示:

BINFILE: "oracle.sql.BLOB@5d67eb18"

如果我这样做:

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

我显然收到一个错误,说原始变量说“PL/SQL:数字或值错误:原始变量长度太长”,因为图像远远超过 100 kB,因为 SQL 中的 VARCHAR2 变量只能支持 2000 个字节。

有针对这个的解决方法吗?

我想编写一个存储过程,逐块读取 BLOB 列并将输出写入文件。但是该文件将保存在数据库服务器上,我无法检索它。

4

3 回答 3

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();
于 2013-06-12T07:29:25.603 回答
0

您是否尝试将字段映射到 jdbcType=LONGVARBINARY?

于 2013-06-12T10:31:54.690 回答
0

在我的例子中,我必须实现一个自定义 BaseTypeHandler 来支持 Oracle BLOB 转换为 Mybatis 的 byte[]

  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. 添加自定义 BaseTypeHandler 以从 Oracle BLOB类中读取 byte[] :

    @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. 将 type handlers 包添加到 mybatis 配置中。如您所见,我使用的是spring-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 中读取 byte[]

    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 。

于 2016-01-29T17:43:11.873 回答