1. 程式人生 > 程式設計 >Mybatis在sqlite中無法讀寫byte[]類問題的解決辦法

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[]了

總結

到此這篇關於Mybatis在sqlite中無法讀寫byte[]類問題的文章就介紹到這了,更多相關Mybatis在sqlite無法讀寫byte[]類內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!