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

  • 8
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
假设我们有一个名为`MyTable`的,其中包含一个名为`myBlob`的`BLOB`字段,包含一些二进制数据。我们可以使用Spring Boot JPA来查询该,并将`myBlob`字段解析为字节数组,并将其作为JSON格式返回给前端。 首先,我们需要定义一个实体类来示`MyTable`: ``` @Entity @Table(name = "MyTable") public class MyTable { @Id private Long id; @Lob private byte[] myBlob; // getters and setters } ``` 注意`myBlob`字段使用了`@Lob`注解,示该字段是一个大型对象(BLOB或CLOB)。这将告诉JPA将该字段存储为二进制数据。 然后,我们需要创建一个Spring Boot Repository来查询`MyTable`: ``` @Repository public interface MyTableRepository extends JpaRepository<MyTable, Long> { @Query("select t.myBlob from MyTable t where t.id = :id") byte[] findMyBlobById(@Param("id") Long id); } ``` 这个Repository定义了一个名为`findMyBlobById`的查询方法,它将返回`myBlob`字段的字节数组,根据`id`参数进行查询。 最后,我们需要创建一个Controller来处理请求并返回JSON格式的字节数组: ``` @RestController @RequestMapping("/mytable") public class MyTableController { @Autowired private MyTableRepository myTableRepository; @GetMapping("/{id}") public ResponseEntity<byte[]> getMyBlob(@PathVariable Long id) { byte[] myBlob = myTableRepository.findMyBlobById(id); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); return new ResponseEntity<>(myBlob, headers, HttpStatus.OK); } } ``` 在这个Controller中,我们注入了`MyTableRepository`,并创建了一个`getMyBlob`方法来处理请求。该方法使用`@GetMapping`注解,示它将处理HTTP GET请求,并使用`{id}`路径变量来指定要查询的记录的ID。 在`getMyBlob`方法中,我们调用`MyTableRepository`的`findMyBlobById`方法来查询`myBlob`字段的字节数组。然后,我们创建了一个`HttpHeaders`对象,并将`Content-Type`设置为`application/json`。最后,我们使用`ResponseEntity`将字节数组作为JSON格式返回给前端。 现在,当我们访问`/mytable/{id}`时,将返回一个由`myBlob`字段解析而来的JSON格式的字节数组。注意,这种方法适用于任何类型的二进制数据,而不仅仅是JSON格式的数据。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

I'm 程序员

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

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

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

打赏作者

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

抵扣说明:

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

余额充值