SpringBoot接收查询参数,Mysql模糊查询包含%_等特殊字符解决⽅案%_ 是MySql中的特殊字符,如需对这⼏个字符在Mysql中进⾏模糊搜索则需要进⾏转义处理 \_\%
SpringBoot接收查询参数,然后拦截做特殊处理
注解
@Target({METHOD, FIELD, CONSTRUCTOR, PARAMETER})
@Retention(RUNTIME)
public @interface LikeCondition {
}
切⾯
@Aspect
@Component
@Slf4j
public class LikeConditionAspect {好听的网名
@Pointcut(value = "@annotation(annotation.LikeCondition)") public void pointcut() {
}
@Around(value = "pointcut() ")
public Object inject(ProceedingJoinPoint pjp) throws Throwable {
Object[] args = Args();
if(args.length>0){
/**
* 处理 @RequestBody 和对象传参等
语言英语怎么说*/
for (int i = 0; i < args.length; i++) {长江主要支流
Class<?> aClass = args[i].getClass();
Field[] declaredFields = DeclaredFields();
for (Field declaredField : declaredFields) {
沙怪LikeCondition annotation = Annotation(LikeCondition.class);
if(annotation != null){
declaredField.tAccessible(true);
Object field = Field(declaredField,args[i]);
ReflectionUtils.tField(declaredField,args[i],replaceLikeCondition(field));高一议论文
}
}
}
/
一根头发系石磨歇后语
**
* 处理 @RequestParam @PathVariable 相关的参数
*/
MethodSignature signature = (MethodSignature) Signature();
Parameter[] parameters = Method().getParameters();禹敏
for (int i = 0; i < parameters.length; i++) {
Parameter parameter=parameters[i];
LikeCondition annotation = Annotation(LikeCondition.class);
if(annotation != null){
args[i] = replaceLikeCondition(args[i]);
}
}
}
// result的值就是被拦截⽅法的返回值
Object result = pjp.proceed(args);
return result;
}
static Object replaceLikeCondition(Object arg){
if(arg instanceof String){
String argStr = String();
良禽择木return placeAll("%","\\\\\\%")
.replaceAll("_","\\\\\\_");
}
return arg;
}
}
使⽤
在请求⽅法前和 Req中的具体参数上 加上 注解 @LikeCondition