温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Mybatis在sqlite中无法读写byte[]类问题的解决办法

发布时间:2020-10-15 16:07:01 来源:脚本之家 阅读:147 作者:icyfox_bupt 栏目:开发技术

开发环境: springboot + mybatis plus

场景:在DAO的bean中有byte[]类时,写入可以成功,但是读取不行。从错误栈中可以看到原因是:sqlite的driver中,JDBC4ResultSet没有实现以下接口:

 public Blob getBlob(int col)
  throws SQLException { throw unused(); }
 public Blob getBlob(String col)
  throws SQLException { throw unused(); }

读写byte[]在JDBC规范中有3种接口:

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

Mybatis Plus默认会选择第3个接口。因此,这里只需要将处理方法切换到前两个接口即可:方法就是更换一个TypeHandler

直接上代码:

@Data
@TableName(autoResultMap = true)
public class Member {

 @TableId
 private String personId;
 private String name;
 private String telephone;
 @TableField(typeHandler = ByteArrayTypeHandler.class)
 private byte[] img;
 private String ext;
 private Integer type;
 private Integer ts;
}

关键点:

  • 添加@TableName(autoResultMap = true)
  • 添加@TableField(typeHandler = ByteArrayTypeHandler.class)

之后就可以正常读写byte[]了

总结

到此这篇关于Mybatis在sqlite中无法读写byte[]类问题的文章就介绍到这了,更多相关Mybatis在sqlite无法读写byte[]类内容请搜索亿速云以前的文章或继续浏览下面的相关文章希望大家以后多多支持亿速云!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI