Skip to content

MyBatisPlus 分页配置

MyBatisPlus 3.2.0

java
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * MyBatisPlus插件配置
 *
 * @author 佳佳
 */
@Configuration
public class MybatisPlusConfig {
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}

参考:


XML 格式配置

使用上面 bean 的方式声明的分页拦截器没有起作用,发现是因为项目使用的是 xml 格式配置的方式声明的 sqlSessionFactoryBean

此时需要在其 plugins 下添加如下配置:

xml
<!-- MyBatisPlus 分页插件 -->
<bean id="paginationInterceptor"
      class="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor">
    <property name="properties">
        <value>
            <!-- 数据库方言(未配置时默认从当前数据库连接获取方言类型) -->
            <!-- dialectType=mysql -->
            <!--自定义数据库类型 -->
            <!-- dialectClazz=true -->
        </value>
    </property>
</bean>

另外项目的旧代码是通过 pagehelper 组件来实现数据库分页的,完整配置如下:

xml
<!--配置myBatis数据库连接工厂-->
<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
    <property name="configLocation" value="classpath:mybatis-config.xml"/>
    <property name="dataSource" ref="mybatis_dataSource"/>
    <property name="mapperLocations">
        <list>
            <value>
                classpath*:me/liujiajia/app/dao/mapper/*.xml
            </value>
        </list>
    </property>
    <property name="plugins">
        <list>
            <!-- MyBatisPlus 分页插件 -->
            <bean id="paginationInterceptor"
                  class="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor">
                <property name="properties">
                    <value>
                        <!-- 数据库方言(未配置时默认从当前数据库连接获取方言类型) -->
                        <!-- dialectType=mysql -->
                        <!-- 自定义数据库方言类型 -->
                        <!-- dialectClazz=customizedDialectClazz -->
                    </value>
                </property>
            </bean>
            <!-- MyBatis-PageHelper 分页插件 -->
            <bean class="com.github.pagehelper.PageInterceptor">
                <property name="properties">
                    <value>
                         helperDialect=mysql
                         reasonable=true
                         supportMethodsArguments=true
                         params=count=countSql
                         autoRuntimeDialect=true
                    </value>
                </property>
            </bean>
            <!-- 多租户插件 插件 -->
            <bean id="tenantMybatisInterceptor"
                  class="me.liujiajia.app.dao.tenant.MultiTenantPlugin">
                <property name="properties">
                    <value>
                        <!--当前数据库方言-->
                        dialect=mysql
                        <!--多租户隔离字段名称-->
                        tenantIdField=user_id
                        <!--需要隔离的表名称java正则表达式-->
                        tableRegex=ccc.*
                        <!--需要隔离的表名称,逗号分隔-->
                        tableNames=tb_log,tb_role
                    </value>
                </property>
            </bean>
        </list>
    </property>
</bean>

MyBatisPlus 3.4.1

java
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * MyBatis 插件配置
 *
 * @author JiaJia
 */
@Configuration
@EnableTransactionManagement
public class MybatisPlusConfig {
    /**
     * 配置分页插件
     *
     * @return
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

Page Layout Max Width

Adjust the exact value of the page width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the page layout
A ranged slider for user to choose and customize their desired width of the maximum width of the page layout can go.

Content Layout Max Width

Adjust the exact value of the document content width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the content layout
A ranged slider for user to choose and customize their desired width of the maximum width of the content layout can go.