【Mybatis异常】Caused by: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).

一、错误原因分析

从错误提示可以看出:实际传入的参数大于sql中待设置的参数,也就是sql中的?少于参数或?根本没有
产生原因:

 ?号被单引号包围

如:

sql += " and article_title like '%#{articleTitle}%'";

 

二、解决办法

去掉单引号

上面sql改为:

sql += " and article_title like concat('%',#{articleTitle},'%')";

三、范式

    <select id="findAllByKeywords" resultType="CheckItem">
        SELECT <include refid="all"/> from <include refid="table"/>
        <where>
        <if test="keywords != null and keywords.length > 0">
        code like concat('%',#{keywords},'%') or name like concat('%', #{keywords}, '%')
        </if>
        </where>
    </select>

 

posted @ 2021-05-01 21:39  能量熊  阅读(589)  评论(0编辑  收藏  举报