MyBatis 插入空值时,需要指定JdbcType
来源:互联网 发布:苹果6无4g网络只显示e 编辑:程序博客网 时间:2024/05/29 07:58
看一看 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); } } }
子类 BigIntegerTypeHandler
对 setNonNullParameter
的实现如下:
@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 SQLExceptionSets 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.TypesThrows: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 PreparedStatementSQLFeatureNotSupportedException - 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
0 0
- MyBatis 插入空值时,需要指定JdbcType
- MyBatis 插入空值时,需要指定JdbcType
- MyBatis 插入空值时,需要指定JdbcType
- MyBatis 插入空值时,需要指定JdbcType
- MyBatis 插入空值时,需要指定JdbcType
- MyBatis 插入空值时,需要指定JdbcType
- MyBatis 插入空值时,需要指定JdbcType
- MyBatis 插入空值时,需要指定JdbcType
- MyBatis 插入空值时,需要指定JdbcType
- mybatis 需要注意的点 MyBatis 插入空值时,需要指定JdbcType
- mybatis 需要注意的点 MyBatis 插入空值时,需要指定JdbcType
- mybatis 需要注意的点 MyBatis 插入空值时,需要指定JdbcType
- mybatis 需要注意的点 MyBatis 插入空值时,需要指定JdbcType (201
- mybatis 需要注意的点 MyBatis 插入空值时,需要指定JdbcType
- mybatis 需要注意的点 MyBatis 插入空值时,需要指定JdbcType (201
- mybatis 需要注意的点 MyBatis 插入空值时,需要指定JdbcType
- MyBatis插入空值时指定的JdbcType
- mybatis需要注意的点,插入空值时,需要制定jdbcType
- C/C++中使用static关键字定义或声明静态对象
- 欢迎使用CSDN-markdown编辑器
- 算法题:合并两个有序的链表
- iOS编译通用framework
- 第55篇 Chrome扩展USB开发(一)及PHP研究 Saturday
- MyBatis 插入空值时,需要指定JdbcType
- Ubuntu 16.04 安装 R 环境
- Escape from the Hell(暴力瞎搞)
- kinect for xboxOne在win10中的SDK安装成功
- php面向对象语法3 继承extends
- codeforces723C- Polycarp at the Radio-greedy
- 【CodeVS1004】四子连棋-迭代加深搜索入门
- Visual C++ 之 文件操作
- [心得]详解MySQL三种删除表的方法