gpt4 book ai didi

java - 使用 mybatis 检索 BLOB?

转载 作者:行者123 更新时间:2023-11-30 11:23:10 26 4
gpt4 key购买 nike

我无法找到使用 mybatis 检索 BLOB 的正确方法。

我发现了一些示例,其中 BLOB 字段被分配给对象中的 byte[] 变量。如果您知道所有的 BLOB 字段都很小并且不介意将它们加载到内存中,我想这是可以的。但是,我有很多大型 BLOB,我更喜欢将它们作为流读取。

我尝试将 BLOB 分配给 java.io.InputStream 类型的属性,但这没有用。错误消息是“未找到属性 inputStream 的类型处理程序”(其中“inputStream”是 InputStream 属性的名称)。

谁能给我指明正确的方向?谢谢。

最佳答案

好吧,我是这样做的,但是对于 byte[],对于 InputStream 应该很简单,但我确实认为你需要保持连接打开(我不知道这是否可以用 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 配置中。可以看到,我使用的是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 .

关于java - 使用 mybatis 检索 BLOB?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21360480/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com