欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MyBatis特殊字符轉(zhuǎn)義攔截器問(wèn)題針對(duì)(_、\、%)

 更新時(shí)間:2023年02月07日 14:42:22   作者:在奮斗的大道  
這篇文章主要介紹了MyBatis特殊字符轉(zhuǎn)義攔截器問(wèn)題針對(duì)(_、\、%),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

一、問(wèn)題反饋

今天公司測(cè)試向我反饋,系統(tǒng)用戶模糊查詢功能在用戶名稱包含特殊字符時(shí)(_、\、%)無(wú)法正常查詢結(jié)果。

二、問(wèn)題驗(yàn)證

1、當(dāng)like中包含_時(shí),查詢?nèi)詾槿?,?like '%_%'查詢出來(lái)的結(jié)果與like '%%'一致,并不能查詢出實(shí)際字段中包含有_特殊字符的結(jié)果條目

2、like中包括%時(shí),與1中相同

3、like中包含\時(shí),帶入查詢時(shí),%\%無(wú)法查詢到包含字段中有\的條目

三、問(wèn)題解決思路

采用MyBatis 攔截器機(jī)制,處理模糊查詢中包含特殊字符(_、\、%)

四、核心功能代碼

4.1 MyBatis 特殊字符攔截器編寫(xiě)

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];
		// 請(qǐng)求參數(shù)對(duì)象
		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("%", "\\\\%");
						// 請(qǐng)求參數(shù)對(duì)象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 通過(guò)@Configuration標(biāo)簽

實(shí)例化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;
	}
 
	/**
	 * 分頁(yè)對(duì)象實(shí)列化
	 * @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é)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論