Springboot+Mybatis 保存图片到数据库

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


  • 6
    点赞
  • 51
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值