最近测试提了一个有意思的BUG,条件查询时,输入"%","\","_"查询时,会查出所有的数据,这是因为它们是通配符和特殊字符,在条件查询中具有特殊的含义。
-
% (百分号): 在SQL中,% 表示零个或多个字符的通配符。例如,如果你使用
WHERE column_name LIKE '%abc%'
,它将匹配任何位置包含 “abc” 的值。 -
_ (下划线): 在SQL中,_ 表示单个字符的通配符。例如,如果你使用
WHERE column_name LIKE 'a_c'
,它将匹配 “abc”、“adc” 等。 -
\ (反斜杠): 在某些SQL实现中,反斜杠可能用作转义字符,用于指示下一个字符是普通字符而不是通配符。例如,如果要匹配实际的百分号或下划线,而不是作为通配符使用,可以使用反斜杠进行转义,如
LIKE '100\%'
如果你在条件查询中直接使用这些字符而不进行转义或处理,SQL解释器会将它们视为通配符,而不是普通字符。因此,查询将返回与所有数据匹配的结果,因为这些通配符匹配几乎任何字符串。如果你要匹配这些字符本身,而不是作为通配符使用,应该使用转义字符(通常是反斜杠 “\”) 来明确指示它们是普通字符。
话不多说直接上代码,可以直接复制使用。
1. 核心代码,拦截查询请求,分析转义特殊字符
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Collection;
import java.util.HashMap;
import java.util.Properties;
import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
/**
* 自定义拦截器方法,处理模糊查询中包含特殊字符(_、%、\)
*
* @author 10JQKA
* @version 1.0
*/
@Intercepts({
@Signature(type = Executor.class,
method = "query",
args = {MappedStatement.class,
Object.class,
RowBounds.class,
ResultHandler.class}),
@Signature(type = Executor.class,
method = "query",
args = {MappedStatement.class,
Object.class,
RowBounds.class,
ResultHandler.class,
CacheKey.class,
BoundSql.class})})
public class MyBatisInterceptor implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
// 拦截sql
Object[] args = invocation.getArgs();
MappedStatement statement = (MappedStatement) args[0];
// 请求参数对象
Object parameterObject = args[1];
// 获取 SQL
SqlCommandType sqlCommandType = statement.getSqlCommandType();
if (SqlCommandType.SELECT.equals(sqlCommandType)) {
if (parameterObject instanceof HashMap) {
// 调用特殊字符处理方法
HashMap hash = (HashMap) parameterObject;
hash.putAll(specialCharacterEscape(hash));
} else if (isEntityClass(parameterObject)) {
HashMap hash = convertEntityToMap(parameterObject);
hash.putAll(specialCharacterEscape(hash));
// 将转义后的值重新设置到实体对象的属性中
for (Field field : parameterObject.getClass().getDeclaredFields()) {
field.setAccessible(true);
String fieldName = field.getName();
if (hash.containsKey(fieldName)) {
field.set(parameterObject, hash.get(fieldName));
}
}
}
}
// 继续执行
return invocation.proceed();
}
@Override
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
@Override
public void setProperties(Properties properties) {
}
//判断是否是实体类
public boolean isEntityClass(Object obj) {
if (obj == null || obj instanceof String || obj instanceof Integer || obj instanceof Long
|| obj instanceof Double || obj instanceof Float || obj instanceof Character) {
return false; // 如果对象为null或者属于基本数据类型,则不是实体类
}
if (obj instanceof Collection) {
return false; // 如果是集合类型,则直接返回false
}
if (obj.getClass().getSimpleName().startsWith("Page")) {
return false; // 如果对象类型是以"Page"开头的参数化类型,则直接返回false
}
Field[] fields = obj.getClass().getDeclaredFields();
for (Field field : fields) {
if (!Modifier.isStatic(field.getModifiers()) && !Modifier.isFinal(field.getModifiers())) {
return true; // 如果有非静态、非final的字段,则认为是实体类
}
}
return false; // 如果没有非静态、非final的字段,则不是实体类
}
//实体类转HashMap
public HashMap<String, Object> convertEntityToMap(Object obj) throws IllegalAccessException {
HashMap<String, Object> map = new HashMap<>();
Field[] fields = obj.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
Object value = field.get(obj);
map.put(field.getName(), value);
}
return map;
}
public HashMap specialCharacterEscape(HashMap hash) throws IllegalAccessException {
hash.forEach((k, v) -> {
// 仅拦截字符串类型且值不为空
if (v instanceof String) {
String value = (String) v;
if (value.equals("\\_") || value.equals("\\%") || value.equals("\\\\")) {
hash.put(k, value);
return;
}
if (value.equals("\u0000")){
return;
}
value = value.replaceAll("\\\\", "\\\\\\\\");
value = value.replaceAll("_", "\\\\_");
value = value.replaceAll("%", "\\\\%");
// 请求参数对象HashMap重新赋值转义后的值
hash.put(k, value);
}
});
return hash;
}
}
2. 配置类,可以放在你自己的相关配置类中
/**
* mybatis 配置类
*
* @author 10JQKA
* @version 1.0
*/
@Configuration
public class MybatisConfig {
/**
* mybatis 自定义拦截器(特殊字符处理)
*
*/
@Bean
public MyBatisInterceptor getEscapeInterceptor() {
return new MyBatisInterceptor();
}
/**
* 分页对象实列化
*
*/
@Bean
public PageHelper pageHelper() {
PageHelper pageHelper = new PageHelper();
Properties p = new Properties();
p.setProperty("offsetAsPageNum", "true");
p.setProperty("rowBoundsWithCount", "true");
p.setProperty("reasonable", "true");
p.setProperty("dialect", "mysql");
pageHelper.setProperties(p);
return pageHelper;
}
}
总结: 目前测试几乎适用所有查询场景,如果有不适应的地方欢迎大家评论留言,看到后我会修改优化,也欢迎大家对其提出建议,互相共勉。