系统城装机大师 - 固镇县祥瑞电脑科技销售部宣传站!

当前位置:首页 > 网络编程 > 其它综合 > 详细页面

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

时间:2020-10-06来源:www.pcxitongcheng.com作者:电脑系统城

发环境: springboot + mybatis plus

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

?
1
2
3
4
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

直接上代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@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[]类问题的文章就介绍到这了,

分享到:

相关信息

  • C++ AVLTree高度平衡的二叉搜索树深入分析

    一、AVL树的概念 二、AVL树节点的定义 三、AVL树的插入 四、AVL树的旋转 1.左单旋 2.右单旋 3.左右双旋 4.右左双旋 五、进行验证 六、AVLTree的性能...

    2023-03-09

  • idea构建web项目的超级详细教程

    1、idea构建web项目 1、新建一个空项目 2、新建java模块,名为webDemo1 3、选择webDemo1右键,选择Add Framework Support 4、在WEB-INF下新建文件夹classes和lib 5、打开项目结构(Project Structure) 6、项目配置 7、模...

    2023-03-09

系统教程栏目

栏目热门教程

人气教程排行

站长推荐

热门系统下载