【问题标题】:Retrieving a BLOB using mybatis?使用 mybatis 检索 BLOB?
【发布时间】:2014-02-17 02:07:36
【问题描述】:

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

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

我尝试将 BLOB 分配给 java.io.InputStream 类型的属性,但这不起作用。错误消息是“No typehandler found for property inputStream”(其中“inputStream”是 InputStream 属性的名称)。

任何人都可以在这里指出正确的方向吗?谢谢。

【问题讨论】:

    标签: java blob inputstream mybatis


    【解决方案1】:

    好吧,这就是我这样做的方法,但是对于 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

    【讨论】:

      【解决方案2】:

      由于 InputStream 也没有预定义的类型处理程序

      1) 您可以通过扩展至 BaseTypeHandler 并覆盖其方法来定义您自己的自定义类型处理程序。我不确定,但我猜你可以返回一个 InputStream 对象。 http://mybatis.github.io/mybatis-3/configuration.html#typeHandlers

      2) 为了处理 clob,我个人使用 DBMS_LOB.SUBSTR(myClobColumn, size)。多亏了 SO,它比 SUBSTR() 快得多。 Performance of SUBSTR on CLOB

      【讨论】:

        【解决方案3】:

        我之前已经遇到过这个问题。您只需要使用“selectByExampleWithBLOBs”。

        databaseStatementMapper.selectByExampleWithBLOBs(selectIn);

        这是我已经遇到过的Mybatis的一个很好的特性:))

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-12-11
          • 1970-01-01
          • 2014-09-25
          • 2011-01-09
          • 1970-01-01
          • 1970-01-01
          • 2013-06-07
          相关资源
          最近更新 更多