You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Data R2DBC操作MySQL Blob类型字段的实现方法咨询

Spring Data R2DBC 处理 MySQL Blob 字段解决方案

1. 依赖校验

首先确认你已引入适配Spring Boot 2.5.4版本的R2DBC MySQL驱动,Maven依赖示例如下:

<dependency>
    <groupId>dev.miku</groupId>
    <artifactId>r2dbc-mysql</artifactId>
    <version>0.8.2.RELEASE</version>
</dependency>

2. 修复Repository定义

你当前的Repository定义存在语法错误,ReactiveCrudRepository是接口,需要改为接口继承:

import org.springframework.data.repository.reactive.ReactiveCrudRepository;

public interface UserNoteRepository extends ReactiveCrudRepository<UserNote, Integer> {
}

3. Blob字段映射方案

方案一:手动转换(轻量无额外配置)

直接用ByteBuffer作为实体类的Blob对应字段,业务代码中手动完成字节与目标类型的转换:

import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Table;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

@Table("user_note")
public class UserNote {
    @Id
    private final Integer id;
    private final String name;
    private final ByteBuffer description;

    public UserNote(Integer id, String name, ByteBuffer description) {
        this.id = id;
        this.name = name;
        this.description = description;
    }

    // 便捷方法:将Blob内容转为UTF-8文本
    public String getDescriptionText() {
        return description == null ? null : StandardCharsets.UTF_8.decode(description).toString();
    }

    // 静态工具方法:将文本转为可入库的ByteBuffer
    public static ByteBuffer toBlob(String text) {
        return text == null ? null : StandardCharsets.UTF_8.encode(text);
    }

    // 其余Getter方法省略
}

读写示例

// 写操作:构造实体入库
public Mono<UserNote> saveNote(String name, String descText) {
    UserNote note = new UserNote(null, name, UserNote.toBlob(descText));
    return userNoteRepository.save(note);
}

// 读操作:取出数据转为文本返回
public Mono<String> getDescById(Integer id) {
    return userNoteRepository.findById(id)
            .map(UserNote::getDescriptionText);
}

方案二:自动转换(无需手动编解码)

通过注册自定义转换器,让Spring Data R2DBC自动完成String与Blob的类型映射,实体类可直接使用String类型的description字段。

步骤1:编写转换器

import io.r2dbc.spi.Blob;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.ReadingConverter;
import org.springframework.data.convert.WritingConverter;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

// 读转换器:数据库Blob转String
@ReadingConverter
public enum BlobToStringConverter implements Converter<Blob, String> {
    INSTANCE;
    @Override
    public String convert(Blob source) {
        return Flux.from(source.stream())
                .reduce(ByteBuffer::concat)
                .map(buffer -> StandardCharsets.UTF_8.decode(buffer).toString())
                .block();
    }
}

// 写转换器:String转数据库Blob
@WritingConverter
public enum StringToBlobConverter implements Converter<String, Blob> {
    INSTANCE;
    @Override
    public Blob convert(String source) {
        return Blob.from(Mono.just(StandardCharsets.UTF_8.encode(source)));
    }
}

步骤2:注册转换器到R2DBC配置

import org.springframework.context.annotation.Configuration;
import org.springframework.data.r2dbc.config.AbstractR2dbcConfiguration;
import java.util.Arrays;
import java.util.List;

@Configuration
public class R2dbcCustomConfig extends AbstractR2dbcConfiguration {
    // 此处需注入你配置的R2DBC ConnectionFactory,省略注入代码

    @Override
    protected List<Object> getCustomConverters() {
        return Arrays.asList(
                BlobToStringConverter.INSTANCE,
                StringToBlobConverter.INSTANCE
        );
    }
}

配置完成后,你原始的String类型description字段的实体类即可直接正常使用,无需额外转换逻辑。

注意:如果Blob字段存储的是非文本类二进制数据(如图片、文件),无需转String,直接使用ByteBuffer或io.r2dbc.spi.Blob类型处理字节流即可。

内容的提问来源于stack exchange,提问作者user1578872

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 12:18:03