MyBatisPlus写入和读取mysql blob数据

实体类

autoResultMap注解必须开启

blob的对应字段,数据类型必须为byte[],并且增加typeHandler = BlobTypeHandler.class

@TableName(value = "pictures",autoResultMap = true)
@Data
public class Pic {
    @TableField(value = "name")
    private String name;
    @TableField(value = "pic_data",typeHandler = BlobTypeHandler.class)
    private byte[] pic_data;
}

其他的正常写即可,读取的时候也按照byte[]即可

将byte[]写入文件

QueryWrapper resultQueryWrapper = Wrappers.query();
resultQueryWrapper.eq("name", "1234.bmp");
Pic pic = PicMapper.selectOne(resultQueryWrapper);
ByteSink myPic= Files.asByteSink(new File("new1234.bmp"));
myPic.write(pic.getPic_data());

你可能感兴趣的:(mysql,java,java,mysql)