If the field stored in our database contains wildcard characters for the two like queries of "%_", then "%_" is not queried as a keyword when queried, because MyBatis will treat the two characters as wildcards. The workaround is to be able to add the escape character
1. Define an interceptor to add an escape character if the string to be queried contains "%_"
ImportOrg.apache.ibatis.executor.Executor;Importorg.apache.ibatis.mapping.MappedStatement;Importorg.apache.ibatis.mapping.ParameterMapping;Importorg.apache.ibatis.plugin.*;ImportOrg.apache.ibatis.session.ResultHandler;Importorg.apache.ibatis.session.RowBounds;ImportJava.util.*;/*** Created by on 2016/7/1.*/@Intercepts ({@Signature (type= Executor.class, method = "Query", args = {mappedstatement.class, Object.class, Rowbounds.class, Resulthandler.class})}) Public classQueryexecutorinterceptorImplementsInterceptor {Private Static Finalstring[] Needescapes =Newstring[]{"%", "_"}; PrivateProperties Properties; Private StaticObject Convertsqlescape (object parameter, list<string>propertylist) { if(PropertyList! =NULL&& propertylist.size () > 0 && parameterinstanceofMap) {Map Map=(Map) parameter; for(Object Key:map.keySet ()) {Object value=Map.get (key); if(ValueinstanceofMap) {Map.replace (Key, Convertsqlescape (value, propertylist)); } Else if(ValueinstanceofString && Propertylist.indexof (Key) >-1) {String val=(String) value; for(String needescape:needescapes) {if(Val.contains (Needescape)) {Val= Val.replace (Needescape, "/" +needescape); }} map.replace (Key, Val); } } } returnparameter; } @Override PublicObject Intercept (invocation invocation)throwsthrowable {Object parameter= Invocation.getargs () [1]; Mappedstatement Statement= (mappedstatement) invocation.getargs () [0]; List<ParameterMapping> parametermappinglist =statement.getboundsql (parameter). Getparametermappings (); List<String> propertylist =NewArraylist<>(); if(Parametermappinglist! =NULL) {Parametermappinglist.foreach (P-{Propertylist.add (P.getproperty ()); }); } Invocation.getargs () [1] =convertsqlescape (parameter, propertylist); returninvocation.proceed (); } @Override PublicObject Plugin (object target) {returnPlugin.wrap (Target, This); } @Override Public voidSetProperties (Properties properties) { This. Properties =properties; }}
2. Opposite query Mapper like after to add escape '/'
<SelectID= "GetList"Resultmap= "Multiresultmap"ParameterType= "Java.util.Map">SELECT * from Sys_test T WHERE 1=1<ifTest= "_parameter.containskey (' key ')">and UPPER (CONCAT (t.role_name,t.role_info)) Like UPPER (CONCAT ('% ', CONCAT, #{key}), '% ') ESCAPE '/'</if> </Select>
As a best practice, you can intercept SQL directly and then automatically add escape '/' after SQL, but you haven't found a suitable method
MyBatis like query processing%_ symbols