解决mybatis在oracle中null的问题
来源:互联网 发布:上汽集团有限公司知乎 编辑:程序博客网 时间:2024/05/16 04:59
使用mybatis时遇到Error setting null parameter异常,常用的解决方式是在sql中指定jdbcType,这样的写法感觉有点罗嗦,后来参考spring jdbc中的实现方式,改写了mybatis中的BaseTypeHandler类,经测试ok。
以下是修改的类文件,修改的地方为19 ~ 36行。package org.apache.ibatis.type;
import java.sql.CallableStatement;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
public abstract class BaseTypeHandler implements TypeHandler {
public void setParameter(PreparedStatement ps, int i, Object parameter,
JdbcType jdbcType) throws SQLException {
if (parameter == null) {
if (jdbcType == null) {
try {
boolean useSetObject = false;
int sqlType = Types.NULL;
try {
DatabaseMetaData dbmd = ps.getConnection()
.getMetaData();
String databaseProductName = dbmd
.getDatabaseProductName();
String jdbcDriverName = dbmd.getDriverName();
if (databaseProductName.startsWith("Informix")
|| jdbcDriverName
.startsWith("Microsoft SQL Server")) {
useSetObject = true;
} else if (databaseProductName.startsWith("Oracle")
|| databaseProductName.startsWith("DB2")
|| jdbcDriverName.startsWith("jConnect")
|| jdbcDriverName.startsWith("SQLServer")
|| jdbcDriverName.startsWith("Apache Derby")) {
sqlType = Types.VARCHAR;
}
} catch (Throwable ex) {
throw new TypeException(
"Could not check database or driver name", ex);
}
if (useSetObject) {
ps.setObject(i, null);
} else {
ps.setNull(i, sqlType);
}
} catch (SQLException e) {
throw new TypeException(
"Error setting null parameter. Most JDBC drivers require that the JdbcType must be specified for all nullable parameters. Cause: "
+ e, e);
}
} else {
ps.setNull(i, jdbcType.TYPE_CODE);
}
} else {
setNonNullParameter(ps, i, parameter, jdbcType);
}
}
public Object getResult(ResultSet rs, String columnName)
throws SQLException {
Object result = getNullableResult(rs, columnName);
if (rs.wasNull()) {
return null;
} else {
return result;
}
}
public Object getResult(CallableStatement cs, int columnIndex)
throws SQLException {
Object result = getNullableResult(cs, columnIndex);
if (cs.wasNull()) {
return null;
} else {
return result;
}
}
public abstract void setNonNullParameter(PreparedStatement ps, int i,
Object parameter, JdbcType jdbcType) throws SQLException;
public abstract Object getNullableResult(ResultSet rs, String columnName)
throws SQLException;
public abstract Object getNullableResult(CallableStatement cs, int columnIndex) throws SQLException;
}
- 解决mybatis在oracle中null的问题
- ORACLE 在not in中使用null的问题
- ORACLE 在not in中使用null的问题
- ORACLE 在not in中使用null的问题
- 解决在mybatis中使用CTE进行oracle查询数据类型为long的字段出现流关闭问题
- oracle中关于null排序的问题
- ORACLE中null的排序问题
- Oracle中null的比较问题
- ORACLE中null的排序问题
- oracle中Null字段的计算问题
- Oracle大文本在ASP中存取问题的解决
- Oracle大文本在ASP中存取问题的解决
- 在Oracle的网络结构中解决连接问题
- oracle中关于null问题
- JAVA解决在@autowired,@Resource注入为null的问题
- Go语言:解决数据库中null值的问题
- mybatis sum函数返回为null的解决方式,格式化小数千分位的问题
- 在ORACLE中NULL与SQLSERVER的区别
- 项目管理人力或时间问题
- 猴子分桃
- 了解架构师(转+原)
- Visual Studio 调试小技巧(2)-从查看窗口得到更多信息
- 手机操作系统开源软件
- 解决mybatis在oracle中null的问题
- android: RoundCornerImageView 图片视图 倒圆角
- struts中redirect与dispatch的区别
- 陆首群: 再谈“看好MeeGo是匹黑马”
- struts学习笔记3接收web前端前台传来的数据
- 解决 GridView 多行表头显示 的方法
- python shell
- Libevent(二)
- MFC中定义全局变量和全局函数的方法