1. 程式人生 > >mybatis加jdbctype,jdbctype和javatype區別mybatis插入insert空指標無效字元

mybatis加jdbctype,jdbctype和javatype區別mybatis插入insert空指標無效字元

我們為什麼在用mybatis的時候要加一個jdbcTYPE

MyBatis 插入空值時,需要指定JdbcType 
mybatis insert空值報空值異常,但是在pl/sql不會提示錯誤,主要原因是mybatis無法進行轉換,

丟擲這樣的異常,setting null 或者無效字元

Exception in thread "main" org.springframework.jdbc.UncategorizedSQLException: Error setting null for parameter #6 with JdbcType OTHER . Try setting a different JdbcType for
this parameter or a different jdbcTypeForNull configuration property. Cause: java.sql.SQLException: 無效的列型別: 1111 ; uncategorized SQLException for SQL []; SQL state [99999]; error code [17004]; 無效的列型別: 1111; nested exception is java.sql.SQLException: 無效的列型別: 1111

MyBatis 通過包含的jdbcType型別

BIT FLOAT CHAR TIMESTAMP OTHER UNDEFINED

TINYINT REAL VARCHAR BINARY BLOB NVARCHAR

SMALLINT DOUBLE LONGVARCHAR VARBINARY CLOB NCHAR

INTEGER NUMERIC DATE LONGVARBINARY BOOLEAN NCLOB

BIGINT DECIMAL TIME NULL CURSOR

Xml程式碼
<insert id="insertCustomerLog" parameterType="map">
    insert into customer_log
    (
    ID,
    CUSTOMER_SERVICE_USER_NAME,
    user_name,
    CONTENT,
    LOG_FIRST_TYPE,
    STATUS,
    LINKED_ID,
    FEE,
    ACCOUNT_FIRST_TYPE,
    ACCOUNT_SECOND_TYPE,
    ACCOUNT_THIRD_TYPE,
    LOG_SECOND_TYPE,
    LOG_IP,
    MEMO
    )
    values
    (
    seq_customer_log.nextval ,
    #{customerServiceUserName}
, #{username}, #{content}, #{logFirstType}, #{status}, #{linkedId}, #{fee}, #{accountFirstType}, #{accountSecondType}, #{accountThirdType}, #{logSecondType}, #{logIp}, #{memo} ) </insert>

將程式碼修改,後異常不在出現,注意我添加了jdbcTYPE

Xml程式碼
<insert id="insertCustomerLog1" parameterType="com.diyicai.customer.domain.CustomerLog">  
        insert into customer_log  
               (  
               ID,  
               CUSTOMER_SERVICE_USER_NAME,  
               user_name ,  
               CONTENT,  
               LOG_FIRST_TYPE,  
               STATUS,  
               LINKED_ID,  
               FEE,  
               ACCOUNT_FIRST_TYPE,  
               ACCOUNT_SECOND_TYPE,  
               ACCOUNT_THIRD_TYPE,  
               LOG_SECOND_TYPE,  
               LOG_IP,  
               MEMO  
               )  
               values  
               (  
               seq_customer_log.nextval ,  
               #{customerServiceUserName,jdbcType=VARCHAR} ,  
               #{username,jdbcType=VARCHAR},  
               #{content,jdbcType=VARCHAR},  
               #{logFirstType,jdbcType=NUMERIC},  
               #{status,jdbcType=NUMERIC},  
               #{linkedId,jdbcType=VARCHAR},  
               #{fee,jdbcType=NUMERIC},  
               #{accountFirstType,jdbcType=NUMERIC},  
               #{accountSecondType,jdbcType=NUMERIC},  
               #{accountThirdType,jdbcType=NUMERIC},  
               #{logSecondType,jdbcType=NUMERIC},  
               #{logIp,jdbcType=VARCHAR},  
               #{memo,jdbcType=VARCHAR}  
               )  
    </insert>  

Mybatis中javaType和jdbcType對應和CRUD例子

Xml程式碼  
<resultMap type="java.util.Map" id="resultjcm">  
  <result property="FLD_NUMBER" column="FLD_NUMBER"  javaType="double" jdbcType="NUMERIC"/>  
  <result property="FLD_VARCHAR" column="FLD_VARCHAR" javaType="string" jdbcType="VARCHAR"/>  
  <result property="FLD_DATE" column="FLD_DATE" javaType="java.sql.Date" jdbcType="DATE"/>  
  <result property="FLD_INTEGER" column="FLD_INTEGER"  javaType="int" jdbcType="INTEGER"/>  
  <result property="FLD_DOUBLE" column="FLD_DOUBLE"  javaType="double" jdbcType="DOUBLE"/>  
  <result property="FLD_LONG" column="FLD_LONG"  javaType="long" jdbcType="INTEGER"/>  
  <result property="FLD_CHAR" column="FLD_CHAR"  javaType="string" jdbcType="CHAR"/>  
  <result property="FLD_BLOB" column="FLD_BLOB"  javaType="[B" jdbcType="BLOB" />  
  <result property="FLD_CLOB" column="FLD_CLOB"  javaType="string" jdbcType="CLOB"/>  
  <result property="FLD_FLOAT" column="FLD_FLOAT"  javaType="float" jdbcType="FLOAT"/>  
  <result property="FLD_TIMESTAMP" column="FLD_TIMESTAMP"  javaType="java.sql.Timestamp" jdbcType="TIMESTAMP"/>  
 </resultMap>  

Mybatis中javaType和jdbcType對應關係

Notepad程式碼  
JDBC Type           Java Type  
CHAR                String  
VARCHAR             String  
LONGVARCHAR         String 
NUMERIC             java.math.BigDecimal  
DECIMAL             java.math.BigDecimal  
BIT             boolean  
BOOLEAN             boolean  
TINYINT             byte  
SMALLINT            short  
INTEGER             int  
BIGINT              long  
REAL                float  
FLOAT               double  
DOUBLE              double  
BINARY              byte[]  
VARBINARY           byte[]  
LONGVARBINARY               byte[]  
DATE                java.sql.Date  
TIME                java.sql.Time  
TIMESTAMP           java.sql.Timestamp  
CLOB                Clob  
BLOB                Blob  
ARRAY               Array  
DISTINCT            mapping of underlying type  
STRUCT              Struct  
REF                         Ref  
DATALINK            java.net.URL[color=red][/color]  

mybatis3報Error setting null parameter. Most JDBC drivers require that the JdbcType must be specified for all nullable parameters,是由於引數出現了null值,對於Mybatis,如果進行操作的時候,沒有指定jdbcType型別的引數,mybatis預設jdbcType.OTHER導致,給引數加上jdbcType可解決(注意大小寫)