How to solve the problem of like wildcard fuzzy matching in sql by Mybatis
小编给大家分享一下Mybatis怎么解决sql中like通配符模糊匹配问题,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
sql中like通配符模糊匹配问题
针对oracle数据库:
将查询条件通过功能类处理/** * Description: 处理转义字符%和_,针对ORACLE数据库 * * @param str * @return */ public static String escapeStr(String str) { String temp = ""; for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == '%' || str.charAt(i) == '_') { temp += "\\" + str.charAt(i); } else { temp += str.charAt(i); } } return temp; }后台Contronller获得查询条件
并调用工具类处理
String areaname = request.getParameter("Areaname"); if (areaname != null) { if ("".equals(areaname)) { areaname = null; } else { areaname = StringUtils.escapeStr(areaname); } }mapper.xml中对应的使用方法 and areaname like '%'||#{param.areaname}||'%' escape '\'使用like实现模糊匹配方式一select * from t_user where name like ' %${value}% '方式二select * from t_user where name like '%'||${value}||'%'方式三select * from t_user where name like #{do_it_in_java}以上是"Mybatis怎么解决sql中like通配符模糊匹配问题"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!