MybatisPlus+SQLite 中无法读BLOB类型数据问题

一、问题描述

对 BLOB 类型操作时,写入可以成功,但是读取失败。

@TableName
public class Demo {

    private byte[] data;

}

执行读取操作时报错如下:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.dao.InvalidDataAccessApiUsageException: Error attempting to get column 'data' from result set.  Cause: java.sql.SQLFeatureNotSupportedException: not implemented by SQLite JDBC driver
; not implemented by SQLite JDBC driver] with root cause

java.sql.SQLFeatureNotSupportedException: not implemented by SQLite JDBC driver

二、原因

对于 BLOB 的读取,在 JDBC 规范中有 3 种接口:

  • InputStream getBinaryStream(int col)
  • byte[] getBytes(int col)
  • Blob getBlob(int col)

而 Mybatis Plus 默认会选择第 3 个接口。
因此只需要通过设置 TypeHandler 修改处理方式即可。

三、解决方法

  1. autoResultMap 属性设置为 true
  2. 添加 @TableField(typeHandler = ByteArrayTypeHandler.class)
@TableName(autoResultMap = true)
public class Demo {

    @TableField(typeHandler = ByteArrayTypeHandler.class)
    private byte[] data;

}

而对于用 XML 实现的 SQL 需要通过以下方式设置 typeHandler:

<resultMap id="DemoMap" type="Demo" >
    <result column="data" property="data" typeHandler="org.apache.ibatis.type.ByteArrayTypeHandler"/>
</resultMap>

<select id="search" resultMap="ResourceMap">
	<!-- sql -->
</select>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值