1. 程式人生 > >MyBatis 插入空值時,需要指定JdbcType

MyBatis 插入空值時,需要指定JdbcType

看一看 org.apache.ibatis.type.BaseTypeHandler

@Override
  public void setParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException {
    if (parameter == null) {
      if (jdbcType == null) {
        throw new TypeException("JDBC requires that the JdbcType must be specified for all nullable parameters."
); } try { ps.setNull(i, jdbcType.TYPE_CODE); } catch (SQLException e) { throw new TypeException("Error setting null for parameter #" + i + " with JdbcType " + jdbcType + " . " + "Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. "
+ "Cause: " + e, e); } } else { try { setNonNullParameter(ps, i, parameter, jdbcType); } catch (Exception e) { throw new TypeException("Error setting non null for parameter #" + i + " with JdbcType " + jdbcType + " . " + "Try setting a different JdbcType for this parameter or a different configuration property. "
+ "Cause: " + e, e); } } }

子類 BigIntegerTypeHandlersetNonNullParameter 的實現如下:

@Override
  public void setNonNullParameter(PreparedStatement ps, int i, BigInteger parameter, JdbcType jdbcType) throws SQLException {
    ps.setBigDecimal(i, new BigDecimal(parameter));
  }

可以看出,當引數的值為 NULL,且沒有指定 jdbcType 時,mybatis會丟擲 “JDBC requires that the JdbcType must be specified for all nullable parameters.” 異常。

而當引數不為 NULL 時,Mybatis 會針對引數自身的型別呼叫 PreparedStatement 上不同的方法,此時 JdbcType 則是可以省略的。

再看 JDBC 中PreparedStatement 的 setNull 方法就不難明白,JDBC 要求我們設定 NULL 引數時必須指定 JDBC 型別,Mybatis 只是照章辦事而已。

void setNull(int parameterIndex,
             int sqlType)
             throws SQLException
Sets the designated parameter to SQL NULL.
Note: You must specify the parameter's SQL type.

Parameters:
parameterIndex - the first parameter is 1, the second is 2, ...
sqlType - the SQL type code defined in java.sql.Types
Throws:
SQLException - if parameterIndex does not correspond to a parameter marker in the SQL statement; if a database access error occurs or this method is called on a closed PreparedStatement
SQLFeatureNotSupportedException - if sqlType is a ARRAY, BLOB, CLOB, DATALINK, JAVA_OBJECT, NCHAR, NCLOB, NVARCHAR, LONGNVARCHAR, REF, ROWID, SQLXML or STRUCT data type and the JDBC driver does not support this data type

相關推薦

MyBatis 插入需要指定JdbcType.如#{name,jdbcType=VARCHAR}

在執行SQL時MyBatis會自動通過物件中的屬性給SQL中引數賦值,它會自動將Java型別轉換成資料庫的型別。而一旦傳入的是null它就無法準確判斷這個型別應該是什麼,就有可能將型別轉換錯誤,從而報錯。要解決這個問題,需要針對這些可能為空的欄位,手動指定其轉換時用到的型別。

MyBatis 插入需要指定JdbcType

看一看 org.apache.ibatis.type.BaseTypeHandler @Override public void setParameter(PreparedStatement ps, int i, T parameter, JdbcType

Oracle資料庫mybatis 插入的處理

mybatis 插入空值時報錯(with JdbcType OTHER)的兩種解決方案在使用Mybatis 3 時,發現了這個問題,當插入資料時,如果有一個欄位為空值時,系統會報異常,導致插入資料失敗。異常資訊類似:org.springframework.jdbc.Uncat

mybatis 插入時報錯 TypeException

diff term pac bigint eric nested lin highlight ping 報錯內容:nested exception is org.apache.ibatis.type.TypeException: Could not set paramete

Mybatis插入報錯

Exception in thread "main" org.springframework.jdbc.UncategorizedSQLException: Error setting null for parameter #6 with JdbcType OTHER . Try setting a dif

access Vba中對日期時間欄位賦要用Null 而不是""。

  最近一片白雲對“鶴慶縣人事工資變動調資與工資管理系統”增加了一些功能,其中就是能將Access資料表“職工檔案”中的“薪級變動時間”欄位、“崗位變動時間”欄位的日期用程式自動更新為空,在用SQL語句中遇到一點點小問題,經過在網上查詢,問題總算解決了,怪自己學藝不夠紮實。現將SQL語句提供給正在尋找答案的

spring boot加mybatis使用Map返回屬性也會沒有(轉)

call pri per n-n spring fig setter 解決 strong 使用spring boot加mybatis時,設置Map返回,當值為空時屬性也會沒有,就會報錯 在application.properties中加入下面配置,將會解決這個問題。

mybatis返回的(resultType)為一個類(javaBean)需要注意的事項

今天做專案時發現一個問題,查詢資料庫時返回的值居然不對,字串的值為空,實數型的值為0,只有那個id對了,意義說連線資料查詢這一塊應該沒錯,看日誌也沒有報錯,想來想去應該是欄位映射出了錯,找到原因就好找

spring mvc+mybatis返回map型別資料為欄位不顯示問題

<select id="getArticleById" resultType="map"> select * from article t </select> 當返回結果resultType為map時,

【sql技巧】mysql修改動態指定要修改的字段 update `table` set (case when ....) = 1 where id = xx

幫助 ase stack pan 希望 需求 記錄 開始 lec 如果你點進了這篇帖子,那麽你一定遇到了跟我一樣的問題。別看題目的set case when...,我一開始也是第一反應是用case when但是發現並不好使。 問題呢,說得高大上一點:動態指定要修改的字段。

利用formdata對象上傳文件需要添加的參數

for alert string local art .ajax ror process 文件 function doUpload() { var formData = new FormData($( "#uploadForm" )[0]);

FreeMarker 的處理 簡單理解 不用TMD就會忘記

mark bool 顯示 空字符 marker freemark 返回 後者 als NO.1  而對於FreeMarker來說,null值和不存在的變量是完全一樣的 NO.2  !        指定缺失變量的默認值      返回String NO.3  ??     

egret 裏面設置MovieClip的scale縮放沒有效果的情況

mil gpo ont 原因 縮放 設置 遇到 spa cli 1,有時,在設置MovieClip的縮放值scale屬性時,沒有效果,本人遇到的情況解決的辦法是:在MovieClip外面套個Group,設置Group的scale屬性就能達到效果了。具體原因待考究egret

(轉)Java 中關於String的對象(null) ,(empty)空格

空值 ise als 內存 ati 調用 ext cor under 原文出處:Java 中關於String的空對象(null) ,空值(empty),空格 定義 空對象: String s = null; 空對象是指定義一個對象s,但是沒有給該對象分配空間,即沒有實例化

mysql插入數據去掉重復的數據;

table nor font eat pre mys tinc 一個 PE 1. 利用insert ignore into語句去重 mysql> INSERT IGNORE INTO person_tbl (last_name, first_name) -&

在使用add()方法添加組件到容器必須指定將其放置在哪個區域中

中國 label 默認 create ram lis 指定 不變 pac BorderLayout是Window、Frame和Dialog的默認布局管理器,其將容器分成North、South、East、West和Center 5個區域,每個區域只能放置一個組件。 在使用ad

Python3基礎 dict get 在查詢不存在的鍵返回指定的內容

ubuntu ocs 理解 初心 git 哲學 print lang finish ? python : 3.7.0 OS : Ubuntu 18.04.1 LTS

問題記錄之用poi生成圖片並插入到word圖片中的中文顯示不出來

ubuntu ttf 第一個 方案 chmod table ber dot 命名 問題原因:linux中沒有相關中文字體 解決方案:在linux中安裝中文字體 有兩種方式可以添加字體,第一種是在java環境下安裝字體,第二種是在linux中安裝字體 一.在java環境下安裝

制定自動化測試的策略需要考慮的點

制定自動化測試的策略時,需要考慮以下幾點:      1. 確定被測的軟體適合做自動化測試 適合做自動化測試的系統,通常是一些生命週期比較長、且系統功能實現自動化測試也較為容易的專案或產品。      2. 確定自動化測試的風險 根據所屬系統

企業在選擇MES軟體需要考慮二次開發嗎?

從一定程度來說,二次開發也是MES上線的目的之一。MES的固化一旦成了僵化,就會成為生產製造的阻礙,這就需要進行二次開發。MES需要再次通過客戶化、功能增強、功能修改、本地化和介面去滿足客戶特定要求。固化而不僵化,唯有將系統的定義置於靈活的需求制導引擎中,不僅開發階段如此,維護階段也是如此。用得好的,就保留、