MyBatis特殊字符转义 * 问题针对(_、\\、%)

作者:在奋斗的大道 时间:2023-09-12 00:46:39 

一、问题反馈

今天公司测试向我反馈,系统用户模糊查询功能在用户名称包含特殊字符时(_、\、%)无法正常查询结果。

二、问题验证

1、当like中包含_时,查询仍为全部,即 like '%_%'查询出来的结果与like '%%'一致,并不能查询出实际字段中包含有_特殊字符的结果条目

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];
// 请求参数对象
Object parameterObject = args[1];

// 获取 SQL
SqlCommandType sqlCommandType = statement.getSqlCommandType();
if (SqlCommandType.SELECT.equals(sqlCommandType)) {
if (parameterObject instanceof HashMap) {
// 调用特殊字符处理方法
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("%", "\\\\%");
// 请求参数对象HashMap重新赋值转义后的值
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标签

实例化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;
}
}

效果展示:

MyBatis特殊字符转义 * 问题针对(_、\\、%)

来源:https://blog.csdn.net/zhouzhiwengang/article/details/116204217

标签:MyBatis,特殊字符, ,
0
投稿

猜你喜欢

  • Android Support Annotations资料整理

    2021-09-21 13:39:33
  • IntelliJ Plugin 开发之添加第三方jar的示例代码

    2021-11-27 13:35:52
  • 使用java从乱码文本中解析出正确的文本

    2023-12-03 12:10:22
  • Java实现接月饼小游戏的示例代码

    2022-08-16 03:26:06
  • Java编程接口回调一般用法代码解析

    2023-11-11 06:55:11
  • jdbc和mybatis的流式查询使用方法

    2023-08-24 15:39:25
  • 利用Kotlin Tools如何快速添加Kotlin依赖详解

    2021-06-24 05:43:52
  • Android Monkey压力测试详细介绍

    2021-10-24 08:02:37
  • C#递归实现显示文件夹及所有文件并计算其大小的方法

    2023-09-13 01:56:01
  • 快速了解Maven

    2022-10-22 20:18:33
  • C++实现幸运大抽奖(QT版)

    2021-09-15 21:32:00
  • 解决idea 暂存文件或idea切换分支代码丢失的问题

    2023-09-11 07:52:41
  • Java中抓取 Thread Dumps 的方式汇总

    2021-08-09 23:20:26
  • java中this的n种使用方法

    2023-12-23 13:13:23
  • Android实现绘制折线图APP代码

    2022-12-10 07:50:02
  • c#委托与事件(详解)

    2022-12-06 04:50:47
  • 谈谈对Java中的volatile的理解

    2022-06-28 06:31:26
  • Android仿QQ首页ListView左滑置顶、删除功能

    2022-02-16 05:15:48
  • SpringAOP+RabbitMQ+WebSocket实战详解

    2023-01-01 18:40:25
  • Android开发之MediaPlayer基本使用方法详解

    2022-09-05 12:21:38
  • asp之家 软件编程 m.aspxhome.com