SoFunction
Updated on 2025-04-14

MyBatis handles large fields or BLOB or CLOB types of data

MyBatis handles large fields or BLOB or CLOB type data

When processing large fields (such as BLOB, CLOB type data) in MyBatis, the framework provides a set of mechanisms to deal with this type of special data type.

Understanding this process requires deeper into MyBatis' Type Handler mechanism, which is the core component of MyBatis converting Java types and JDBC types when performing result set mapping and preprocessing statements.

Type Handler mechanism

MyBatisTypeHandlerThe interface defines the conversion rules between Java types and JDBC types.

EachTypeHandlerThe implementation class is responsible for the mapping relationship between a Java type and a JDBC type.

For BLOB and CLOB type data, MyBatis has built-in specialTypeHandlerTo handle them.

  • For BLOB fields, MyBatis providesBlobTypeHandler
  • For CLOB fields, providedClobTypeHandler

When the query operation returns the BLOB or CLOB field, MyBatis passes theseTypeHandlerImplement classes to process data mapping.

Source code analysis

byBlobTypeHandlerFor example, let's see how MyBatis handles BLOB type data.

In MyBatis,BaseTypeHandlerThe class has been implementedTypeHandlerMost methods of interfaces and provide default implementations for several basic scenarios.

BlobTypeHandlerInherited fromBaseTypeHandler<byte[]>, means it handles conversions mapped from BLOB fields to Java byte array.

public class BlobTypeHandler extends BaseTypeHandler&lt;byte[]&gt; {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, byte[] parameter, JdbcType jdbcType) throws SQLException {
        // Use the setBlob method of PreparedStatement to set parameters        (i, new ByteArrayInputStream(parameter));
    }

    @Override
    public byte[] getNullableResult(ResultSet rs, String columnName) throws SQLException {
        // Use the getBlob method of ResultSet to get the BLOB data and convert it to a byte array        Blob blob = (columnName);
        return blob == null ? null : (1, (int) ());
    }

    //Omit other getNullableResult methods implementations...}

In the above code,setNonNullParameterMethod UsePreparedStatementofsetBlobMethod sets the value of the BLOB field, andgetNullableResultMethod passedResultSetofgetBlobMethod takes BLOB data and converts it into a byte array.

Code Demo

Suppose you have a database table containing BLOB type fields, you can use MyBatis to query and update the BLOB field as follows:

&lt;!-- Mapper XML Configuration --&gt;
&lt;mapper namespace=""&gt;

    &lt;!-- Query operation --&gt;
    &lt;select  resultType="byte[]" parameterType="int"&gt;
        SELECT file_data FROM files WHERE id = #{id}
    &lt;/select&gt;

    &lt;!-- Update operation --&gt;
    &lt;update  parameterType="map"&gt;
        UPDATE files SET file_data = #{fileData} WHERE id = #{id}
    &lt;/update&gt;

&lt;/mapper&gt;

In the example above,selectFileThe query will return a BLOB field (file_data) content, mapped to Javabyte[]type.updateFileThe update operation demonstrates how to update a byte array into a BLOB field.

Summarize

MyBatis byTypeHandlerThe mechanism provides powerful type mapping and conversion capabilities. For large-field data such as BLOB and CLOB, MyBatis is built-inBlobTypeHandlerandClobTypeHandlerAllows developers to conveniently map large field data in databases to byte arrays or strings in Java. This mechanism simplifies the complexity of processing large-field data, allowing developers to focus more on the implementation of business logic.

The above is personal experience. I hope you can give you a reference and I hope you can support me more.