Get the App
SLTechnology News&Howtos  ›  Development  › 

How to escape the special character error report of like fuzzy query in mybatis

Shulou Source: shulou.com Published: 2022-06-02 02:29:18 09月12日 Update

The purpose of this article is to share with you about how to escape the special character error of like fuzzy query in mybatis. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Like Fuzzy query Special character error escape processing Scheme 1 AND info.name like # {projectName_} Scheme 2 AND info.name like concat ('%', # {projectName_},'%') like fuzzy query contains special characters (_,\,%)

When using a fuzzy query in MyBatis, the query keyword becomes invalid when the query keyword includes _,\,%.

When like contains _, the query is still all, that is, the result of the query from like'%%'is consistent with that of like'%%', and the result entries containing the special character _ in the actual field cannot be queried.

When% is included in like, it is the same as in 1

When like contains\,%% cannot query entries with\ in the containing field when brought into the query.

Deal with

Tool classes: escape of special characters

Import org.apache.commons.lang3.StringUtils; public class EscapeUtil {/ / mysql special characters escape public static String escapeChar (String before) {if (StringUtils.isNotBlank (before)) {before = before.replaceAll ("\", "\"); before = before.replaceAll ("_", "\ _") Before = before.replaceAll ("%", "\%");} return before;}} attention

The above method contains\ in keywords that may fail because the database field sorting rule of the query keyword is utf8_unicode_ci. In order not to fail, the query keyword sorting rule must be utf8_general_ci, or unify all database character sets and collation rules are: utf8mb4 and utf8mb4_general_ci, respectively.

Thank you for reading! On the "mybatis like fuzzy query special character error how to achieve escape processing" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, you can share it out for more people to see it!

Tags: Query character special package escape key keyword processing field rule sort content data database scheme more entry article result good Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Shulou Technology macOS OPPO Reno Shulou Information