MyBatis like query processing%_ symbols

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.