Mybatis在sqlite中无法读写byte[]类问题的解决办法
开发环境:springboot+mybatisplus
场景:在DAO的bean中有byte[]类时,写入可以成功,但是读取不行。从错误栈中可以看到原因是:sqlite的driver中,JDBC4ResultSet没有实现以下接口:
publicBlobgetBlob(intcol) throwsSQLException{throwunused();} publicBlobgetBlob(Stringcol) throwsSQLException{throwunused();}
读写byte[]在JDBC规范中有3种接口:
- InputStreamgetBinaryStream(intcol)
- byte[]getBytes(intcol)
- BlobgetBlob(intcol)
MybatisPlus默认会选择第3个接口。因此,这里只需要将处理方法切换到前两个接口即可:方法就是更换一个TypeHandler
直接上代码:
@Data @TableName(autoResultMap=true) publicclassMember{ @TableId privateStringpersonId; privateStringname; privateStringtelephone; @TableField(typeHandler=ByteArrayTypeHandler.class) privatebyte[]img; privateStringext; privateIntegertype; privateIntegerts; }
关键点:
- 添加@TableName(autoResultMap=true)
- 添加@TableField(typeHandler=ByteArrayTypeHandler.class)
之后就可以正常读写byte[]了
总结
到此这篇关于Mybatis在sqlite中无法读写byte[]类问题的文章就介绍到这了,更多相关Mybatis在sqlite无法读写byte[]类内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!