资讯动态

自己挖的坑自己填--Mybatis mapper文件if标签中number类型及String类型的坑

发布时间:2026/10/21 10:34:42 来源:尧图企业网站定制
源码仓库Gitee · GitHub1.现象描述1使用 Mybatis 在进行数据更新时大部分时候update语句都需要通过动态SQL进行拼接。在其中if标签中经常会有 xxx ! 这种判断若 number 类型的字段上传递的值为 0 执行更新时会发现数据库中的数据并没有被更新成 0这种异常现象不会报错所以容易被忽视。update idupdate parameterTypecom.hrh.mybatis.bean.Person update tab_person set if testid ! null id #{id,jdbcTypeBIGINT}, /if if testname ! null name #{name,jdbcTypeVARCHAR}, /if if testage ! null and age ! age #{age,jdbcTypeBIGINT} /if /set where id #{id,jdbcTypeBIGINT} /update2在 if 标签中有时会用到加条件的判断如 xxx ! ‘x’如果筛选条件中只有单个字符时这样拼接执行会报错### Error querying database. Cause: java.lang.NumberFormatException: For input string: 张三### Cause: java.lang.NumberFormatException: For input string: 张三select idselectByCondition resultTypecom.hrh.mybatis.bean.Person parameterTypejava.lang.String select include refidBase_Column_List/ from tab_person where if testname ! null and name !a name #{name,jdbcTypeVARCHAR} /if /select2.原因探究1Mybatis的表达式使用 OGNL 处理的若对象是一个 number 类型值为0时将被解析为 false否则为 true浮点型 0.00也是如此。所以问题1中的 if testage ! null and age ! 当age为0时导致表达式的值为false不进行SQL拼接执行更新后数据库中的数据不会被更新成0。所以对于insert语句拼接也是一样的if testage ! null and age ! 当age为0时导致表达式的值为false不进行SQL拼接导致参数和值传递不一致报错### Error updating database. Cause: java.sql.SQLException: Column count doesnt match value count at row 1### The error may involve com.hrh.mybatis.mapper.PersonMapper.insertSelective-Inline### The error occurred while setting parameters### SQL: insert into tab_person ( id, name, age ) values ( ?, ? )### Cause: java.sql.SQLException: Column count doesnt match value count at row 1bad SQL grammar []; nested exception is java.sql.SQLException: Column count doesnt match value count at row 1insert idinsertSelective parameterTypecom.hrh.mybatis.bean.Person insert into tab_person trim prefix( suffix) suffixOverrides, if testid ! null id, /if if testname ! null name, /if if testage ! null age /if /trim trim prefixvalues ( suffix) suffixOverrides, if testid ! null#{id,jdbcTypeBIGINT}, /if if testname ! null #{name,jdbcTypeVARCHAR}, /if if testage ! null and age ! #{age,jdbcTypeBIGINT} /if /trim /insert2单引号 内如果为单个字符时OGNL 将会识别为Java的char 类型String类型与char 类型做运算时会报错。3.解决办法1number 类型的字段在进行拼接时只需要判断 xxx!null即可。update idupdate parameterTypecom.hrh.mybatis.bean.Person update tab_person set if testid ! null id #{id,jdbcTypeBIGINT}, /if if testname ! null name #{name,jdbcTypeVARCHAR}, /if if testage ! null age #{age,jdbcTypeBIGINT} /if /set where id #{id,jdbcTypeBIGINT} /update2第二种情况有多种解决办法I.需要将单引号和双引号调换即可。select idselectByCondition resultTypecom.hrh.mybatis.bean.Person parameterTypejava.lang.String select include refidBase_Column_List/ from tab_person where if testname ! null and name !a name #{name,jdbcTypeVARCHAR} /if /selectII.添加toString() 进行类型转换select idselectByCondition resultTypecom.hrh.mybatis.bean.Person parameterTypejava.lang.String select include refidBase_Column_List/ from tab_person where if testname ! null and name !a.toString() name #{name,jdbcTypeVARCHAR} /if /selectIII.添加转义实体字符串引号select idselectByCondition resultTypecom.hrh.mybatis.bean.Person parameterTypejava.lang.String select include refidBase_Column_List/ from tab_person where if testname ! null and name ! quot;aquot; name #{name,jdbcTypeVARCHAR} /if /select

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价 →
↑