zoukankan      html  css  js  c++  java
  • mybatis bind标签

    开门见山的说,平时写模糊查询,一直用${name},例如:

    select * from table where name like '%${name}%'

    后来知道了,这样写可能会引发sql注入,于是乎,要用到这样一个标签 bind,经过改正上面的sql可以变成

    <bind name="bindeName" value="'%'+name+'%'"/>
    SELECT * FROM table where name like #{bindeName}

    大致就上面这个意思,不要在意一些细节。就相当于在bind标签中的value值中,把需要的字符拼接好,然后用name中的值去代替拼接好的参数。

    这个用法同样用于一些防止更换数据库的sql中。例如:

    concat标签,在mysql中,可以拼接多个字符,但是如果有一天,你的数据库突然变成了oracle,concat就会报错了,因为它只能接受两个参数。

    这个时候,bind同样适用,如下:

    开始的时候:

    <if test=” userName != null and userName ! = ””>
      and username like concat ( '1',#{userName},'2'</if>

    可以改成:

    <if test=” userName != null and userName !=””> 
      <bind name= " userNameLike ” value = ”'1'+ userName + '2'”/>
      and username like #{userNameLike} 
    </if>  
  • 相关阅读:
    Python中 sys.argv[]的用法简明解释
    python多线程
    python 多进程
    shell----bash
    linux crontab
    Elastic search 概述
    Elastic search 入门
    Elastic search CURL命令
    Elastic search 基本使用
    Elastic search 字段折叠 collaose
  • 原文地址:https://www.cnblogs.com/bpjj/p/11880424.html
Copyright © 2011-2022 走看看