微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – MyBatis – jdbcTypeForNull Oracle

我正在使用MyBatis和Oracle 11g R2数据库.我正在使用MyBatis 3.3和ojdbc6 12.1.0.2.我的问题是每当我尝试插入一个null的对象时,我得到以下内容.

org.springframework.jdbc.UncategorizedsqlException: Error setting null
for parameter #8 with JdbcType OTHER . Try setting a different
JdbcType for this parameter or a different jdbcTypeForNull
configuration property. Cause: java.sql.sqlException: Invalid column
type: 1111

我的理解是在最新版本的JDBC中将null映射到JdbcType.OTHERS,而没有所有驱动程序都处理,显然Oracle就是其中之一.

我在MyBatis配置中尝试了以下内容,但仍然没有运气.

    @Bean
    public sqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        final sqlSessionfactorybean sessionFactory = new sqlSessionfactorybean();
        sessionFactory.setDataSource(dataSource);
        sessionFactory.setTypeAliasesPackage("org.ohtech.innovationexchange.core.domain");
        sessionFactory.setTransactionFactory(springManagedTransactionFactory());
        sessionFactory.setConfigurationProperties(getProperties());
        return sessionFactory.getobject();
    }

    @Bean(name = "transactionManager")
    public DataSourceTransactionManager dataSourceTransactionManager() throws PropertyVetoException{
        return new DataSourceTransactionManager(dataSource());
    }

    @Bean
    public SpringManagedTransactionFactory springManagedTransactionFactory() {
        return new SpringManagedTransactionFactory();
    }

    private Properties getProperties() {
        final Properties myBatisProperties = new Properties();
        myBatisProperties.put("jdbcTypeForNull", "NULL");
        return myBatisProperties;
    }

我可以通过在我的映射文件中执行以下操作来使其工作,但它看起来非常重复和丑陋.不确定为什么MyBatis没有使用我的属性我传递的是sqlSessionFactory bean.

解决方法:

“jdbcTypeForNull”不是“属性”,而是“设置”.目前无法通过java配置设置,我猜.你需要这样的config.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <setting name="jdbcTypeForNull" value="NULL" />
    </settings>
</configuration>

并使用sessionFactory.setConfigLocation(…).

有关设置和属性间的区别,请参阅文档:
https://mybatis.github.io/mybatis-3/configuration.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐