做項目遇到了個奇怪的問題,項目里面要對商品、賬戶、進行分別的多條件查詢,於是我就采用動態多條件分頁查詢,起初在做賬戶部分的時候Mybatis是這樣寫的
<!-- 動態多條件分頁查詢 --> <select id="searchPageUseDyc" parameterType="page" resultMap="accountResultMap"> select acc_id,acc_login,acc_name,acc_pass from account <where> <if test="paramsEntity.accId!=null"> and acc_id like #{paramsEntity.accId} </if> <if test="paramsEntity.accLogin!=null"> and acc_login like #{paramsEntity.accLogin} </if> <if test="paramsEntity.accName!=null"> and acc_name like #{paramsEntity.accName} </if> <if test="paramsEntity.accPass!=null"> and acc_pass like #{paramsEntity.accPass} </if> </where> limit #{start},#{rows} </select>
like 后面直接跟 #{paramsEntity.accName} 不需要添加單引號
然后完成商品查詢的時候我一樣寫了一套
<!-- 動態分頁多條件查詢(找內容) --> <select id="searchPageUseDyc" parameterType="page" resultMap="goodsResultMap"> select goods_Id,goods_name,goods_unit,goods_type,goods_color,goods_store,goods_limit,goods_commission,goods_producer,goods_remark,goods_sel_price,goods_buy_price from goods <where> <if test="paramsEntity.goodsId!=null">
and goods_Id like ${paramsEntity.goodsId}
</if> <if test="paramsEntity.goodsName!=null">
and goods_name like ${paramsEntity.goodsName}
</if> <if test="paramsEntity.goodsUnit!=null">
and goods_unit like ${paramsEntity.goodsUnit}
</if> <if test="paramsEntity.goodsType!=null">
and goods_type like ${paramsEntity.goodsType}
</if> <if test="paramsEntity.goodsColor!=null">
and goods_color like ${paramsEntity.goodsColor}
</if> <if test="paramsEntity.goodsStore!=null">
and goods_store like ${paramsEntity.goodsStore}
</if> <if test="paramsEntity.goodsLimit!=null">
and goods_limit like ${paramsEntity.goodsLimit}
</if> <if test="paramsEntity.goodsCommission!=null">
and goods_commission like ${paramsEntity.goodsCommission}
</if> <if test="paramsEntity.goodsProducer!=null">
and goods_producer like ${paramsEntity.goodsProducer}
</if> <if test="paramsEntity.goodsRemark!=null">
and goods_remark like ${paramsEntity.goodsRemark}
</if> <if test="paramsEntity.goodsSelPrice!=null">
and goods_sel_price like ${paramsEntity.goodsSelPrice}
</if> <if test="paramsEntity.goodsBuyPrice!=null">
and goods_buy_price like ${paramsEntity.goodsBuyPrice}
</if> </where> limit #{start},#{rows} </select>
但是運行報錯了!!!
錯誤信息You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%% limit 0,3' at line 3
然后我就給單引號添加上了,然后居然就成了,代碼這樣子
<!-- 動態分頁多條件查詢(找內容) --> <select id="searchPageUseDyc" parameterType="page" resultMap="goodsResultMap"> select goods_Id,goods_name,goods_unit,goods_type,goods_color,goods_store,goods_limit,goods_commission,goods_producer,goods_remark,goods_sel_price,goods_buy_price from goods <where> <if test="paramsEntity.goodsId!=null">
and goods_Id like '${paramsEntity.goodsId}'
</if> <if test="paramsEntity.goodsName!=null">
and goods_name like '${paramsEntity.goodsName}'
</if> <if test="paramsEntity.goodsUnit!=null">
and goods_unit like '${paramsEntity.goodsUnit}'
</if> <if test="paramsEntity.goodsType!=null">
and goods_type like '${paramsEntity.goodsType}'
</if> <if test="paramsEntity.goodsColor!=null">
and goods_color like '${paramsEntity.goodsColor}'
</if> <if test="paramsEntity.goodsStore!=null">
and goods_store like '${paramsEntity.goodsStore}'
</if> <if test="paramsEntity.goodsLimit!=null">
and goods_limit like '${paramsEntity.goodsLimit}'
</if> <if test="paramsEntity.goodsCommission!=null">
and goods_commission like '${paramsEntity.goodsCommission}'
</if> <if test="paramsEntity.goodsProducer!=null">
and goods_producer like '${paramsEntity.goodsProducer}'
</if> <if test="paramsEntity.goodsRemark!=null">
and goods_remark like '${paramsEntity.goodsRemark}'
</if> <if test="paramsEntity.goodsSelPrice!=null">
and goods_sel_price like '${paramsEntity.goodsSelPrice}'
</if> <if test="paramsEntity.goodsBuyPrice!=null">
and goods_buy_price like '${paramsEntity.goodsBuyPrice}'
</if> </where> limit #{start},#{rows} </select>
然后我就去查文檔,光放文檔給出的也是不用加單引號的!!
<select id=”findActiveBlogLike” parameterType=”Blog” resultType=”Blog”> SELECT * FROM BLOG <where> <if test=”state != null”> state = #{state} </if> <if test=”title != null”> AND title like #{title} </if> <if test=”author != null and author.name != null”> AND title like #{author.name} </if> </where> </select>
我的問題還真不知道出在哪里!!!奇了怪了,有空再去搞清楚吧 !!!!