记一次处理Mybatis遇到的问题很恶心很恶心的一个问题但是我没想到会是这种地方有问题
具体报错内容如下 大概的意思就是传了个String但是程序认为这字段是数字,
所以就报了这错
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.lang.NumberFormatException: For input string: "606f0daa79d603bb2d6ece4e235b2279"
### Cause: java.lang.NumberFormatException: For input string: "606f0daa79d603bb2d6ece4e235b2279"
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:96)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:441)
at com.sun.proxy.$Proxy157.selectList(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:224)
at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.executeForMan
巴拉巴一大堆
<select id="selectUserList" parameterType="SysUser" resultMap="SysUserResult">
select u.user_id, u.dept_id, u.nick_name, u.user_name, u.email, u.avatar, u.phonenumber, u.password, u.sex,
u.status, u.del_flag, u.login_ip, u.login_date, u.create_by, u.create_time, u.remark, d.dept_name, d.leader from
sg_sys_user u
left join sg_sys_dept d on u.dept_id = d.dept_id
where u.del_flag = '0'
<if test="userId != null and userId != '0' ">
AND u.user_id = #{userId}
</if>
<if test="userName != null and userName != ''">
AND u.user_name like concat('%', #{userName}, '%')
</if>
<if test="status != null and status != ''">
AND u.status = #{status}
</if>
<if test="phonenumber != null and phonenumber != ''">
AND u.phonenumber like concat('%', #{phonenumber}, '%')
</if>
<if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
AND date_format(u.create_time,'%y%m%d') >= date_format(#{params.beginTime},'%y%m%d')
</if>
<if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
AND date_format(u.create_time,'%y%m%d') <= date_format(#{params.endTime},'%y%m%d')
</if>
<if test="deptId != null and deptId != '0'.toString()">
AND (u.dept_id = #{deptId} OR u.dept_id IN ( SELECT t.dept_id FROM sg_sys_dept t WHERE find_in_set(#{deptId},
ancestors) ))
</if>
</select>
问题就处在
<if test="userId != null and userId != '0' ">
AND u.user_id = #{userId}
</if>
这个动态sql判断条件的userId != ‘0’ 程序把这字段当成数字了,然后我改成了
<if test="userId != null and userId != '0'.toString() ">
AND u.user_id = #{userId}
</if>
这样 系统就不报错了 意思就是把0转成字符串
是不是很恶心 谁特么能想的到会是这种问题