java 尝试插入空值时,mybatis 与 spring 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26330809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error in mybatis with spring when try to insert nulls
提问by Jose Diaz Diaz
I am trying to execute this insert:
我正在尝试执行此插入:
@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);
But if some #{param} is NULL I got it error:
但是如果某些 #{param} 是 NULL 我得到它错误:
Caused by: org.apache.ibatis.type.TypeException: Error setting null for parameter #1 with JdbcType NULL . Try setting a different JdbcType for this parameter or a different jdbcTypeForNull
引起:org.apache.ibatis.type.TypeException: Error setting null for parameter #1 with JdbcType NULL 。尝试为此参数设置不同的 JdbcType 或不同的 jdbcTypeForNull
I was searching some solution and read that I must to configure a property: jdbcTypeForNull
我正在搜索一些解决方案并阅读我必须配置一个属性:jdbcTypeForNull
Then I update my configuration in Spring:
然后我在 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;
}
But the error continues yet.
但错误仍在继续。
I found many solutions for xml but I use only java interfaces.
Some idea about this error?
我找到了很多 xml 解决方案,但我只使用 java 接口。
关于这个错误的一些想法?
回答by Centinul
According to the 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.
如果 null 作为值传递,则 JDBC 需要所有可空列的 JDBC 类型。您可以通过阅读 PreparedStatement.setNull() 方法的 JavaDocs 自行调查。
Have you set the jdbcType for your parameters? For example:
您是否为您的参数设置了 jdbcType?例如:
#{property,javaType=int,jdbcType=NUMERIC}
Setting the jdbcTypeForNull may not work for your driver as indicated in the 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.
当没有为参数提供特定的 JDBC 类型时,为空值指定 JDBC 类型。一些驱动程序需要指定列 JDBC 类型,但其他驱动程序使用通用值,如 NULL、VARCHAR 或 OTHER。
回答by Eddú Meléndez
Try something like that
尝试这样的事情
Map<String, Object> map = new HashMap<>();
map.put("entTipo", (Long) null);
insertEntidades(map);
回答by Prasad Paravatha
If you want a global setting..try something like this
如果你想要一个全局设置..试试这样的
in Java code
在 Java 代码中
sqlSessionFactoryProperties.put("jdbcTypeForNull", JdbcType.DATE);
or in in mybatis-config.xml
或在 mybatis-config.xml 中
<configuration>
<settings>
<setting name="jdbcTypeForNull" value="DATE" />
</settings>
</configuration>
This way you are allowing insert/update for any DATE column.
这样您就可以允许对任何 DATE 列进行插入/更新。
I'd recommend using mybatis-config.xml solution to avoid issues with your Unit Tests
我建议使用 mybatis-config.xml 解决方案来避免单元测试出现问题