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
MyBatisTypeHandler
The interface defines the conversion rules between Java types and JDBC types.
EachTypeHandler
The 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 specialTypeHandler
To handle them.
- For BLOB fields, MyBatis provides
BlobTypeHandler
。 - For CLOB fields, provided
ClobTypeHandler
。
When the query operation returns the BLOB or CLOB field, MyBatis passes theseTypeHandler
Implement classes to process data mapping.
Source code analysis
byBlobTypeHandler
For example, let's see how MyBatis handles BLOB type data.
In MyBatis,BaseTypeHandler
The class has been implementedTypeHandler
Most methods of interfaces and provide default implementations for several basic scenarios.
BlobTypeHandler
Inherited fromBaseTypeHandler<byte[]>
, means it handles conversions mapped from BLOB fields to Java byte array.
public class BlobTypeHandler extends BaseTypeHandler<byte[]> { @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,setNonNullParameter
Method UsePreparedStatement
ofsetBlob
Method sets the value of the BLOB field, andgetNullableResult
Method passedResultSet
ofgetBlob
Method 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:
<!-- Mapper XML Configuration --> <mapper namespace=""> <!-- Query operation --> <select resultType="byte[]" parameterType="int"> SELECT file_data FROM files WHERE id = #{id} </select> <!-- Update operation --> <update parameterType="map"> UPDATE files SET file_data = #{fileData} WHERE id = #{id} </update> </mapper>
In the example above,selectFile
The query will return a BLOB field (file_data
) content, mapped to Javabyte[]
type.updateFile
The update operation demonstrates how to update a byte array into a BLOB field.
Summarize
MyBatis byTypeHandler
The mechanism provides powerful type mapping and conversion capabilities. For large-field data such as BLOB and CLOB, MyBatis is built-inBlobTypeHandler
andClobTypeHandler
Allows 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.