zoukankan      html  css  js  c++  java
  • 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>

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

  • 相关阅读:
    linux 操作系统/xxx目录下都是什么文件?
    Linux /bin, /sbin, /usr/bin, /usr/sbin 区别
    java初学
    虚拟机联网及远程连接-Linux基础环境命令学习笔记
    Linux 文件操作命令-Linux基础环境命令学习笔记
    Linux 权限、磁盘操作命令-Linux基础环境命令学习笔记
    Linux shell编程命令-Linux基础环境命令学习笔记
    C程序编译执行过程
    刨根问底:什么是yum源,yum的工作原理又是什么
    网关人性化详解
  • 原文地址:https://www.cnblogs.com/zyh1994/p/5869414.html
Copyright © 2011-2022 走看看