Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report