方式一:
使用like运算符
LIKE运算符用于WHERE表达式中,通常与 % 一同使用,类似于一个元字符的搜索(模糊匹配)
使用mybatis<bind />标签
规避更换数据库有些SQL语句可能需要重写,不同数据库之间存在语法差异,比如Oracle中 CONCAT() 只支持两个参数
使用mysql函数 CONCAT_WS(x, s1,s2...sn)
CONCAT With Separator,同 CONCAT(s1,s2...sn) 函数,但是每个字符串之间要加上 x,x 可以是分隔符。CONCAT() 如有任何一个参数为NULL,则返回值为 NULL,CONCAT_WS() 则不会返回
<select id="queryPageList" resultMap="BaseResultMap">
select *
from sys_user
<where>
del_flag = '0'
<if test="keyword != null and keyword != ''">
<!-- bind标签的两个属性都是必选项,name为绑定到上下文的变量名,value为OGNL表达式 -->
<bind name="temp" value="'%' + keyword + '%'"/>
and CONCAT_WS("-", telephone, mail) like #{temp}
</if>
</where>
</select>
方式二:
使用mysql函数 INSTR(str, substr)
INSTR(str, substr) 在指定的字符串(str)中,搜索指定的字符(substr),返回发现指定的字符的位置;
str 被搜索的字符串
substr 希望搜索的字符
使用mysql函数 CONCAT_WS(x, s1,s2...sn)
<select id="queryPageList" resultMap="BaseResultMap">
select *
from sys_user
<where>
del_flag = '0'
<if test="keyword != null and keyword != ''">
and INSTR(CONCAT_WS("-", telephone, mail), #{keyword}) > 0
</if>
</where>
</select>