MyBatis特殊字符轉(zhuǎn)義攔截器問題針對(_、\、%)
一、問題反饋
今天公司測試向我反饋,系統(tǒng)用戶模糊查詢功能在用戶名稱包含特殊字符時(_、\、%)無法正常查詢結(jié)果。
二、問題驗證
1、當(dāng)like中包含_時,查詢?nèi)詾槿?,?like '%_%'查詢出來的結(jié)果與like '%%'一致,并不能查詢出實際字段中包含有_特殊字符的結(jié)果條目
2、like中包括%時,與1中相同
3、like中包含\時,帶入查詢時,%\%無法查詢到包含字段中有\的條目
三、問題解決思路
采用MyBatis 攔截器機制,處理模糊查詢中包含特殊字符(_、\、%)
四、核心功能代碼
4.1 MyBatis 特殊字符攔截器編寫
package com.zzg.sql.interceptor;
import java.util.HashMap;
import java.util.Properties;
import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
/**
* 自定義攔截器方法,處理模糊查詢中包含特殊字符(_、%、\)
*/
@Intercepts({
@Signature(type = Executor.class, method = "query", args = { MappedStatement.class, Object.class,
RowBounds.class, ResultHandler.class }),
@Signature(type = Executor.class, method = "query", args = { MappedStatement.class, Object.class,
RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class }) })
public class EscapeInterceptor implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
// TODO Auto-generated method stub
// 攔截sql
Object[] args = invocation.getArgs();
MappedStatement statement = (MappedStatement) args[0];
// 請求參數(shù)對象
Object parameterObject = args[1];
// 獲取 SQL
SqlCommandType sqlCommandType = statement.getSqlCommandType();
if (SqlCommandType.SELECT.equals(sqlCommandType)) {
if (parameterObject instanceof HashMap) {
// 調(diào)用特殊字符處理方法
HashMap hash = (HashMap) parameterObject;
hash.forEach((k, v) -> {
// 僅攔截字符串類型且值不為空
if (v != null && v instanceof String) {
String value = (String) v;
value = value.replaceAll("\\\\", "\\\\\\\\");
value = value.replaceAll("_", "\\\\_");
value = value.replaceAll("%", "\\\\%");
// 請求參數(shù)對象HashMap重新賦值轉(zhuǎn)義后的值
hash.put(k, value);
}
});
}
}
// 返回
return invocation.proceed();
}
@Override
public Object plugin(Object target) {
// TODO Auto-generated method stub
return Plugin.wrap(target, this);
}
@Override
public void setProperties(Properties properties) {
// TODO Auto-generated method stub
}
}4.2 通過@Configuration標(biāo)簽
實例化EscapeInterceptor 攔截器。
package com.zzg.config;
import java.util.Properties;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.zzg.sql.interceptor.EscapeInterceptor;
import com.github.pagehelper.PageHelper;
@Configuration
public class MyBatisConfig {
/**
* mybatis 自定義攔截器(特殊字符處理)
* @return
*/
@Bean
public EscapeInterceptor getEscapeInterceptor(){
EscapeInterceptor interceptor = new EscapeInterceptor();
return interceptor;
}
/**
* 分頁對象實列化
* @return
*/
@Bean
public PageHelper pageHelper() {
PageHelper pageHelper = new PageHelper();
Properties p = new Properties();
p.setProperty("offsetAsPageNum", "true");
p.setProperty("rowBoundsWithCount", "true");
p.setProperty("reasonable", "true");
p.setProperty("dialect", "mysql");
pageHelper.setProperties(p);
return pageHelper;
}
}效果展示:

總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java 調(diào)用Restful API接口的幾種方式(HTTPS)
這篇文章主要介紹了Java 調(diào)用Restful API接口的幾種方式(HTTPS),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02
SpringBoot+MySQL+Jpa實現(xiàn)對數(shù)據(jù)庫的增刪改查和分頁詳解
這篇文章主要介紹了SpringBoot+MySQL+Jpa實現(xiàn)對數(shù)據(jù)庫的增刪改查和分頁詳解,需要的朋友可以參考下2020-02-02
BeanUtils.copyProperties()參數(shù)的賦值順序說明
這篇文章主要介紹了BeanUtils.copyProperties()參數(shù)的賦值順序說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
Idea報錯: A JNI error has occurred的問題
這篇文章主要介紹了Idea報錯: A JNI error has occurred的問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08
java實現(xiàn)日歷(某年的日歷,某月的日歷)用戶完全自定義
本篇文章介紹了,java實現(xiàn)日歷(某年的日歷,某月的日歷)用戶完全自定義。需要的朋友參考下2013-05-05

