首页 > 其他 > 详细

Mybatis--模糊查询 Like

时间:2021-08-15 23:02:38      阅读:25      评论:0      收藏:0      [点我收藏+]

Mybatis--模糊查询 Like

  1. 第一种(个人常用):使用CONCAT()函数

    <select id="selectPageList" resultType="com.shop.cms.category.vo.CmsCategoryListVO">
        SELECT
        <include refid="pageListVO"></include>
        FROM cms_content_category
        <where>
           <if test="param.categoryName != null and param.categoryName != ‘‘">AND `name` LIKE CONCAT(‘%‘, #{param.categoryName}, ‘%‘)</if>
        </where>
    </select>
  2. 第二种:使用#{}

    <select id="selectPageList" resultType="com.shop.cms.category.vo.CmsCategoryListVO">
        SELECT
        <include refid="pageListVO"></include>
        FROM cms_content_category
        <where>
           <if test="param.categoryName != null and param.categoryName != ‘‘">AND `name` LIKE #{param.categoryName}</if>
        </where>
    </select>

     注意:因为#{...}解析成sql语句时候,会在变量外侧自动加单引号‘  ‘,所以这里 % 需要使用双引号"  ",不能使用单引号 ‘  ‘,不然会查不到任何结果。

  3. 第三种:使用${}

    <select id="selectPageList" resultType="com.shop.cms.category.vo.CmsCategoryListVO">
        SELECT
        <include refid="pageListVO"></include>
        FROM cms_content_category
        <where>
           <if test="param.categoryName != null and param.categoryName != ‘‘">AND `name` LIKE ‘%${param.categoryName}%‘</if>
        </where>
    </select>

    注意:由于$是参数直接注入的,导致这种写法,大括号里面不能注明jdbcType,不然会报错 

  弊端:可能会引起sql的注入,平时尽量避免使用${...}

Mybatis--模糊查询 Like

原文:https://www.cnblogs.com/Yaoson-Heyi/p/15141553.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!