为什么要把“like '${}%'放在其他条件之后会导致 PersistenceException?

Why put 'like '${}%' after other conditions will cause PersistenceException?

提问人:April_hyq 提问时间:10/31/2023 最后编辑:April_hyq 更新时间:10/31/2023 访问量:40

问:

当我尝试搜索符合要求的数据时,包括 marketCode、类型、is_deleted和邮政编码(如果存在)。如果我使用以下语句,将导致异常。

    @Select(value = {
        "<script>",
        "select * from table1 where ",
        "code = #{condition.code} and type = '3' ",
        "<if test = 'condition.zipcode != null'>",
        "and REPLACE(REPLACE(`zip_code`, '-', ''), ' ', '') like '${condition.zipcode}%' ",
        "</if>",
        "and is_deleted = 0",
        "</script>"
})
List<Result> queryByCondition(@Param("condition") RequestConditionVo conditionVo);

例外情况:

"nested exception is org.apache.ibatis.exceptions.PersistenceException: \n### Error querying database.  Cause: java.lang.IndexOutOfBoundsException: Index 1 out of bounds for length 1\n### Cause: java.lang.IndexOutOfBoundsException: Index 1 out of bounds for length 1"e

但是当我使用以下语句时,它会成功。

@Select(value = {
        "<script>",
        "select * from table1 where ",
        "<if test = 'condition.zipcode != null'>",
        "REPLACE(REPLACE(`zip_code`, '-', ''), ' ', '') like '${condition.zipcode}%' ",
        "</if>",
        "and code = #{condition.code} and type = '3' ",
        "and is_deleted = 0",
        "</script>"
})
List<Result> queryByCondition(@Param("condition") RequestConditionVo conditionVo);

我不明白它为什么有效。

了解为什么两者之间的差异可以以不同的方式工作

Java MyBatis

评论

0赞 drum 10/31/2023
发布完整的错误消息

答: 暂无答案