Mybatis-Plus 使用简析

依赖

		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-boot-starter</artifactId>
			<version>3.5.3.1</version>
		</dependency>

注:不要再配置 Mybatis 的依赖,否则会报错

日志

添加如下依赖,可以方便打印 sql 进行调试,生产环境记得去掉

mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

分页原理

必须拦截器,否则不生效

@Configuration
public class MyBatisPlusConfig {
    
    

    /**
     * 新增分页拦截器,并设置数据库类型为mysql
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
    
    
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
        return interceptor;
    }
}

核心要点

要理解 Mybatis 的分页逻辑,这段代码看懂即可

1、参数中必须包含 IPage 对象。

    public static Optional<IPage> findPage(Object parameterObject) {
    
    
        if (parameterObject != null) {
    
    
            if (parameterObject instanceof Map) {
    
    
                Map<?, ?> parameterMap = (Map)parameterObject;
                Iterator var2 = parameterMap.entrySet().iterator();

                while(var2.hasNext()) {
    
    
                    Map.Entry entry = (Map.Entry)var2.next();
                    if (entry.getValue() != null && entry.getValue() instanceof IPage) {
    
    
                        return Optional.of((IPage)entry.getValue());
                    }
                }
            } else if (parameterObject instanceof IPage) {
    
    
                return Optional.of((IPage)parameterObject);
            }
        }

        return Optional.empty();
    }

2、分页查询

1)不想使用分页,size < 0 和 maxLimit == null(默认为 null) 即可。
2)limit 的目的是限制 size 的最大值

    public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
    
    
        IPage<?> page = (IPage)ParameterUtils.findPage(parameter).orElse((Object)null);
        if (null != page) {
    
    
            boolean addOrdered = false;
            String buildSql = boundSql.getSql();
            List<OrderItem> orders = page.orders();
            if (CollectionUtils.isNotEmpty(orders)) {
    
    
                addOrdered = true;
                buildSql = this.concatOrderBy(buildSql, orders);
            }

            Long _limit = page.maxLimit() != null ? page.maxLimit() : this.maxLimit;
            if (page.getSize() < 0L && null == _limit) {
    
    
                if (addOrdered) {
    
    
                    PluginUtils.mpBoundSql(boundSql).sql(buildSql);
                }

            } else {
    
    
                this.handlerLimit(page, _limit);
                IDialect dialect = this.findIDialect(executor);
                Configuration configuration = ms.getConfiguration();
                DialectModel model = dialect.buildPaginationSql(buildSql, page.offset(), page.getSize());
                PluginUtils.MPBoundSql mpBoundSql = PluginUtils.mpBoundSql(boundSql);
                List<ParameterMapping> mappings = mpBoundSql.parameterMappings();
                Map<String, Object> additionalParameter = mpBoundSql.additionalParameters();
                model.consumers(mappings, configuration, additionalParameter);
                mpBoundSql.sql(model.getDialectSql());
                mpBoundSql.parameterMappings(mappings);
            }
        }
    }

猜你喜欢

转载自blog.csdn.net/wenxueliu/article/details/131031175