mybatis的模糊查询功能使用的很广泛,以MySQL数据库为例(不同的数据库,有些可能不支持)
常用的模糊查询有三种方法:
- 直接使用 % 拼接字符串,如
"%"#{name}"%",双引号。(注不能使用单引号'%'#{name}'%')。
例如:<if test="name != null and name !='' "> name like "%"#{name}"%" </if>注意:此处不能写成 "%#{name}%" ,#{name}就成了字符串的一部分。
- 使用concat(str1,str2)函数拼接
<if test="phone != null"> and phone like concat('%',#{phone},'%') </if> - 使用mybatis的bind标签
<if test="email != null"> <bind name="pattern" value="'%'+email+'%'"/> and email like #{pattern} </if>
本文介绍了在MyBatis中实现模糊查询的三种方法:直接使用%拼接字符串、使用concat函数以及使用bind标签,并提供了具体的XML配置示例。
813

被折叠的 条评论
为什么被折叠?



