MyBatis 对数据库的 Blob 字段也进行了支持,它提供了一个 BlobTypeHandler,为了应付更多的场景,它还提供了 ByteArrayTypeHandler,只是它不太常用,这里为读者展示 BlobTypeHandler 的使用方法。首先建一个表。
create table file( id int(12) not null auto_increment, content blob not null, primary key(id));
加粗的代码,使用了 Blob 字段,用于存入文件。然后创建一个 POJO,用于处理这个表,如下所示。
public class TestFile{
long id;
byte[] content;
/** setter and getter **/
}