mybatis查询的bind标签

 工作中用到了这个:

    <select id="find" resultMap="BaseResultMap">
        SELECT <include refid="Base_Column_List" />
        FROM mal_merch_allc t
        <where>
            <if test="s.merchName != null and s.merchName != ''">
                <bind name="likeValue1"
                    value="@com.hbis.ttie.core.mybatis.util.OgnlUtils@toFuzzyValue(s.merchName)"></bind>
                and t.merch_name like #{likeValue1,jdbcType=VARCHAR}
            </if>
            <if test="s.shop != null and s.shop != ''">
                and t.shop = #{s.shop,jdbcType=VARCHAR}
            </if>
        </where>
    </select>

总结一下:

在进行模糊查询时,如果使用“${}”拼接字符串,则无法防止 SQL 注入问题。

select * from user where username like '%${name}%'

如果使用字符串拼接函数或连接符号,但不同数据库的拼接函数或连接符号不同。 

使用 concat 函数连接字符串,在 MySQL 中,这个函数支持多个参数,但在 Oracle 中只支持两个参数。由于不同数据库之间的语法差异 ,如果更换数据库,有些 SQL 语句可能就需要重写。

select * from user where username like concat ( '1',#{userName},'2' )

针对这种情况,可以使用 bind 标签来避免由于更换数据库带来的一些麻烦。

select * from user 
<where>
    <if test=" userName != null and userName !='' ">
        <bind name="userNameLike" value=" '1'+ userName + '2' " />
        and username like #{userNameLike} 
    </if>
</where>

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值