java.lang.NumberFormatException: For input string: "F"

在通过myBatis执行sql时,报错: java.lang.NumberFormatException: For input string: "F"

xml中sql内容为:

                    <if test="myKey == 'P' ">
                        and `Field1` = #{fieldname}
                    </if>

其中 fieldname的值为 F, 没明白会报 NumberFormatException, 明明是字符型,后一步步调试代码到:

\.m2\repository\org\mybatis\mybatis\3.5.2\mybatis-3.5.2.jar!\org\apache\ibatis\ognl\ASTNotEq.class

报错的代码为(OgnlOps.equal):

    protected Object getValueBody(OgnlContext context, Object source) throws OgnlException {
        Object v1 = this._children[0].getValue(context, source);
        Object v2 = this._children[1].getValue(context, source);
        return OgnlOps.equal(v1, v2) ? Boolean.FALSE : Boolean.TRUE;
    }

v1,v2值分别为下图:

 v2的值成了P,得到v2的类型为: 

 没明白,后边试着把 <if test="myKey == 'P' "> 调整为:  <if test="myKey == 'PP' "> 多加了一个P

这时v2的类型就为String了,估计MyBatis如果发现为单字符,都统一处理为了 Character, 试着调整为: <if test="myKey == "P" "> 单引号改为双引号(或加转义符),都不行

试着转换单双引号为(test里外包的将双引号改为单引号, P字符调整为双引号): 

                    <if test='myKey == "P" '>
                        and `Field1` = #{fieldname}
                    </if>

再试,功能正常

猜你喜欢

转载自www.cnblogs.com/Wicher-lsl/p/11535942.html