mybatis-plus处理blob字段

转载自:www.javaman.cn

在 Spring Boot 项目中使用 MyBatis-Plus 处理 longblob 字段时,我们可以按照以下步骤进行操作。假设 longblob 存储的是字符串数据。以下是完整的示例代码:

  1. 添加依赖 :在你的项目的 pom.xml 文件中添加 MyBatis-Plus 的依赖:
xml 复制代码
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>最新版本号</version>
</dependency>
  1. 创建 MyLongBlobTypeHandler 类 :创建一个自定义的类型处理器 MyLongBlobTypeHandler 来处理 longblob 字段的数据。这个处理器会将 longblob 转换为字符串。
java 复制代码
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import java.sql.*;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@Component
public class MyLongBlobTypeHandler extends BaseTypeHandler<String> {

    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, String s, JdbcType jdbcType) throws SQLException {
        preparedStatement.setBytes(i, s.getBytes(StandardCharsets.UTF_8));
    }

    @Override
    public String getNullableResult(ResultSet resultSet, String s) throws SQLException {
        byte[] bytes = resultSet.getBytes(s);
        return new String(bytes, StandardCharsets.UTF_8);
    }

    @Override
    public String getNullableResult(ResultSet resultSet, int i) throws SQLException {
        byte[] bytes = resultSet.getBytes(i);
        return new String(bytes, StandardCharsets.UTF_8);
    }

    @Override
    public String getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        byte[] bytes = callableStatement.getBytes(i);
        return new String(bytes, StandardCharsets.UTF_8);
    }
}

​ 2.定义实体类 :创建一个实体类,用于映射数据库表。在实体类中,使用 @TableField 注解来指定数据库字段和类型处理器。例如下面文章的content内容字段就是longblob字段,通过@TableField注解指定类型处理

java 复制代码
@Data
@TableName("blog_article")
public class Article extends BaseEntity {
    private String name;
    private String url;
    private String tag;
    private Long channelId;
    private String channelName;
    @TableField(value = "content", typeHandler = MyLongBlobTypeHandler.class)
    private String content;
    private Integer orderNum;
    //是否启用,Y启用,N禁用
    private String enabled;
    //浏览数
    private Integer views;
    //description
    private String description;
    //keywords
    private String keywords;
}

​ 3.使用 MyBatis-Plus 正常插入即可:sevice层中正常使用,处理器会默认转成longblob插入数据库

java 复制代码
@Service
public class ArticleService extends ServiceImpl<ArticleMapper, Article> {
    /**
     * 添加文章
     * @param article
     */
    public void add(@NotNull Article article){
        this.save(article);
    }
}
相关推荐
Java小白程序员17 小时前
MyBatis基础到高级实践:全方位指南(中)
数据库·mybatis
山楂树下懒猴子19 小时前
ChatAI项目-ChatGPT-SDK组件工程
人工智能·chatgpt·junit·https·log4j·intellij-idea·mybatis
Mr_hwt_1231 天前
基于mybatis-plus动态数据源实现mysql集群读写分离和从库负载均衡教程(详细案例)
数据库·spring boot·mysql·mybatis·mysql集群
Z_z在努力2 天前
【杂类】Spring 自动装配原理
java·spring·mybatis
little_xianzhong2 天前
关于对逾期提醒的定时任务~改进完善
java·数据库·spring boot·spring·mybatis
MadPrinter2 天前
SpringBoot学习日记 Day11:博客系统核心功能深度开发
java·spring boot·后端·学习·spring·mybatis
奔跑吧邓邓子2 天前
【Java实战㉟】Spring Boot与MyBatis:数据库交互的进阶之旅
java·spring boot·实战·mybatis·数据库交互
lunzi_fly2 天前
【源码解读之 Mybatis】【基础篇】-- 第1篇:MyBatis 整体架构设计
java·mybatis
摸鱼仙人~2 天前
深入理解 MyBatis-Plus 的 `BaseMapper`
java·开发语言·mybatis
隔壁阿布都2 天前
spring boot + mybatis 使用线程池异步修改数据库数据
数据库·spring boot·mybatis