欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Mybatis如何解决sql中like通配符模糊匹配问题

程序员文章站 2022-03-09 22:52:39
目录sql中like通配符模糊匹配问题将查询条件通过功能类处理后台contronller获得查询条件mapper.xml中对应的使用方法使用like实现模糊匹配方式一方式二方式三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中对应的使用方法

<if test="param.areaname!=null"> and areaname like '%'||#{param.areaname}||'%' escape '\'</if>

使用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}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。