Mybatis-Error setting null parameter

前端 未结 2 1069
梦毁少年i
梦毁少年i 2020-12-31 06:10

I used mybatis-spring-1.0.3-SNAPSHOT mybatis-3.0.6 spring3.0.6.I tried to delete record from a table like this:

        

        
相关标签:
2条回答
  • 2020-12-31 06:38

    OK I see a few problems. First, when setting a null parameter into a Prepared Statement or a Callable Statement MyBatis needs to know the jdbc type. Like this,

    #{myNullParamenter, jdbcType=VARCHAR}
    

    You're also generating your 'in clause incorrectly. You need to use the foreach tag to only generate list of the values. Move the "ID IN" part out of the foreach tag.

    <if test="ids !=null and ids.length > 0">
        ID IN
        <foreach collection="ids" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </if>
    

    I would also recommend against using HashMaps. The new Mapper classes are much better.

    0 讨论(0)
  • 2020-12-31 06:59

    The problem is that since the 3.0.x versions the default JDBC type for null parameters is Types.OTHER which not supported by some JDBC drivers like Oracle 10g.

    Here a post that explain this issue.

    The solution I found is very simple, I set jdbcTypeForNull to NULL in the configuration file.

    <configuration>
        <properties resource="mybatis-config.properties" />
        <settings>
            <setting name="jdbcTypeForNull" value="NULL" />
        </settings>
    
        <environments default="development">
        ....
        </environments>
    
        <mappers>
        ....
       </mappers>
    </configuration>
    
    0 讨论(0)
提交回复
热议问题