本文介绍了MyBatis-jdbcTypeForNull Oracle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将MyBatis与Oracle 11g R2数据库一起使用.我正在将MyBatis 3.3与ojdbc6 12.1.0.2.我的问题是,每当我尝试插入空对象时,我都会得到以下提示.

I am using MyBatis with an Oracle 11g R2 database. I am using MyBatis 3.3 with ojdbc6 12.1.0.2. My issue is whenever I tried to insert an object that is null I get the following.

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

My understanding is in the latest version of JDBC null is mapped to JdbcType.OTHERS which no all drivers handle, apparently Oracle is one of them.

我在MyBatis配置中尝试了以下操作,但还是没有运气.

I tried the following in my MyBatis configuration, but still no luck.

    @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;
    }

我可以通过在我的mapper文件中执行以下操作来使其工作,但它看起来确实是重复且丑陋的.不知道为什么MyBatis不使用我的属性,我正在传递SqlSessionFactory bean.

I can make it work by doing the following in my mapper files but it seems really repetitive and ugly. Not sure why MyBatis is not using my properties I am passing the SqlSessionFactory bean.

推荐答案

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

"jdbcTypeForNull" is not a 'property' but a 'setting'. Can not be set by java config currently, I guess. You need a config.xml like this:

<?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(...).

and use sessionFactory.setConfigLocation(...).

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

For the difference between settings and properties please refer to the documentation:https://mybatis.github.io/mybatis-3/configuration.html

这篇关于MyBatis-jdbcTypeForNull Oracle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 16:28