一、SQL表结构dataimage
字段 | 类型 |
---|---|
ID | varchar |
Image | mediumblob |
在MySQL数据库中,MEDIUMBLOB是一种数据类型,用于存储二进制大对象(Binary Large Object),最大长度为16MB。
二、mapper.xml
<resultMap type="DataImage" id="DataImageResult">
<result property="ID" column="ID" />
<result property="Image" column="Image" jdbcType="BLOB" javaType="byte[]" typeHandler="org.apache.ibatis.type.BlobTypeHandler"/>
</resultMap>
在mapper里面定义resultMap的result column,mybatis中直接对应byte[]即可,mybatis官方提供了BlobTypeHandler解析方法。
在Mybatis的官方文档中说明了,框架内置的TypeHandler类型。请参见链接:http://mybatis.github.io/mybatis-3/zh/configuration.html#typeHandlers
三、显示给前端
在使用的时候将byte[]转为String就OK了
byte[] imageBytes = dataImage.getImage();
return Base64.getEncoder().encodeToString(imageBytes);