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

问题描述

我使用mybatis-spring-1.0.3-SNAPSHOT mybatis-3.0.6 spring3.0.6.我试图从这样的表中删除记录:

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:

        <delete id="deleteNote" parameterType="hashMap">
    DELETE FROM BBSCS_NOTE 
    <where>
        <if test="ids !=null and ids.length > 0">
            <foreach collection="ids" item="id" open="(" close=")" separator=",">
                ID IN #{id}
            </foreach>
        </if>
        <if test="toID != null and toID != ''">AND TOID = #{toID}</if>
        <if test="fromID != null and fromID != ''">AND FROMID = #{fromID}</if>
        <if test="noteType != -1">AND NOTETYPE = #{noteType}</if>
    </where>         
</delete>

如您所见,它是一个动态sql.java测试代码如下:

As you have seen,it's a dynamic sql.The java test code like this:

Map map = new HashMap();
String ids[] = {"1","2","3"};
map.put("ids", ids);
noteService.del(map);

当我执行Java测试代码时,会出现一些类似这样的异常:

When I executed java test code,there was some exception like this:

   org.springframework.jdbc.UncategorizedSQLException: Error setting null parameter.  Most JDBC drivers require that the JdbcType must be specified for all nullable parameters. Cause: java.sql.SQLException: Invalid column type
; uncategorized SQLException for SQL []; SQL state [null]; error code [17004]; Invalid column type; nested exception is java.sql.SQLException: Invalid column type

为什么?您能给我一些解决这个问题的建议吗?

Why?Can you give me some advice to solve this problem?Thanks.

推荐答案

好,我看到了一些问题.首先,当在Prepared语句或Callable语句中设置null参数时,MyBatis需要知道jdbc类型.像这样

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}

您还会错误地生成'in子句.您需要使用foreach标记仅生成值列表.将"ID IN"部分移出foreach标记.

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>

我也建议您不要使用HashMaps.新的Mapper类要好得多.

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

这篇关于Mybatis-Error设置null参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 16:25