Mybatis bind标签与concat函数如何正确结合使用?
- 内容介绍
- 文章标签
- 相关推荐
本文共计534个文字,预计阅读时间需要3分钟。
首先,两种方式都可以用于模糊查询和预防SQL注入。但在更换数据库的情况下,推荐使用bind标签。以下是一个简化的示例:
sqlif test=userName !=null and userName !='' and userName like concat('%', #{userName}, '%')
注意:这里使用了`#{userName}`来代替直接拼接的`userName`值,以避免SQL注入。
首先,二种方式都可以用来模糊查询,都能预防 SQL 注入。但是在更换数据库情况下,bind标签通用。
<if test=” userName != null and userName !=””> and userName like concat('%' ,#{userName},'%') </if>
使用concat函数连接字符串,在mysql中这个函数支持多个参数,但是在oracle中这个函数只支持2个参数,由于不同数据库之间的语法差异,更换数据库,这些语法就需要重写。可以用bind标签来避免更换数据库所带来的一些麻烦。
bind 标签可以使用 OGNL 表达式创建一个变量井将其绑定到上下文中。
<bind name= " userNameBind ” value = ”' % '+ userNarne + ' %'” /> <if test=” userName != null and userName !=””> and userName like #{userNameBind} </if>
bind 标签的两个属性都是必选项, name 为绑定到上下文的变量名, value 为 OGNL 表达式。
本文共计534个文字,预计阅读时间需要3分钟。
首先,两种方式都可以用于模糊查询和预防SQL注入。但在更换数据库的情况下,推荐使用bind标签。以下是一个简化的示例:
sqlif test=userName !=null and userName !='' and userName like concat('%', #{userName}, '%')
注意:这里使用了`#{userName}`来代替直接拼接的`userName`值,以避免SQL注入。
首先,二种方式都可以用来模糊查询,都能预防 SQL 注入。但是在更换数据库情况下,bind标签通用。
<if test=” userName != null and userName !=””> and userName like concat('%' ,#{userName},'%') </if>
使用concat函数连接字符串,在mysql中这个函数支持多个参数,但是在oracle中这个函数只支持2个参数,由于不同数据库之间的语法差异,更换数据库,这些语法就需要重写。可以用bind标签来避免更换数据库所带来的一些麻烦。
bind 标签可以使用 OGNL 表达式创建一个变量井将其绑定到上下文中。
<bind name= " userNameBind ” value = ”' % '+ userNarne + ' %'” /> <if test=” userName != null and userName !=””> and userName like #{userNameBind} </if>
bind 标签的两个属性都是必选项, name 为绑定到上下文的变量名, value 为 OGNL 表达式。

