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) 制限の目的は、サイズの最大値を制限することです。

    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