mybatis中bind标签和concat的使用

首先,二种方式都可以用来模糊查询,都能预防 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 表 
达式。创建一个 bind 标签的变量后 , 就可以在下面直接使用,使用 bind 拼接字符串不仅可 
以避免因更换数据库而修改 SQL,也能预防 SQL 注入,还能实现多个引用userNameBind

发布了42 篇原创文章 · 获赞 14 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/a116385895/article/details/102956925