2025年3月26日 星期三 甲辰(龙)年 月廿五 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > MyBatis

mybatis xml模糊查询

时间:12-11来源:作者:点击数:32
CDSY,CDSY.XYZ

Mybatis中mapper.xml中的模糊查询

  • 方法一: 直接使用 % 拼接字符串
  • 注意:此处不能写成 "%#{name}%" ,#{name}就成了字符串的一部分,
  • 会发生这样一个异常: The error occurred while setting parameters,
  • 应该写成: "%"#{name}"%",即#{name}是一个整体,前后加上%
  • <if test="name != null">
  • name like "%"#{name}"%"
  • </if>
  • <!--方法二: 使用concat(str1,str2)函数将两个参数连接 -->
  • <if test="phone != null">
  • and phone like concat("%",#{phone},"%")
  • </if>
  • <!--方法三: 使用 bind 标签,对字符串进行绑定,然后对绑定后的字符串使用 like 关键字进行模糊查询 -->
  • <if test="email != null">
  • <bind name="pattern" value="'%'+email+'%'"/>
  • and email like #{pattern}
  • </if>
CDSY,CDSY.XYZ
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐