Springboot+Mybatis 保存图片到数据库
哼!
实体类,图片用byte[]
public class BaseRelationShipPhoto {
private Long id;
private Long relationshipId;
private String employeeId;
private byte[] photo;
}
controller
@PostMapping (value = "saveimg")
public Object searchMember( HttpServletRequest request,
@RequestParam("relationid")Long relationid){
GenericResult result = new GenericResult();
// 得到文件
MultipartFile file = ((MultipartHttpServletRequest) request).getFile("file");
FilterRelationCondition filterRelationCondition = new FilterRelationCondition();
filterRelationCondition.setId(relationid);
Relation relation = relationMapper.filterRelation(filterRelationCondition).get(0);
try {
byte[] data;
data = file.getBytes();
BaseRelationShipPhoto photo = new BaseRelationShipPhoto();
photo.setEmployeeId(relation.getEmployeeId().toString());
photo.setRelationshipId(relation.getId());
photo.setPhoto(data);
relationMapper.insertRelationPhoto(photo);
}catch (Exception e){
e.printStackTrace();
}
result.success("上传成功");
return result;
}
Mapper :
放两个,插入和查询的方法
//增加
@Insert({"insert into BASE_RELATIONSHIP_PHOTO (" +
"id,relationship_id,employee_id,photo) values(" +
" S_DEPART.Nextval,#{e.relationshipId},#{e.employeeId,jdbcType=VARCHAR},#{e.photo,jdbcType=BLOB} )"})
void insertRelationPhoto(@Param("e") BaseRelationShipPhoto photo);
@Select(" select * from BASE_RELATIONSHIP_PHOTO where relationship_id = #{e} ")
@Result(property = "photo",column = "photo", jdbcType = JdbcType.BLOB)
List<BaseRelationShipPhoto> fiterPhoto(@Param("e") Long id);
奥对了,数据库用的是oracle