Mybatis-plus使用oracle强制索引

使用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);
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容