在做项目的时候,图片一般是存在单独的服务器中,数据库中只存url地址,这样做事比较好的,减轻了数据库的压力,存取图片的时候也简单。然而,也不排除直接将二进制文件存到数据库中的情况,这种做法,无论是存还是取都不方便,也增加了数据库的压力,所以尽量避免这种做法。
- JavaBean
public class CustomerPMProduct {
private String name;//产品名称
private Float weightAu;//含金量
private String standard;//规格
private Float remainAmount;//剩余量
private byte[] thumbnail;//缩略图,这个就是二进制字段
public String getStandard() {
return standard;
}
public void setStandard(String standard) {
this.standard = standard;
}
public byte[] getThumbnail() {
return thumbnail;
}
public void setThumbnail(byte[] thumbnail) {
this.thumbnail = thumbnail;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Float getWeightAu() {
return weightAu;
}
public void setWeightAu(Float weightAu) {
this.weightAu = weightAu;
}
public Float getRemainAmount() {
return remainAmount;
}
public void setRemainAmount(Float remainAmount) {
this.remainAmount = remainAmount;
}
}
- mapper.java接口
@Component
public interface CustomerPMProductMapper {
CustomerPMProduct selectPMProduct();
}
- mapper.xml文件
<select id="selectPMProduct" resultType="com.yihuisoft.testprojectbiz.entity.CustomerPMProduct">
SELECT name,
weight_au weightAu,
standard,
REMAIN_AMOUNT remainAmount,
THUMBNAIL01 thumbnail
FROM
(SELECT name,
weight_au,
standard,
REMAIN_AMOUNT ,
THUMBNAIL01
FROM PM_PRODUCT
WHERE exp_yield IS NOT NULL
ORDER BY exp_yield DESC
)
WHERE rownum=1
</select>
- service
public OutDTO selectPMProduct(InDTO inDTO){
OutDTO outDTO = new OutDTO();
CustomerPMProduct pmProduct = wealthSuccessionMapper.selectPMProduct();
JSONObject jsonObject = new JSONObject();
jsonObject.put("data",pmProduct);
outDTO.setData(jsonObject);
return outDTO;
}
- 前端显示
- image标签
<img id="img1" src="" height="128px" width="200px">
- js
document.getElementById("img1").src = "data:image/png;base64,"+this.pmProduct.thumbnail;