Springboot+Mybatis 保存图片到数据库

本文介绍如何使用SpringBoot与MyBatis实现图片的上传与存储功能,通过BaseRelationShipPhoto实体类将图片作为byte[]类型字段保存到Oracle数据库中,并提供相应的Controller与Mapper实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值