我正在尝试执行这个插入:
@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)。
关于这个错误的一些想法?
最佳答案
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/