SpringBoot Mybatis Oracle相关报错解决与原理源码解析:java.sql.SQLException 无效的列类型 1111

问题

SpringBoot+Mybatis+Oracle这套环境出现报错

Caused by: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='modifyTime', mode=IN, javaType=class java.util.Date, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting null for parameter #5 with JdbcType OTHER . Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. Cause: java.sql.SQLException: 无效的列类型: 1111
	at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:96)
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:441)
	at com.sun.proxy.$Proxy133.insert(Unknown Source)
	at org.mybatis.spring.SqlSessionTemplate.insert(SqlSessionTemplate.java:272)
	at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.execute(MybatisMapperMethod.java:59)
	at com.baomidou.mybatisplus.core.override.MybatisMapperProxy$PlainMethodInvoker.invoke(MybatisMapperProxy.java:148)
	at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:89)
	at com.sun.proxy.$Proxy138.insert(Unknown Source)
	at com.baomidou.mybatisplus.extension.service.IService.save(IService.java:63)
	at com.baomidou.mybatisplus.extension.service.IService$$FastClassBySpringCGLIB$$f8525d18.invoke(<generated>)
	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:779)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:750)
	at org.springframework.retry.annotation.AnnotationAwareRetryOperationsInterceptor.invoke(AnnotationAwareRetryOperationsInterceptor.java:156)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:750)
	at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:692)
	at com.ais.cdc.service.impl.FileDirectoryServiceImpl$$EnhancerBySpringCGLIB$$9db0eb62.save(<generated>)
	at com.ais.cdc.config.init.DbDataInitializer.initParentDir(DbDataInitializer.java:28)
	at com.ais.cdc.config.init.DbDataInitializer.run(DbDataInitializer.java:21)
	at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:783)
	... 10 common frames omitted
Caused by: org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='modifyTime', mode=IN, javaType=class java.util.Date, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting null for parameter #5 with JdbcType OTHER . Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. Cause: java.sql.SQLException: 无效的列类型: 1111
	at com.baomidou.mybatisplus.core.MybatisParameterHandler.setParameters(MybatisParameterHandler.java:236)
	at org.apache.ibatis.executor.statement.PreparedStatementHandler.parameterize(PreparedStatementHandler.java:94)
	at org.apache.ibatis.executor.statement.RoutingStatementHandler.parameterize(RoutingStatementHandler.java:64)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:64)
	at com.sun.proxy.$Proxy228.parameterize(Unknown Source)
	at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:88)
	at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:49)
	at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:117)
	at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:76)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.apache.ibatis.plugin.Invocation.proceed(Invocation.java:49)
	at com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor.intercept(MybatisPlusInterceptor.java:106)
	at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:62)
	at com.sun.proxy.$Proxy227.update(Unknown Source)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:64)
	at com.sun.proxy.$Proxy227.update(Unknown Source)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:64)
	at com.sun.proxy.$Proxy227.update(Unknown Source)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:194)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:181)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:427)
	... 30 common frames omitted
Caused by: org.apache.ibatis.type.TypeException: Error setting null for parameter #5 with JdbcType OTHER . Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. Cause: java.sql.SQLException: 无效的列类型: 1111
	at org.apache.ibatis.type.BaseTypeHandler.setParameter(BaseTypeHandler.java:67)
	at com.baomidou.mybatisplus.core.MybatisParameterHandler.setParameters(MybatisParameterHandler.java:234)
	... 69 common frames omitted
Caused by: java.sql.SQLException: 无效的列类型: 1111
	at oracle.jdbc.driver.OracleStatement.getInternalType(OracleStatement.java:4369)
	at oracle.jdbc.driver.OraclePreparedStatement.setNullCritical(OraclePreparedStatement.java:5836)
	at oracle.jdbc.driver.OraclePreparedStatement.setNull(OraclePreparedStatement.java:5818)
	at oracle.jdbc.driver.OraclePreparedStatementWrapper.setNull(OraclePreparedStatementWrapper.java:1292)
	at com.alibaba.druid.pool.DruidPooledPreparedStatement.setNull(DruidPooledPreparedStatement.java:270)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.apache.ibatis.logging.jdbc.PreparedStatementLogger.invoke(PreparedStatementLogger.java:67)
	at com.sun.proxy.$Proxy230.setNull(Unknown Source)
	at org.apache.ibatis.type.BaseTypeHandler.setParameter(BaseTypeHandler.java:65)
	... 70 common frames omitted

剖析报错

错误分一下层次

第一层 转发错误

MyBatisSystemException

Caused by: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='modifyTime', mode=IN, javaType=class java.util.Date, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting null for parameter #5 with JdbcType OTHER . Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. Cause: java.sql.SQLException: 无效的列类型: 1111
	at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:96)

报错位置

org.mybatis.spring.MyBatisExceptionTranslator

public DataAccessException translateExceptionIfPossible(RuntimeException e) {
    if (e instanceof PersistenceException) {
        if (((RuntimeException)e).getCause() instanceof PersistenceException) {
            e = (PersistenceException)((RuntimeException)e).getCause();
        }

        if (((RuntimeException)e).getCause() instanceof SQLException) {
            this.initExceptionTranslator();
            String task = ((RuntimeException)e).getMessage() + "\n";
            SQLException se = (SQLException)((RuntimeException)e).getCause();
            DataAccessException dae = this.exceptionTranslator.translate(task, (String)null, se);
            return (DataAccessException)(dae != null ? dae : new UncategorizedSQLException(task, (String)null, se));
        } else if (((RuntimeException)e).getCause() instanceof TransactionException) {
            throw (TransactionException)((RuntimeException)e).getCause();
        } else {
            return new MyBatisSystemException((Throwable)e);
        }
    //***
}

只是负责转发报错

第二层 获取默认jdbcType

TypeException

Caused by: org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='modifyTime', mode=IN, javaType=class java.util.Date, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting null for parameter #5 with JdbcType OTHER . Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. Cause: java.sql.SQLException: 无效的列类型: 1111
	at com.baomidou.mybatisplus.core.MybatisParameterHandler.setParameters(MybatisParameterHandler.java:236)

报错位置

com.baomidou.mybatisplus.core.MybatisParameterHandler

public void setParameters(PreparedStatement ps) {
    //***
				TypeHandler typeHandler = parameterMapping.getTypeHandler();
    			//这一步获取的jdbcType是null
				JdbcType jdbcType = parameterMapping.getJdbcType();
          		if (value == null && jdbcType == null) {
                    //从配置获取jdbcType,获取到的是1111,指什么,看下一节
            		jdbcType = configuration.getJdbcTypeForNull();
          		}
                try {
                    //把1111传给了TypeHandler对象的setParameter方法
                    typeHandler.setParameter(ps, i + 1, value, jdbcType);
                } catch (SQLException | TypeException var10) {
                    throw new TypeException("Could not set parameters for mapping: " + parameterMapping + ". Cause: " + var10, var10);
                }
    //***
}

进configuration.getJdbcTypeForNull();看一下

org.apache.ibatis.session.Configuration

protected JdbcType jdbcTypeForNull = JdbcType.OTHER;

public JdbcType getJdbcTypeForNull() {
    return this.jdbcTypeForNull;
}

public void setJdbcTypeForNull(JdbcType jdbcTypeForNull) {
    this.jdbcTypeForNull = jdbcTypeForNull;
}

如果没人来set一下,那默认值是JdbcType.OTHER

org.apache.ibatis.type.JdbcType

public enum JdbcType {
	//***
    OTHER(Types.OTHER),
    //***
}

java.sql.Types 用来标识数据库类型对应的值

package java.sql;

/**
 * <P>The class that defines the constants that are used to identify generic
 * SQL types, called JDBC types.
 * <p>
 * This class is never instantiated.
 */
public class Types {

/**
 * <P>The constant in the Java programming language, sometimes referred
 * to as a type code, that identifies the generic SQL type
 * <code>BIT</code>.
 */
        public final static int BIT             =  -7;

	//***
    
    /**
     * The constant in the Java programming language that indicates
     * that the SQL type is database-specific and
     * gets mapped to a Java object that can be accessed via
     * the methods <code>getObject</code> and <code>setObject</code>.
     */
        public final static int OTHER           = 1111;
		//***
}

这里获取了默认的jdbcType,值是1111

第三层 空参数的处理

TypeException

Caused by: org.apache.ibatis.type.TypeException: Error setting null for parameter #5 with JdbcType OTHER . Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. Cause: java.sql.SQLException: 无效的列类型: 1111
   at org.apache.ibatis.type.BaseTypeHandler.setParameter(BaseTypeHandler.java:67)

报错位置

org.apache.ibatis.type.BaseTypeHandler

public void setParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException {
    //参数为空,进来了
    if (parameter == null) {
        //jdbcType是默认值1111兜底,不会进
        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 var7) {
            //报了这个错
            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: " + var7, var7);
        }
    } else {
        try {
            this.setNonNullParameter(ps, i, parameter, jdbcType);
        } catch (Exception var6) {
            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: " + var6, var6);
        }
    }

}

在参数为空、jdbcType为1111时,调用了PreparedStatement对象的setNull方法

第四层 查找JDK与Oracle数据类型的对应关系

java.sql.SQLException

Caused by: java.sql.SQLException: 无效的列类型: 1111
   at oracle.jdbc.driver.OracleStatement.getInternalType(OracleStatement.java:4369)

报错位置

oracle.jdbc.driver.OracleStatement

void setNullCritical(int var1, int var2) throws SQLException {
    int var3 = var1 - 1;
    if (var3 >= 0 && var1 <= this.numberOfBindPositions) {
        Object var4 = null;
        int var5 = this.getInternalType(var2);
        //***
}

这没捕获异常,进去

int getInternalType(int var1) throws SQLException {
    boolean var2 = false;
    short var3;
    switch (var1) {
        case -104:
            var3 = 183;
            break;
        case -103:
            var3 = 182;
            break;
        case -102:
            var3 = 231;
            break;
        case -101:
        case 2013:
        case 2014:
            var3 = 181;
            break;
        case -100:
        case 93:
            var3 = 180;
            break;
        case -16:
        case -1:
            var3 = 8;
            break;
        case -15:
        case -9:
        case 12:
            var3 = 1;
            break;
        case -14:
            var3 = 998;
            break;
        case -13:
            var3 = 114;
            break;
        case -10:
        case 2012:
            var3 = 102;
            break;
        case -8:
            var3 = 104;
            break;
        case -7:
        case -6:
        case -5:
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
        case 7:
        case 8:
            var3 = 6;
            break;
        case -4:
            var3 = 24;
            break;
        case -3:
        case -2:
            var3 = 23;
            break;
        case 0:
            var3 = 995;
            break;
        case 1:
            var3 = 96;
            break;
        case 70:
            var3 = 1;
            break;
        case 91:
        case 92:
            var3 = 12;
            break;
        case 100:
            var3 = 100;
            break;
        case 101:
            var3 = 101;
            break;
        case 252:
            var3 = 252;
            break;
        case 999:
            var3 = 999;
            break;
        case 2002:
        case 2003:
        case 2007:
        case 2008:
        case 2009:
            var3 = 109;
            break;
        case 2004:
            var3 = 113;
            break;
        case 2005:
        case 2011:
            var3 = 112;
            break;
        case 2006:
            var3 = 111;
            break;
        default:
            throw (SQLException)((SQLException)DatabaseError.createSqlException(this.getConnectionDuringExceptionHandling(), 4, Integer.toString(var1)).fillInStackTrace());
    }

    return var3;
}

这里1111匹配不到对应的数字,把1111,就是入参var1打了出来

这数字对应关系,应该就是java.sql.Types(参数类型值)和对应数据库(就是Oracle)的数据类型的对应关系,找不到就报错

解决

设置

mybatis.configuration.jdbc-type-for-null='null'

mybatis-plus是这个

mybatis-plus.configuration.jdbc-type-for-null=null

原理

原理是这个类或读取配置中的jdbcTypeForNull,配置文件json中mybatis.configuration.jdbc-type-for-null对应的就是org.apache.ibatis.session.Configuration的jdbcTypeForNull

org\mybatis\spring\boot\mybatis-spring-boot-autoconfigure\2.2.0\mybatis-spring-boot-autoconfigure-2.2.0.jar!\META-INF\spring-configuration-metadata.json

{
  "name": "mybatis.configuration.jdbc-type-for-null",
  "type": "org.apache.ibatis.type.JdbcType",
  "sourceType": "org.apache.ibatis.session.Configuration"
},

null会定位到org.apache.ibatis.type.JdbcType的NULL(0),

org.apache.ibatis.builder.xml.XMLConfigBuilder

configuration.setJdbcTypeForNull(JdbcType.valueOf(props.getProperty("jdbcTypeForNull", "OTHER")));

org.apache.ibatis.type.JdbcType

NULL(0),
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: MyBatisPlus引起的java.sql.SQLException: ORA-00911: 无效字符。 这个错误是由于在SQL语句中使用到了无效的字符导致的。ORA-00911错误通常发生在Oracle数据库中,意味着在SQL语句中存在无效或不支持的字符。 要解决这个问题,你可以遵循以下几个步骤: 1. 检查你的SQL语句中是否有不支持的字符,例如特殊符号或非法的空格。确保SQL语句的语法是正确的,并且只包含支持的字符。 2. 如果你使用了特殊字符,尝试使用转义字符将其转义。例如,对于引号字符('),你可以使用两个引号('')进行转义。 3. 检查连接到数据库的字符集设置。确保你的数据库字符集与你的SQL语句中使用的字符集匹配。如果字符集不匹配,可能会导致无效字符错误。 4. 如果以上步骤都没有解决问题,可能是由于数据库版本不兼容或存在其他数据库配置问题。可以尝试更新数据库版本,或者参考MyBatisPlus的官方文档,查找其他可能的配置问题。 总结:ORA-00911错误是由于SQL语句中的无效字符引起的。通过检查SQL语句中的字符和语法,确认字符集设置是否匹配,以及检查数据库配置等方法,可以解决这个问题。 ### 回答2: mybatisplus引发的java.sql.SQLException: ORA-00911: 无效字符 错误是由于在执行SQL语句时使用了无效的字符导致的。 ORA-00911错误通常是由于SQL语句中包含了Oracle数据库不支持的特殊字符或者符号,比如表名或字段名中包含了非法字符,或者使用了Oracle不支持的SQL语法等。 解决该问题的方法包括以下几点: 1. 检查SQL语句中所有的表名、字段名是否都是合法的,特别是需要注意有无非法字符,比如空格、特殊符号等。 2. 检查SQL语句中是否使用了Oracle数据库不支持的SQL语法,比如使用了MySQL或者SQL Server中的特定函数或者关键字,需要更换为Oracle数据库支持的相应语法。 3. 检查SQL语句是否缺少必要的关键字或者符号,比如漏写了逗号、括号等。 4. 检查是否存在SQL注入的风险,如果在SQL语句中使用了动态参数传递数据,需要对输入参数进行严格校验和过滤,避免用户输入恶意的SQL语句。 总之,ORA-00911错误通常是由于SQL语句中包含了Oracle数据库不支持的字符或者符号所导致的,通过仔细排查相关SQL语句中的问题,并进行相应的修正,通常可以解决该问题。 ### 回答3: MyBatis Plus引发的java.sql.SQLException: ORA-00911: 无效字符。 这个错误通常是由于SQL查询中包含了一个无效或非法的字符导致的。ORA-00911错误可能发生在以下几种情况下: 1. SQL查询中使用了无效的特殊字符,如@、#、$等。请检查SQL查询语句,并确保其中没有使用这些特殊字符。 2. SQL查询中使用了Oracle数据库不支持的特定字符或语法。请确保SQL查询语句符合Oracle数据库的语法规则,并查找是否使用了不支持的特定函数或操作。 3. SQL查询中存在语法错误或丢失的标点符号。请仔细检查SQL查询,确保语法正确,并不缺少任何必要的标点符号。 4. SQL查询中的表名、名或字符串值中包含了非法字符。请检查SQL查询中的表名、名或字符串值,并确保它们不包含任何特殊字符或非法字符。 要解决这个问题,您可以按照以下步骤进行操作: 1. 检查SQL查询语句,确认其中没有使用无效或非法的特殊字符。 2. 验证SQL查询语句是否符合Oracle数据库的语法规则,确保没有使用不支持的特定函数或操作。 3. 仔细检查SQL查询语句是否存在语法错误或丢失的标点符号,并进行必要的修正。 4. 检查SQL查询中的表名、名或字符串值,确保它们不包含任何特殊字符或非法字符。 5. 如果以上步骤都无法解决问题,可以将错误的SQL查询语句和相应的环境信息提交给MyBatis Plus的开发者或Oracle数据库的支持团队,以便他们能够提供更进一步的帮助和解决方案。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值