java - 尝试插入空值时 mybatis 与 spring 出错

标签 java spring oracle11g mybatis

我正在尝试执行这个插入:

@Insert("" +
            "insert into EXTRANET.EX_ENTIDAD (ENT_REFNUM, ENT_TIPO, REG_PUB_ID, OFIC_REG_ID, AREA_REG_ID, " +
            "COD_LIBRO, NUM_PARTIDA, ANO_TITU, NUM_TITU, ENT_TRIGGER, TMSTMP_CREA, PRIORIDAD) " +
            " values (EXTRANET.EX_ENTIDAD_SEQ.nextval, #{entTipo}, " +
            "#{regPubId}, #{oficRegId}, #{areaRegId}, #{codLibro}, #{numPartida}, #{anoTitu}, " +
            " #{numTitu}, 'SYNC', SYSDATE, #{prioridad})")
    public int insertEntidades(Map map);

但是如果一些#{param} 是 NULL 我得到它错误:

Caused by: org.apache.ibatis.type.TypeException: Error setting null for parameter #1 with JdbcType NULL .尝试为此参数设置不同的 JdbcType 或不同的 jdbcTypeForNull

我正在寻找一些解决方案,并读到我必须配置一个属性:jdbcTypeForNull

然后我在 Spring 中更新我的配置:

@Bean
public SqlSessionFactoryBean sqlSessionFactory() throws Exception,Throwable  {
    SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
    sessionFactory.setDataSource(getDataSource());
    //sessionFactory.setConfigLocation( new ClassPathResource("gob.pe.sunarp.extranet.config.config.xml"));
    sessionFactory.setTypeAliasesPackage("gob.pe.sunarp.extranet.dao.beans");
    final Properties sqlSessionFactoryProperties = new Properties();
    sqlSessionFactoryProperties.put("jdbcTypeForNull", "NULL");
    sessionFactory.setConfigurationProperties(sqlSessionFactoryProperties );

    return sessionFactory;
}

但错误仍然存​​在。

我找到了很多 xml 的解决方案,但我只使用 java 接口(interface)。
关于这个错误的一些想法?

最佳答案

根据MyBatis documentation

The JDBC Type is required by JDBC for all nullable columns, if null is passed as a value. You can investigate this yourself by reading the JavaDocs for the PreparedStatement.setNull() method.

您是否为参数设置了 jdbcType?例如:

#{property,javaType=int,jdbcType=NUMERIC}

documentation 中所示,设置 jdbcTypeForNull 可能不适用于您的驱动程序.

Specifies the JDBC type for null values when no specific JDBC type was provided for the parameter. Some drivers require specifying the column JDBC type but others work with generic values like NULL, VARCHAR or OTHER.

关于java - 尝试插入空值时 mybatis 与 spring 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26330809/

相关文章:

java - 使用java自动旋转扫描的文档

java - 我怎么知道我的 html 链接是否从 Servlet 被点击?

java - Spring 的链式 react

sql - Oracle函数以无序方式比较字符串

java - 如何提高 oracle 中简单选择查询的性能

java - 如何使用java在正则表达式中转义星号

java - x 时间后跳过 Selenium 测试

spring - 收到错误没有定义类型 [javax.persistence.EntityManagerFactory] ​​的合格 bean : expected single matching bean but found 2

sql - 查询性能非常差

java - 使用的 Lucene 搜索库算法