gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 07:05:48 25 4
gpt4 key购买 nike

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

@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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com