Mysql 字段模糊查询,在页面中输入%查询全部的问题处理
一、背景
测试小妹闲着无聊,针对某一个查询项进行“%”测试,正常查询效果应该是返回空数据,但是却查出所有的数据。
二、解决方案
1、在使用mybatis的模糊查询时,有两个特殊符号需要注意:
%(百分号):相当于任意多个字符;
_(下划线):相当于任意的单个字符;
2、处理方法:
1:(查询条件参数,比如”xx%_x”)param.replaceAll(“%”, “/%”).replaceAll(“-”, “/-”)
2:select * from table where column like concat(’%’,#{param},’%’) escape ‘/’;
处理之后百分号%、下划线_在mybatis执行该拼接的sql语句的时候就不会被转义处理了
escape ‘/’ 指用’/’说明后面的%或_就不作为通配符而是普通字符了,注意前面没有转义字符的%仍然起通配符作用
like concat(’%’,#{param},’%’) 、like ‘%${param}%’ 、 like ‘%’||#{param}||’%’是一个意思;
3、转译工具类
public class EscapeUtil {
/**
* mysql的模糊查询时特殊字符转义(条件查询%或者_查询所有问题)
*/
public static String escapeChar(String string){
if(StringUtils.isNotBlank(string)){
string = string.replaceAll("_", "/_");
string = string.replaceAll("%", "/%");
}
return string.trim() ;
}
}
三、实战
1、实战一
假设User有一个name字段
此时在查询数据前需要做一次转译
user.setName(EscapeUtil.escapeChar(user.getName()));
在Mapper文件需要如下编码
NAME LIKE CONCAT('%',CONCAT(#{condition.name},'%')) escape '/'
2、实战二:切面、注解
1、Annotation :用于标注在方法上
/** * 功能: 处理模糊查询中的特殊符号 ** ────────────────────────────────────────── * version 变更日期 修改人 修改说明 * ------------------------------------------ * V1.0.0 2023/03/29 CHY 初版 * ────────────────────────────────────────── */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Annotation { boolean isTrue() default true; }
2、EncryptField 注解:用于标注在模糊查询字段上
/** * 功能: 对模糊查询字段进行处理 ** ────────────────────────────────────────── * version 变更日期 修改人 修改说明 * ------------------------------------------ * V1.0.0 2023/03/29 CHY 初版 * ────────────────────────────────────────── */ @Target({ElementType.FIELD,ElementType.PARAMETER}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface EncryptField { String [] values() default ""; }
3、MybatisSpecialCharactersAspect:Mybatis特殊字符处理切面
import cn.hutool.core.util.ObjectUtil; import com.chy.exception.BusinessException; import com.chy.Interceptor.annotation.Annotation; import com.chy.Interceptor.annotation.EncryptField; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.Signature; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.stereotype.Component; import java.lang.reflect.Field; import java.lang.reflect.Method; /** * 功能: Mybatis特殊字符处理切面 ** ────────────────────────────────────────── * version 变更日期 修改人 修改说明 * ------------------------------------------ * V1.0.0 2023/03/29 CHY 初版 * ────────────────────────────────────────── */ @Component @Aspect public class MybatisSpecialCharactersAspect { @Pointcut(value = "@annotation(com.chy.Interceptor.annotation.Annotation)") public void execyteAspectj() { } @Around(value = "execyteAspectj();") public Object around(ProceedingJoinPoint joinPoint) throws Throwable { Signature signature = joinPoint.getSignature(); if (!(signature instanceof MethodSignature)) { throw new BusinessException("user permission check fail,stop this request!"); } Object proceed; MethodSignature methodSignature = (MethodSignature) signature; Object target = joinPoint.getTarget(); Method declaredMethod = target.getClass().getDeclaredMethod(methodSignature.getName(), methodSignature.getParameterTypes()); Annotation annotation = declaredMethod.getAnnotation(Annotation.class); if (ObjectUtil.isNull(annotation) || !annotation.isTrue()) { return proceed = joinPoint.proceed(); } else { Object[] args = joinPoint.getArgs(); for (Object param : args) { if (ObjectUtil.isNotEmpty(param)) { Field[] declaredFields = param.getClass().getDeclaredFields(); for (Field filed : declaredFields) { boolean annotationPresent = filed.isAnnotationPresent(EncryptField.class); if (annotationPresent) { filed.setAccessible(true); Object o = filed.get(param); if (o == null) { continue; } filed.set(param, o.toString().replaceAll("/", "//").replaceAll("%", "/%").replaceAll("_", "/_")); } } } } proceed = joinPoint.proceed(); } return proceed; } }
4、应用
1、user类的name字段上使用 @EncryptField 注解
class User{
@EncryptField
private String name;
}
2、查询方法加上@Annotation注解
@Annotation
public PageInfo selectUserPage(User user) {
}
3、在Mapper文件需要如下编码
NAME LIKE CONCAT('%',CONCAT(#{condition.name},'%')) escape '/'
本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://net2asp.com/03b3784709.html
