这里记录一下个人犯过的错误。
首先是写一个不能执行的代码:
<select id="getAll" resultMap="BaseResultMap"> SELECT <include refid="Base_Column_List" /> FROM t_menu <where> <if test="title != null and title != ''"> and title like concat('%', #{title}, '%') </if> <if test="deleted != null and deleted != ''"> and deleted = #{deleted} </if> <if test="dateStar != null and dateStar != ''"> and gmt_create >=#{dateStar} </if> <if test="dateEnd != null and dateEnd != ''"> and gmt_create <=#{dateEnd} </if> </where> ORDER BY pid ASC <if test="pageIndex != null and pageIndex != ''"> limit #{(pageIndex-1)*pageSize},#{pageIndex*pageSize} </if> </select>
limit字句中是不允许运算的,而#{}表示的是一个占位符,所以报错sql语句放到编译器历名也不能执行。
解决方案:
将#{}变成${},也就是相当于limit后面的值是定值,sql语句是拼接而成的而不是占位符赋值运算:
<select id="getAll" resultMap="BaseResultMap"> SELECT <include refid="Base_Column_List" /> FROM t_menu <where> <if test="title != null and title != ''"> and title like concat('%', #{title}, '%') </if> <if test="deleted != null and deleted != ''"> and deleted = #{deleted} </if> <if test="dateStar != null and dateStar != ''"> and gmt_create >=#{dateStar} </if> <if test="dateEnd != null and dateEnd != ''"> and gmt_create <=#{dateEnd} </if> </where> ORDER BY pid ASC <if test="pageIndex != null and pageIndex != ''"> limit ${(pageIndex-1)*pageSize},${pageIndex*pageSize} </if> </select>