本文主要是介绍java.lang.NumberFormatException: For input string: myabtis 报错解决方案,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前言:mybatis使用字符串参数的时候 mybatis java.lang.NumberFormatException: For input string: 报错,是因为我们的用法有偏差
解决方法
第一种解决方法:
<if test="isExpired=='Y'">
and msg.expire_time < now()
</if>
会报NumberFormatException,这样就可以了。
<if test="isExpired=='Y'.toString()">
and msg.expire_time < now()
</if>
第二种解决方法
<if test=" name=='你好' "><if>这样会有问题,换成<if test=' name=="你好" '><if>
实际用到的地方是这样的
当一个条件既要用到等于又要用到>等判断的时候这样做
<if test="_parameter.containsKey('colNum') and colNum!='6'.toString()" >and a.col_num = #{colNum}</if><if test="_parameter.containsKey('colNum') and colNum =='6'.toString()" >and (a.col_num+0) > 5</if>
非常方便
如果用<号 会冲突和<if,是这样解决的
<![CDATA[<if test="_parameter.containsKey('sendAreaName')" >and a.send_area_name <#{sendAreaName} </if>]]>
研究探究:
mybatis 动态sql 中if判断使用的ognl表达式,现在分3中情况说明并验证。
一、情况说明:
传入的itemCode为参数传入mybatis
<if test='itemCode != null and itemCode !="" and itemCode =="xxx" '>
1、 单个字符的数字型字符串
例如:传入参数 itemCode=“1”
以下写法不符合判断
<if test="itemCode != null and itemCode !='' and itemCode =='1'.toString()">
或
<if test='itemCode != null and itemCode !="" and itemCode =="1" '>
2、单个字符的非数字型字符串
例如:传入参数 itemCode=“z”
<if test="itemCode != null and itemCode !='' and itemCode =='z'">
会报错 NumberFormatException,如果想让判断符合条件如下写法。
<if test="itemCode != null and itemCode !='' and itemCode =='z'.toString()">
或
<if test='itemCode != null and itemCode !="" and itemCode =="z" '>
3、不是单个字符的字符串
例如:传入参数 itemCode=“张三”
不用.toString()或单引号变双引号就会符合条件
<if test="itemCode != null and itemCode !='' and itemCode =='张三'">
二、验证代码
1.添加依赖
<dependency><groupId>ognl</groupId><artifactId>ognl</artifactId><version>2.6.9</version></dependency>
2.代码示例
public class TestOgnl {public static void main(String[] args) throws Exception {Map<String, Object> context = new HashMap<String, Object>();context.put("id", "1");context.put("name", "z");context.put("sex", "man");System.out.println(context);// 单个字符的数字型字符串System.out.println(getValue("id == '1'", context));// falseSystem.out.println(getValue("id == '1'.toString()", context));// true// 单个字符的非数字型字符串try {System.out.println(getValue("name == 'z'", context));} catch (Exception e) {// 'z'不能被转成数值型,此处会抛出NumberFormatExceptione.printStackTrace();}System.out.println(getValue("name == \"z\"", context));// trueSystem.out.println(getValue("name == 'z'.toString()", context));// true// 不是单个字符的字符串System.out.println(getValue("sex == 'man'", context));// trueSystem.out.println(getValue("sex == \"man\"", context));// trueSystem.out.println(getValue("sex == 'man'.toString()", context));// true}public static Object getValue(String expression,Object map) throws OgnlException {return Ognl.getValue(expression, map);}}
这篇关于java.lang.NumberFormatException: For input string: myabtis 报错解决方案的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!