软件编程
位置:首页>> 软件编程>> java编程>> Mybatis如何解决sql中like通配符模糊匹配问题

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

作者:时间辜负了谁  发布时间:2023-12-22 19:39:52 

标签: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中对应的使用方法

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

来源:https://blog.csdn.net/kpchen_0508/article/details/48496515

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com