Hibernate 存入image 到数据库

 

/**
 * 头像
 */
private byte[] headIcon;

@Lob
@Column(name = "PERSON_IMAGE")
public byte[] getHeadIcon() {
    return headIcon;
}

public void setHeadIcon(byte[] headIcon) {
    this.headIcon = headIcon;
}

//读取图片的地方
private ByteArrayOutputStream headImg;

@Transient
public ByteArrayOutputStream getHeadImg() {
    ByteArrayOutputStream baos = null;
    if(getHeadIcon()!=null){
        try {
            baos = new ByteArrayOutputStream();
            baos.write(Base64.encodeBase64(getHeadIcon()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return baos;
}
<!-- 图片显示的地方 -->
<span class="headicon-upload" id="headicon-show" style="background: url(data:image/png;base64,${user.headImg});background-size: cover;">
    <span class="btn-headicon-upload" id="btn-headicon-upload">点击上传</span>
</span>