美文网首页
Mybatis-plus使用oracle强制索引

Mybatis-plus使用oracle强制索引

作者: Jkanon | 来源:发表于2021-10-16 22:49 被阅读0次

使用Mybatis-plus(以下简称MP,当前最新版本为v3.4.3.4)在单表操作上真的是非常的舒适,代码写到飞起。项目中遇到oracle默认没有使用正确的索引的情况,需要手工根据查询条件使用不同的强制索引。第一想法是先到官方文档上去找,无果。接着到Github上去搜索一番,确实有人提到了类似的 需求,不过官方貌似无意支持。所以摆在面前的只有两条路,一是使用原生的语法去写sql语句,二是想办法改造MP进行适配。鉴于这并非特殊需求,而且小编也着实不忍放弃MP所带来的快感,就选择了后者以除此坑一劳永逸。

首先,我们先来看看oracle的强制索引语法

 SELECT /*+index(t index_name)*/TABLE_FIELD FROM TABLE_NAME t 

--强制索引,/*.....*/第一个星星后不能有空格,里边内容结构为:+index(表名 索引名)。
--如果使用了表别名,括号中的表名也必须是别名

可以看到,这和普通的语句的区别只是在查询字段前边添加了一个索引标示/* ... */,所以小编先是想到了MP中条件构造器的select函数,能够自定义查询字段,我们可以写作select("/*+index(table_name index_name)*/*"),通过*号表示取出所有字段,这种写法初步达到了我们的目的。

不过这在遇到分页查询统计就不那么好使了,因为这样生成的语句是select count(/*+index(table_name index_name)*/*),通过比对强制索引的语法,现在的问题在于我们需要把/*...*/之中的内容搬到count前边去。

在阅读MP的源码时候我们发现其selectCount的语法是这样的SELECT_COUNT("selectCount", "查询满足条件总记录数", "<script>%s SELECT COUNT(%s) FROM %s %s %s\n</script>"),,也就是说COUNT前边并无注入字符串的地方,无法满足我们的需求。

无奈之际,小编本想着重写一份selectCount方法,不过好在阅读官方文档之后,发现sql注入器,如此一来我们就可以另起炉灶,自定义一个方法,而不用去修改官方源码。想法也很简单,就是通过判断查询字段中的sql是否包含*/,如果有的话将其进行分割并放到适当的位置。

话不多说,源码敬上。下边我们自定义了一个selectCountViaIndex的函数,实现了上述需求,我们只需要在将Mapper继承自MyBaseMapper就继承了这个方法,然后在分页构造Page对象时,我们必须通过page.setSearchCount(false)关闭默认的查询方法,手工调用selectCountViaIndex方法。

  • SelectCountViaIndex.java
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;

public class SelectCountViaIndex extends AbstractMethod {
    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        String methodSql = "<script>%s SELECT %s FROM %s %s %s\n</script>";
        String sql = String.format(methodSql, sqlFirst(), getSqlCount(), tableInfo.getTableName(),
                sqlWhereEntityWrapper(true, tableInfo), sqlComment());
        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
        return this.addSelectMappedStatementForOther(mapperClass, "selectCountViaIndex", sqlSource, Long.class);
    }

    protected String getSqlCount() {
        return SqlScriptUtils.convertChoose(String.format("%s != null and %s != null", WRAPPER, Q_WRAPPER_SQL_SELECT),
                getSqlBySqlSelect(), "count(1)");
    }

    protected String getSqlBySqlSelect() {
        return SqlScriptUtils.convertChoose(String.format("%s.indexOf('*/') != -1", Q_WRAPPER_SQL_SELECT),
                "${ew.sqlSelect.substring(0, ew.sqlSelect.indexOf(\"*/\") + 2)}count(${ew.sqlSelect.substring(ew.sqlSelect.indexOf(\"*/\") + 2)})",
                String.format("count(${%s})", Q_WRAPPER_SQL_SELECT));
    }
}
  • MyLogicSqlInjector
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import java.util.List;

public class MyLogicSqlInjector extends DefaultSqlInjector {
    @Override
    public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
        List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo);
        methodList.add(new SelectCountViaIndex());
        return methodList;
    }
}
  • MyBaseMapper.java
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface MyBaseMapper<T> extends BaseMapper<T> {
    long selectCountViaIndex(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
}

相关文章

  • Mybatis-plus使用oracle强制索引

    使用Mybatis-plus(以下简称MP,当前最新版本为v3.4.3.4)在单表操作上真的是非常的舒适,代码写到...

  • [SQL]ORACLE的强制索引的方法

    ORACLE的强制索引 在一些场景下,可能ORACLE不会自动走索引,这时候,如果对业务清晰,可以尝试使用强制索引...

  • mysql查询时强制或者禁止使用某个索引

    mysql强制索引和禁止某个索引 1、mysql强制使用索引:force index(索引名或者主键PRI)例如:...

  • mysql hint优化器提示之使用force index 强制

    mysql强制使用索引 1、mysql强制使用主键索引 2、强制指定一个特定索引 3、同时指定两个 4、在多个表j...

  • Mysql 索引优化

    联合索引和单个索引选择 对比,值越大越好 强制使用某个索引 使用explain分析索引 1、id:SQL执行的顺序...

  • laravel eloquent 强制mysql使用索引

    为什么需要强制索引? `数据库没有使用我们设想的索引进行sql查询,导致查询特别慢。` mysql强制索引查询语句...

  • Oracle数据源Sequence使用

    关于配置Oracle数据源无法使用Mybatis-plus 主键类型 AUTO:"数据库ID自增", INPUT...

  • 数据库性能与并发锁系列

    1.oracle精进 强走索引使用; 2.oracle闪回数据维护使用; ```swift ``` 3.数据库机制...

  • Mysql使用强制索引

    问题 生成环境,同一条sql在不同的从库执行,产生的执行计划不同,一个使用了索引,一个未使用索引 原因分析 分析是...

  • mongodb索引

    先题几个问题 什么是索引? 如何建立索引? 如何选择建立索引的字段? 如何强制使用索引? 如何评估索引效率? 如果...

网友评论

      本文标题:Mybatis-plus使用oracle强制索引

      本文链接:https://www.haomeiwen.com/subject/rqxioltx.html