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)查询照片
抱歉啦,存的照片有点大了,所以都看不到了
在这里插入图片描述
结束啦,你们也可以尝试玩一下哦

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

I'm 程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值