【绅士礼仪,先赞后看,鼓励作者,拒绝白嫖】
在springboot中,controller层接受参数时特殊字符(比如:(、‘、\、%、_等)被转为类似与这样& #40的编码,所以我们需要转码,否则直接在数据库中搜索是无法查询到数据的。
package com.mifu.core.util;
import org.apache.commons.lang.StringEscapeUtils;
public class EscapeUtil {
/**
* like查询转码、转义
*
* @param str
* @return
*/
public static String likeEscape(String str) {
if (str != null && !"".equals(str)) {
//转码
str = StringEscapeUtils.unescapeHtml(str.replace("& ","&"));
//转义
return str.replaceAll("[('%_\\\\)]", "\\\\$0");
}
return str;
}
}