使用bind进行动态模糊查询

转自:https://blog.csdn.net/yangshangwei/article/details/80073978

https://www.cnblogs.com/kangkaii/p/8421837.html

概述

bind标签可以使用OGNL表达式创建一个变量并将其绑定到上下文中。

用法

<select id="selectSysUsersAdvancedWithWhere" resultType="com.artisan.mybatis.xml.domain.SysUser">
        SELECT
            a.id,
            a.user_name userName,
            a.user_password userPassword,
            a.user_email userEmail,
            a.user_info userInfo,
            a.head_img headImg,
            a.create_time createTime
        FROM
            sys_user a
        <where>
            <if test="userName != null and userName != '' ">
                and user_name like concat('%',#{userName},'%')
            </if>
            <if test="userEmail != null and userEmail != '' ">
                and user_email = #{userEmail}
            </if>
        </where>
    </select>

使用concat函数连接字符串,在MySQL中,这个函数支持多个参数,但是在Oracle中只支持两个参数,CONCAT(char1 , char2)|| 可以无限拼接。由于不同数据库之间的语法差异,如果更换了数据库,有些SQL语句可能就需要重写。 针对这种情况,可以使用bind标签来避免由于更换数据库带来的一些麻烦。 我们将上面的语句改为bind方式,如下

<select id="selectSysUserByAdvancedCondition" resultType="com.artisan.mybatis.xml.domain.SysUser">
        SELECT
            a.id,
            a.user_name userName,
            a.user_password userPassword,
            a.user_email userEmail,
            a.user_info userInfo,
            a.head_img headImg,
            a.create_time createTime
        FROM
            sys_user a
        <where>
            <if test="userName != null and userName != '' ">
                <!-- and user_name like concat('%',#{userName},'%') -->
                <bind name="userNameLike" value=" '%' + userName + '%' "/>
                    and user_name like #{userNameLike}
            </if>
            <if test="userEmail != null and userEmail != '' ">
                and user_email = #{userEmail}
            </if>
        </where>
    </select>   

 

bind标签的两个属性都是不选项,name为绑定到上下文的变量名,value为OGNL表达式,创建一个bind标签后,就可以在下面直接使用了。 使用bind拼接字符串不仅可以避免因更换数据库而修改SQL,也能预防SQL注入。

实例

1、接口方法

/**
     * 
     * 
     * @Title: selectSysUserByAdvancedCondition
     * 
     * @Description: 演示bind用法
     * 
     * @param sysUser
     * @return
     * 
     * @return: List<SysUser>
     */
    List<SysUser> selectSysUserByAdvancedCondition(SysUser sysUser);

2、动态sql

<select id="selectSysUserByAdvancedCondition" resultType="com.artisan.mybatis.xml.domain.SysUser">
        SELECT
            a.id,
            a.user_name userName,
            a.user_password userPassword,
            a.user_email userEmail,
            a.user_info userInfo,
            a.head_img headImg,
            a.create_time createTime
        FROM
            sys_user a
        <where>
            <if test="userName != null and userName != '' ">
                <!-- and user_name like concat('%',#{userName},'%') -->
                <bind name="userNameLike" value=" '%' + userName + '%' "/>
                    and user_name like #{userNameLike}
            </if>
            <if test="userEmail != null and userEmail != '' ">
                and user_email = #{userEmail}
            </if>
        </where>
    </select>   

总结:oracle中常用的模糊查询SQL

方法1:使用bind进行动态查询

<if test="wName != null and wName != ''">
      <bind name="wNameLike" value=" '%' + wName + '%' "/>
      AND a.wname LIKE #{wNameLike}
</if>

方法2:使用||连接符

<if test="wName != null and wName != ''">
    AND a.wname LIKE  '%' || #{wName} || '%'
</if>

方法3:使用concat

<if test="userName != null and userName != '' ">
    and user_name like concat('%',#{userName},'%')    
</if>

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值