mysql使用blob存储图片,java实体类private byte[] image;Dao Service Controller层 前端代码如下,为什么页面显示的是十六进制数据,怎么才可以正确显示图片?Java 的实体类是要使用String image吗?
mysql
DAO层
public interface CommodityDao {
@Select("select * from commodity")
List<Commodity> selectAll();
}
Service层
public class CommodityServiceImpl implements CommodityService {
@Autowired
private CommodityDao commodityDao;
@Override
public List<Commodity> selectAll() {
return commodityDao.selectAll();
}
}
Controller层
@RestController
@RequestMapping("/merchant")
public class CommodityController {
@Autowired
private CommodityService commodityService;
@GetMapping("/selectAll")
public Result selectAll(){
List<Commodity> commodities = commodityService.selectAll();
int code = commodities != null ? Code.SELECT_OK:Code.SELECT_ERR;
String msg = commodities != null ? "数据查询成功":"数据查询失败,请重试";
return new Result(code,commodities,msg);
}
}
前端
<el-table-column
prop="image"
label="图片">
<el-image
style="width: 100px; height: 100px"
:src="url"
:fit="fit">
</el-image>
</el-table-column>
selectAll(){
var _this = this;
axios({
method: "get",
url: "http://localhost:8080/merchant/selectAll"
}).then(function (resp) {
_this.commodity = resp.data.data;
let blob = new window.Blob([resp.data.data.image]);
let url = window.URL.createObjectURL(blob);
_this.commodity.image.url = url;
})
}
页面显示结果