🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## 1. 统一去除查询中的空格 ```php package com.faw_qm.ad_ops.close_test.config; import com.baomidou.mybatisplus.mapper.Wrapper; import org.apache.ibatis.executor.Executor; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.mapping.SqlCommandType; import org.apache.ibatis.plugin.*; import org.apache.ibatis.session.ResultHandler; import org.apache.ibatis.session.RowBounds; import org.springframework.stereotype.Component; import java.util.Iterator; import java.util.Map; import java.util.Properties; /** * 查询过滤拦截器 */ @Component @Intercepts({@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})}) public class QueryBlankInterceptor implements Interceptor { public Object intercept(Invocation invocation) throws Throwable { Object[] args = invocation.getArgs(); for (int i = 0; i < args.length; i++) { Object arg = args[i]; if (arg instanceof MappedStatement) { MappedStatement ms = (MappedStatement) arg; SqlCommandType sqlCommandType = ms.getSqlCommandType(); if (sqlCommandType != SqlCommandType.SELECT) { break; } } if (arg instanceof Map) { Wrapper ewWrapper = (Wrapper) ((Map) arg).get("ew"); Map paramNameValuePairs = ewWrapper.getParamNameValuePairs(); Iterator<Map.Entry<String, Object>> iterator = paramNameValuePairs.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry<String, Object> next = iterator.next(); if (next.getValue() instanceof String) { //日期只去除两边空格 String dateRex = "\\d{4}[/ \\.-]\\d{1,2}[/ \\.-]\\d{1,2}\\s*.*"; if (((String) next.getValue()).matches(dateRex)) { next.setValue(((String) next.getValue()).trim()); } else { next.setValue(((String) next.getValue()).replaceAll("\\s*", "")); } } } } } return invocation.proceed(); } @Override public Object plugin(Object target) { return Plugin.wrap(target, this); } @Override public void setProperties(Properties properties) { } } ```