解决mybatis在oracle中null的问题

本文介绍了如何通过修改MyBatis中的BaseTypeHandler类来解决ErrorSettingNullParameter异常的问题,避免了手动指定JdbcType带来的繁琐。详细解释了关键修改部分的作用,并提供了测试验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用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;

}

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值