springboot操作数据库表的blob字段

太艰难了,终于把它搞定了!

1,实体类

public class TUser {
    private String username;
    private String password;
    private byte[] headimage;

    public TUser() {
    }

    public TUser(String username, String password, byte[] headimage) {
        this.username = username;
        this.password = password;
        this.headimage = headimage;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public byte[] getHeadimage() {
        return headimage;
    }

    public void setHeadimage(byte[] headimage) {
        this.headimage = headimage;
    }
}

2,mapper接口

import com.example.play.model.TUser;
import org.springframework.stereotype.Repository;

@Repository
public interface TUserMapper {
    TUser getByName(String username);

    void addTUser(TUser user);
}

3,service接口

import com.example.play.model.TUser;

public interface TUserService {

    TUser getByName(String username);

    void addTUser(TUser user);
}

4,Impl实现类

import com.example.play.mapper.TUserMapper;
import com.example.play.model.TUser;
import com.example.play.service.TUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TUserServiceImpl implements TUserService {

    @Autowired
    TUserMapper tUserMapper;

    @Override
    public TUser getByName(String username) {
        return tUserMapper.getByName(username);
    }

    @Override
    public void addTUser(TUser user) {
        tUserMapper.addTUser(user);
    }
}

5,xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.play.mapper.TUserMapper">
    <resultMap id="X" type="com.example.play.model.TUser">
        <result property="username" column="username" jdbcType="VARBINARY"/>
        <result property="password" column="password" jdbcType="VARBINARY"/>
        <result property="headimage" column="headimage" jdbcType="BLOB"/>
    </resultMap>

    <select id="getByName" parameterType="string" resultMap="X">
        select * from t_user where username=#{username}
    </select>

    <insert id="addTUser" parameterType="com.example.play.model.TUser">
        insert into t_user value (#{username},#{password},#{headimage})
    </insert>
</mapper>

6,最重要的controller层

@RestController
public class TUserController {
    @Autowired
    TUserService userService;

    @PostMapping("/getByName")
    @ApiOperation("获取头像")
    public ResponseEntity<byte[]> getByName(String username){
        TUser user = userService.getByName(username);
        byte[] imageContent = user.getHeadimage();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.IMAGE_PNG);
        return new ResponseEntity<>(imageContent, headers, HttpStatus.OK);
    }
    
    @PostMapping("/addTUser")
    @ApiOperation("增加成员")
    public void addTUser(String username, String password, @RequestParam MultipartFile file){
        InputStream ins = null;
        byte[] data=new byte[1024];
        try {
            ins = file.getInputStream();
            byte[] buffer=new byte[1024];
            int len=0;
            ByteArrayOutputStream bos=new ByteArrayOutputStream();
            while((len=ins.read(buffer))!=-1){
                bos.write(buffer,0,len);
            }
            bos.flush();
            data = bos.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
        TUser user = new TUser(username,password,data);
        userService.addTUser(user);
    }
}

7,效果展示啦
1)存储照片
在这里插入图片描述
2)查询照片
抱歉啦,存的照片有点大了,所以都看不到了
在这里插入图片描述
结束啦,你们也可以尝试玩一下哦

  • 1
    点赞
  • 1
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 1024 设计师:白松林 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值