springboot中bean形式配置mybatis的分页插件

mybatis的分页插件在开发中往往必不可少,使用起来也非常简单。以往我们的配置都是在xml中进行的,springboot推荐使用bean的形式进行配置。所以,今天就来看看使用java bean的形式配置mybatis的分页插件。

1、添加依赖

首先引入必要的依赖:分页插件的依赖

<dependency>
	<groupId>com.github.pagehelper</groupId>
	<artifactId>pagehelper</artifactId>
	<version>5.1.7</version>
</dependency>

2、配置拦截器

既然是一个插件,我们就应该知道它配置在什么地方。

在SqlSessionFactoryBean中有一个属性plugins,就是用来配置插件的:

可以看到,这是一个数组,意思很清楚,就是说我们可以配置多个拦截器(其实就是多个插件)。

Interceptor接口

我们有必要看看这个接口:

public interface Interceptor {

  Object intercept(Invocation invocation) throws Throwable;

  Object plugin(Object target);

  void setProperties(Properties properties);

}

这个接口里有三个方法。并且有2个默认的实现类:

(看名字你也大概猜到是干嘛的啦)

intercept方法:拦截器拦截时执行的,进行相关的逻辑处理。

plugin方法:处理插件的,这个我们不需要关注。

setProperties方法:这个就是设置参数的,我们通常需要用到这个方法。

如何配置

如上,我们看到Interceptor有2个默认的实现类,第一个PageInterceptor就是用来分页的。

先说说它配置在哪里吧:

和mybatis的其他配置一样,我们需要把它加到sqlSessionFactoryBean里,其实就是把这个拦截器加入plugins。

@Bean(name = "sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource dataSource) 
        throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources(mapperLocations));
        // 添加分页插件
        bean.setPlugins(new Interceptor[]{pageInterceptor()});
        return bean.getObject();
    }

说说分页参数的配置

上面说到我们需要用到 setProperties方法,接下来就说说这个方法的妙用:

上面我们将pageInterceptor加入到sqlSessionFactoryBean里,可以看到我写了个pageInterceptor方法:

    // 分页拦截器
    private PageInterceptor pageInterceptor(){
        PageInterceptor pageInterceptor = new PageInterceptor();
        // 详见 com.github.pagehelper.page.PageParams
        Properties p = new Properties();
//        p.setProperty("offsetAsPageNum", "false");
//        p.setProperty("rowBoundsWithCount", "false");
//        p.setProperty("reasonable", "false");
        // 设置数据库方言 , 也可以不设置,会动态获取
        p.setProperty("helperDialect", "mysql");
        pageInterceptor.setProperties(p);
        return pageInterceptor;
    }

简单来看,这个方法就是创建了一个PageInterceptor对象,然后返回。(其实我们可以不做任何配置,就返回这个对象就可以的,但是某些情况下你可能需要做一些自己的配置,而不使用默认的)

可以看到,这里使用了setProperties方法,在这里Propertie放的就是配置,是一个键值对的。那么自然不是瞎写的了。

我们需要知道这些属性(或者说配置更好理解)都在哪里用到。就需要由setProperties这个方法往下追代码:

(具体这个就不多说了,如果时间允许,你可以自己跟下代码的)

我们继续进入到 上面圈出的方法里:

此时你会发现,这个方法有多个实现的。既然是分页,那么自然就是使用PageHelper这个实现了(其实,上面的default_dialect_class就是这个类)。

继续进入该方法:

    @Override
    public void setProperties(Properties properties) {
        setStaticProperties(properties);
        pageParams = new PageParams();
        autoDialect = new PageAutoDialect();
        pageParams.setProperties(properties);
        autoDialect.setProperties(properties);
        //20180902新增 aggregateFunctions, 允许手动添加聚合函数(影响行数)
        CountSqlParser.addAggregateFunctions(properties.getProperty("aggregateFunctions"));
    }

在这里就有你需要的所有的配置:  pageParams 和 autoDialect  (分别是分页和数据库方言相关设置)。因为这两个类里的注释写的很是详细,我就不多说了,贴出部分代码。



package com.github.pagehelper.page;

import com.github.pagehelper.IPage;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageRowBounds;
import com.github.pagehelper.util.PageObjectUtil;
import com.github.pagehelper.util.StringUtil;
import org.apache.ibatis.session.RowBounds;

import java.util.Properties;

/**
 * Page 参数信息
 *
 * @author liuzh
 */
public class PageParams {
    //RowBounds参数offset作为PageNum使用 - 默认不使用
    protected boolean offsetAsPageNum = false;
    //RowBounds是否进行count查询 - 默认不查询
    protected boolean rowBoundsWithCount = false;
    //当设置为true的时候,如果pagesize设置为0(或RowBounds的limit=0),就不执行分页,返回全部结果
    protected boolean pageSizeZero = false;
    //分页合理化
    protected boolean reasonable = false;
    //是否支持接口参数来传递分页参数,默认false
    protected boolean supportMethodsArguments = false;
    //默认count(0)
    protected String countColumn = "0";

    /**
     * 获取分页参数
     *
     * @param parameterObject
     * @param rowBounds
     * @return
     */
    public Page getPage(Object parameterObject, RowBounds rowBounds) {
        Page page = PageHelper.getLocalPage();
        if (page == null) {
            if (rowBounds != RowBounds.DEFAULT) {
                if (offsetAsPageNum) {
                    page = new Page(rowBounds.getOffset(), rowBounds.getLimit(), rowBoundsWithCount);
                } else {
                    page = new Page(new int[]{rowBounds.getOffset(), rowBounds.getLimit()}, rowBoundsWithCount);
                    //offsetAsPageNum=false的时候,由于PageNum问题,不能使用reasonable,这里会强制为false
                    page.setReasonable(false);
                }
                if(rowBounds instanceof PageRowBounds){
                    PageRowBounds pageRowBounds = (PageRowBounds)rowBounds;
                    page.setCount(pageRowBounds.getCount() == null || pageRowBounds.getCount());
                }
            } else if(parameterObject instanceof IPage || supportMethodsArguments){
                try {
                    page = PageObjectUtil.getPageFromObject(parameterObject, false);
                } catch (Exception e) {
                    return null;
                }
            }
            if(page == null){
                return null;
            }
            PageHelper.setLocalPage(page);
        }
        //分页合理化
        if (page.getReasonable() == null) {
            page.setReasonable(reasonable);
        }
        //当设置为true的时候,如果pagesize设置为0(或RowBounds的limit=0),就不执行分页,返回全部结果
        if (page.getPageSizeZero() == null) {
            page.setPageSizeZero(pageSizeZero);
        }
        return page;
    }

    public void setProperties(Properties properties) {
        //offset作为PageNum使用
        String offsetAsPageNum = properties.getProperty("offsetAsPageNum");
        this.offsetAsPageNum = Boolean.parseBoolean(offsetAsPageNum);
        //RowBounds方式是否做count查询
        String rowBoundsWithCount = properties.getProperty("rowBoundsWithCount");
        this.rowBoundsWithCount = Boolean.parseBoolean(rowBoundsWithCount);
        //当设置为true的时候,如果pagesize设置为0(或RowBounds的limit=0),就不执行分页
        String pageSizeZero = properties.getProperty("pageSizeZero");
        this.pageSizeZero = Boolean.parseBoolean(pageSizeZero);
        //分页合理化,true开启,如果分页参数不合理会自动修正。默认false不启用
        String reasonable = properties.getProperty("reasonable");
        this.reasonable = Boolean.parseBoolean(reasonable);
        //是否支持接口参数来传递分页参数,默认false
        String supportMethodsArguments = properties.getProperty("supportMethodsArguments");
        this.supportMethodsArguments = Boolean.parseBoolean(supportMethodsArguments);
        //默认count列
        String countColumn = properties.getProperty("countColumn");
        if(StringUtil.isNotEmpty(countColumn)){
            this.countColumn = countColumn;
        }
        //当offsetAsPageNum=false的时候,不能
        //参数映射
        PageObjectUtil.setParams(properties.getProperty("params"));
    }

    public boolean isOffsetAsPageNum() {
        return offsetAsPageNum;
    }

    public boolean isRowBoundsWithCount() {
        return rowBoundsWithCount;
    }

    public boolean isPageSizeZero() {
        return pageSizeZero;
    }

    public boolean isReasonable() {
        return reasonable;
    }

    public boolean isSupportMethodsArguments() {
        return supportMethodsArguments;
    }

    public String getCountColumn() {
        return countColumn;
    }
}

小结

在不知不觉中,已经配置完成了。其实配置很简单,但是我们有必要知道其中的实现流程。

关于分页如何使用就不多说了,在PageHelper中的很多方法大家一看就懂了。

转载请务必保留此出处(原作者):https://blog.csdn.net/zhuzhezhuzhe1

版权声明:本文为原创文章,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。

https://blog.csdn.net/zhuzhezhuzhe1/article/details/84832400

猜你喜欢

转载自blog.csdn.net/zhuzhezhuzhe1/article/details/84832400
今日推荐