首页 > 代码库 > MyBatis做动态模糊查询时,like后面要不要加单引号??

MyBatis做动态模糊查询时,like后面要不要加单引号??

做项目遇到了个奇怪的问题,项目里面要对商品、账户、进行分别的多条件查询,于是我就采用动态多条件分页查询,起初在做账户部分的时候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>

我的问题还真不知道出在哪里!!!奇了怪了,有空再去搞清楚吧 !!!!

MyBatis做动态模糊查询时,like后面要不要加单引号??