這里記錄一下個人犯過的錯誤。
首先是寫一個不能執行的代碼:
<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>