postgresql和kingbase关于模糊查询大小写兼容问题

在mysql中,会有相关的like关键词,并且默认的是忽略大小写的。但是在postgresql和kingbase中,只有ilike关键字,并且默认是大小写敏感的。当我们使用mybatisplus的时候,默认提供的api也只有like()。这里提供一种方式来对原始api进行拓展

java 复制代码
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.reflection.DefaultReflectorFactory;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.util.Properties;

/**
 * sql查询拦截器
 * <p>
 * 模糊查询时忽略大小写
 * </p>
 * @author lsh
 * @version 1.0
 * @date 2022/5/10 11:13
 */
@Slf4j
@Component
@Intercepts({@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})})
public class FuzzyQueryInterceptor implements Interceptor{
    private static final String FUZZY_QUERY_KEY = "like";
    private static final String IGNORE_FUZZY_QUERY_KEY = "ilike";

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
       // 这里添加判断容器中是否为KingBase获取postgresql的代码
        //拦截StatementHandler,获取Mybatis对象属性
        StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
        MetaObject metaObject = MetaObject
                .forObject(statementHandler, SystemMetaObject.DEFAULT_OBJECT_FACTORY, SystemMetaObject.DEFAULT_OBJECT_WRAPPER_FACTORY,
                        new DefaultReflectorFactory());
        MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
        //执行mapper方法的全路径名
        String id = mappedStatement.getId();
        //只拦截查询语句,将like关键字替换成ilike,以忽略大小写
        if (SqlCommandType.SELECT.equals(mappedStatement.getSqlCommandType())){
            BoundSql boundSql = statementHandler.getBoundSql();
            String sql = boundSql.getSql();
            sql=sql.replaceAll("\\s+"," ");
            if (StringUtils.containsIgnoreCase(sql," like ") || StringUtils.containsIgnoreCase(sql," like ?")){
                String mSql = sql.replaceAll("(?i)"+FUZZY_QUERY_KEY ,IGNORE_FUZZY_QUERY_KEY )
                log.debug(id + ":原始查询语句 ==> " + sql);
                log.debug(id + ":替换后的查询语句 ==> " + mSql);
                //通过反射修改sql语句
                Field field = boundSql.getClass().getDeclaredField("sql");
                field.setAccessible(true);
                field.set(boundSql, mSql);
            }
        }
        //执行结果
        return invocation.proceed();
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {
    }
}

直接通过拦截器的方式,将select语句进行分析,当有like的时候将进行转为ilike

相关推荐
Paraverse_徐志斌3 小时前
MySQL 线上大表 DDL 如何避免锁表(pt-online-schema-change)
数据库·mysql·ddl·mysql锁·锁表·pt-osc
哈哈幸运3 小时前
MySQL运维三部曲初级篇:从零开始打造稳定高效的数据库环境
linux·运维·数据库·mysql·性能优化
愚公搬代码4 小时前
【愚公系列】《Python网络爬虫从入门到精通》055-Scrapy_Redis分布式爬虫(安装Redis数据库)
数据库·爬虫·python
pwzs4 小时前
深入浅出 MVCC:MySQL 并发背后的多版本世界
数据库·后端·mysql
大熊猫今天吃什么4 小时前
【一天一坑】空数组,使用 allMatch 默认返回true
前端·数据库
双叶8364 小时前
(51单片机)LCD显示数据存储(DS1302时钟模块教学)(LCD1602教程)(独立按键教程)(延时函数教程)(I2C总线认识)(AT24C02认识)
c语言·数据库·单片机·嵌入式硬件·mongodb·51单片机·nosql
XY.散人4 小时前
初识Redis · C++客户端list和hash
数据库·redis·缓存
码上飞扬5 小时前
深入 MySQL 高级查询:JOIN、子查询与窗口函数的实用指南
数据库·mysql
海洋与大气科学6 小时前
【matlab】地图上的小图
开发语言·数据库·matlab
Geek__19926 小时前
Sqlite3交叉编译全过程
jvm·数据库·sqlite