!116 optimize BaseMapper

Merge pull request !116 from 王帅/main
This commit is contained in:
Michael Yang 2023-07-08 03:40:29 +00:00 committed by Gitee
commit 39ff0e7d28
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
4 changed files with 440 additions and 384 deletions

View File

@ -0,0 +1,58 @@
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mybatisflex.core.exception;
import java.util.Map;
/**
* 断言
*
* @author 王帅
* @since 2023-07-08
*/
public final class FlexAssert {
private FlexAssert() {
}
/**
* 断言对象不为空如果为空抛出异常并指明哪个对象为空
*
* @param obj 对象
* @param msg 错误消息
* @throws MybatisFlexException 如果对象为空抛出此异常
*/
public static void notNull(Object obj, String msg) {
if (obj == null) {
throw FlexExceptions.wrap(msg);
}
}
/**
* 断言 Map 集合不为 {@code null} 或者空集合如果为空则抛出异常并指明为什么不允许为空集合
*
* @param map Map 集合
* @param msg 错误消息
* @throws MybatisFlexException 如果集合为空抛出此异常
*/
public static void notEmpty(Map<?, ?> map, String msg) {
if (map == null || map.isEmpty()) {
throw FlexExceptions.wrap(msg);
}
}
}

View File

@ -20,6 +20,7 @@ import com.mybatisflex.core.constant.SqlConsts;
import com.mybatisflex.core.exception.FlexExceptions;
import com.mybatisflex.core.field.FieldQuery;
import com.mybatisflex.core.field.FieldQueryBuilder;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.*;
import com.mybatisflex.core.relation.RelationManager;
import org.apache.ibatis.exceptions.TooManyResultsException;
@ -127,6 +128,53 @@ public class MapperUtil {
return !CPI.containsTable(where, CollectionUtil.toArrayString(joinTables));
}
@SafeVarargs
public static <T, R> Page<R> doPaginate(BaseMapper<T> mapper, Page<R> page, QueryWrapper queryWrapper, Class<R> asType, boolean withRelations, Consumer<FieldQueryBuilder<R>>... consumers) {
try {
// 只有 totalRow 小于 0 的时候才会去查询总量
// 这样方便用户做总数缓存而非每次都要去查询总量
// 一般的分页场景中只有第一页的时候有必要去查询总量第二页以后是不需要的
if (page.getTotalRow() < 0) {
QueryWrapper countQueryWrapper;
if (page.needOptimizeCountQuery()) {
countQueryWrapper = MapperUtil.optimizeCountQueryWrapper(queryWrapper);
} else {
countQueryWrapper = MapperUtil.rawCountQueryWrapper(queryWrapper);
}
page.setTotalRow(mapper.selectCountByQuery(countQueryWrapper));
}
if (page.isEmpty()) {
return page;
}
queryWrapper.limit(page.offset(), page.getPageSize());
List<R> records;
if (asType != null) {
records = mapper.selectListByQueryAs(queryWrapper, asType);
} else {
// noinspection unchecked
records = (List<R>) mapper.selectListByQuery(queryWrapper);
}
if (withRelations) {
queryRelations(mapper, records);
}
queryFields(mapper, records, consumers);
page.setRecords(records);
return page;
} finally {
// 将之前设置的 limit 清除掉
// 保险起见把重置代码放到 finally 代码块中
CPI.setLimitRows(queryWrapper, null);
CPI.setLimitOffset(queryWrapper, null);
}
}
@SuppressWarnings({"rawtypes", "unchecked"})
public static <R> void queryFields(BaseMapper<?> mapper, List<R> list, Consumer<FieldQueryBuilder<R>>[] consumers) {
@ -166,12 +214,12 @@ public class MapperUtil {
}
public static <Entity> Entity queryRelations(BaseMapper<?> mapper, Entity entity) {
queryRelations(mapper,Collections.singletonList(entity));
public static <E> E queryRelations(BaseMapper<?> mapper, E entity) {
queryRelations(mapper, Collections.singletonList(entity));
return entity;
}
public static <Entity> List<Entity> queryRelations(BaseMapper<?> mapper, List<Entity> entities) {
public static <E> List<E> queryRelations(BaseMapper<?> mapper, List<E> entities) {
RelationManager.queryRelations(mapper, entities);
return entities;
}
@ -219,4 +267,5 @@ public class MapperUtil {
throw FlexExceptions.wrap("selectCountByQuery error, can not get number value of result: \"" + object + "\"");
}
}
}

View File

@ -1,17 +1,17 @@
/**
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mybatisflex.test;
@ -20,6 +20,7 @@ import com.mybatisflex.core.audit.AuditManager;
import com.mybatisflex.core.audit.ConsoleMessageCollector;
import com.mybatisflex.core.audit.MessageCollector;
import com.mybatisflex.core.logicdelete.LogicDeleteManager;
import org.junit.Assert;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
@ -59,10 +60,9 @@ public class InsertWithPkTestStarter {
Account account = new Account();
account.setId(1L);
account.setUserName("michael1");
accountMapper.insertWithPk(account);
int rows = accountMapper.insertWithPk(account);
Assert.assertEquals(1, rows);
accountMapper.selectAll().forEach(System.out::println);
}
}