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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

icyfox_bupt   2020-10-04 我要评论

本文着重给大家讲解了关于Mybatis在sqlite中无法读写byte[]类问题的解决办法,文中通过代码实例讲解的非常细致,对大家的工作和学习具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

开发环境: 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[]了

总结

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们