java.sql.SQLSyntaxErrorException: ORA-00909: 参数个数无效

用MyBatis进行多参数模糊查询的时候遇到这个异常,看了下打印日志,发现异常出在预编译之后,插入实参的时候。

 ==>  Preparing: select role_id, role_name, note from t_role where role_name like concat('%', ?, '%') and note like concat('%', ?, '%') 
2018-12-13 20:24:28,567 DEBUG [com.ss.learn.chapter3.mapper.RoleMapper.getRolesByIdAndNote] - ==> Parameters: 1(String), 1(String)

异常提示:参数个数无效。检查了下SQL语句

select role_id, role_name, note from t_role

where role_name like concat('%', ?, '%') and note like concat('%', ?, '%') 

发现问题出现在concat上,concat是连接两个字符串的函数,这里连接了三个,把SQL改成两个concat嵌套的

<select id="getRolesByIdAndNote" parameterType="map" resultType="role">
        select role_id, role_name, note from t_role 
        where role_name like concat(concat('%', #{roleName}), '%')
        and note like concat(concat('%', #{note}), '%')
    </select>

运行成功。

猜你喜欢

转载自blog.csdn.net/weixin_42070871/article/details/84994612