酒店网站建设公司排名wordpress站点链接打不开网址
web/
2025/10/3 3:22:39/
文章来源:
酒店网站建设公司排名,wordpress站点链接打不开网址,安康网站开发公司价格,上海本地企业在mysql中#xff0c;会有相关的like关键词#xff0c;并且默认的是忽略大小写的。但是在postgresql和kingbase中#xff0c;只有ilike关键字#xff0c;并且默认是大小写敏感的。当我们使用mybatisplus的时候#xff0c;默认提供的api也只有like()。这里提供一种方式来对…在mysql中会有相关的like关键词并且默认的是忽略大小写的。但是在postgresql和kingbase中只有ilike关键字并且默认是大小写敏感的。当我们使用mybatisplus的时候默认提供的api也只有like()。这里提供一种方式来对原始api进行拓展
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;Overridepublic 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();sqlsql.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();}Overridepublic Object plugin(Object target) {return Plugin.wrap(target, this);}Overridepublic void setProperties(Properties properties) {}
}直接通过拦截器的方式将select语句进行分析当有like的时候将进行转为ilike
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/85993.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!