加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在userMapper.xml文件中模糊查询的常用的3种方法

发布时间:2020-12-16 23:45:28 所属栏目:百科 来源:网络整理
导读:在userMapper.xml文件中新建映射sql的标签 !-- ******************** 模糊查询的常用的3种方式:********************* -- select id= " getUsersByFuzzyQuery " parameterType= " User " resultType= " User " select include refid= " columns " / from use
在userMapper.xml文件中新建映射sql的标签

<!-- ******************** 模糊查询的常用的3种方式:********************* -->
    <select id="getUsersByFuzzyQuery" parameterType="User" resultType="User">
        select <include refid="columns"/> from users
        <where>
            <!--
                方法一: 直接使用 % 拼接字符串 
                注意:此处不能写成  "%#{name}%",#{name}就成了字符串的一部分,会发生这样一个异常: The error occurred while setting parameters,应该写成: "%"#{name}"%",即#{name}是一个整体,前后加上%
            -->
            <if test="name != null">
                name like "%"#{name}"%"
            </if>
            <!--方法二: 使用concat(str1,str2)函数将两个参数连接 -->
            <if test="phone != null">
                and phone like concat(concat("%",#{phone}),"%")
            </if>
            <!--方法三: 使用 bind 标签,对字符串进行绑定,然后对绑定后的字符串使用 like 关键字进行模糊查询 -->
            <if test="email != null">
                <bind name="pattern" value="‘%‘+email+‘%‘"/>
                and email like #{pattern}
            </if>
        </where>
    </select>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读