spring aop 解决模糊查询参数 % - /等特殊符号问题

import com.hsq.common.utils.StringUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;


@Component
@Aspect
public class FuzzyQueryAspect {

@Around("@annotation(com.hsq.common.aop.FuzzyQuery)")
public Object proceed(ProceedingJoinPoint joinPoint)
throws Throwable {
Object[] args = joinPoint.getArgs();
for (Object arg : args) {
Field[] fields = arg.getClass().getDeclaredFields();
for (Field field : fields) {
FuzzyQuery fuzzyQuery = field.getAnnotation(FuzzyQuery.class);
if (fuzzyQuery != null) {
field.setAccessible(true);
String value = field.get(arg) == null ? null : String.valueOf(field.get(arg));
value = StringUtil.escapeSpecialChar(value);
field.set(arg, value);
}
}
}
return joinPoint.proceed();
}

}



StringUtil
public  static String escapeSpecialChar(String keyword){
log.info("input:"+keyword);
if (StringUtils.isNotBlank(keyword)) {
String[] fbsArr = { "\\", "$", "|","%","_" , "(", ")", "*", "+", ".", "[", "]", "?", "^", "{", "}"};
for (String key : fbsArr) {
if (keyword.contains(key)) {
log.info("escapeSpecialChar:"+key);
keyword = keyword.replace(key, "\\" + key);
}
}
}
log.info("output:"+keyword);
return keyword;
}


@Target({ElementType.FIELD,ElementType.PARAMETER,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface FuzzyQuery {



}


使用
@FuzzyQuery
private String keyWord;//关键字查询

controller也加上@FuzzyQuery

转载于:https://www.cnblogs.com/hsq666666/p/11002442.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值