mybatis 报错:Cause: java.lang.NumberFormatException: For input string: ““

mybatis 报错:Cause: java.lang.NumberFormatException: For input string: “”


问题描述

我使用的mybatis版本是3.5.5。
使用mybatis动态SQL进行模糊查询,if条件判断时,报出数字格式错误。

	//Mapper.xml源代码
    <select id="selectByCondition" resultMap="brandResultMap">
        select *
        from tb_brand
        where
            status = #{status}
        <if test="companyName != null and companyName != ' '">
            and company_name like #{companyName}
        </if>
        <if test="brandName != null and brandName != ' '">
            and brand_name like #{brandName}
        </if>
    </select>
org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.lang.NumberFormatException: For input string: "%华为%"
### Cause: java.lang.NumberFormatException: For input string: "%华为%"

	... more
	at org.apache.ibatis.scripting.xmltags.DynamicSqlSource.getBoundSql(DynamicSqlSource.java:39)
	at org.apache.ibatis.mapping.MappedStatement.getBoundSql(MappedStatement.java:305)
	at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:87)
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:147)
	... 32 more
	
Process finished with exit code -1


原因分析:

问题的原因在于mybiatis是基于OGNL语法,在解析单引号+单字符 ‘Y’ 的解析时会默认时char类型,单引号+多字符 ‘AA’ 或者双引号+单字符 “A” 解析为String类型。


解决方案:

把单引号和双引号的位置互换即可。

	    <if test='companyName != null and companyName != " "'>
            and company_name like #{companyName}
        </if>
  • 9
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值