mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-08 01:28:24 +08:00
commit
39ff0e7d28
File diff suppressed because it is too large
Load Diff
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -20,6 +20,7 @@ import com.mybatisflex.core.constant.SqlConsts;
|
|||||||
import com.mybatisflex.core.exception.FlexExceptions;
|
import com.mybatisflex.core.exception.FlexExceptions;
|
||||||
import com.mybatisflex.core.field.FieldQuery;
|
import com.mybatisflex.core.field.FieldQuery;
|
||||||
import com.mybatisflex.core.field.FieldQueryBuilder;
|
import com.mybatisflex.core.field.FieldQueryBuilder;
|
||||||
|
import com.mybatisflex.core.paginate.Page;
|
||||||
import com.mybatisflex.core.query.*;
|
import com.mybatisflex.core.query.*;
|
||||||
import com.mybatisflex.core.relation.RelationManager;
|
import com.mybatisflex.core.relation.RelationManager;
|
||||||
import org.apache.ibatis.exceptions.TooManyResultsException;
|
import org.apache.ibatis.exceptions.TooManyResultsException;
|
||||||
@ -127,6 +128,53 @@ public class MapperUtil {
|
|||||||
return !CPI.containsTable(where, CollectionUtil.toArrayString(joinTables));
|
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"})
|
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||||
public static <R> void queryFields(BaseMapper<?> mapper, List<R> list, Consumer<FieldQueryBuilder<R>>[] consumers) {
|
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) {
|
public static <E> E queryRelations(BaseMapper<?> mapper, E entity) {
|
||||||
queryRelations(mapper,Collections.singletonList(entity));
|
queryRelations(mapper, Collections.singletonList(entity));
|
||||||
return 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);
|
RelationManager.queryRelations(mapper, entities);
|
||||||
return entities;
|
return entities;
|
||||||
}
|
}
|
||||||
@ -219,4 +267,5 @@ public class MapperUtil {
|
|||||||
throw FlexExceptions.wrap("selectCountByQuery error, can not get number value of result: \"" + object + "\"");
|
throw FlexExceptions.wrap("selectCountByQuery error, can not get number value of result: \"" + object + "\"");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,17 +1,17 @@
|
|||||||
/**
|
/*
|
||||||
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
|
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
|
||||||
* <p>
|
* <p>
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
* You may obtain a copy of the License at
|
* You may obtain a copy of the License at
|
||||||
* <p>
|
* <p>
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
* <p>
|
* <p>
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package com.mybatisflex.test;
|
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.ConsoleMessageCollector;
|
||||||
import com.mybatisflex.core.audit.MessageCollector;
|
import com.mybatisflex.core.audit.MessageCollector;
|
||||||
import com.mybatisflex.core.logicdelete.LogicDeleteManager;
|
import com.mybatisflex.core.logicdelete.LogicDeleteManager;
|
||||||
|
import org.junit.Assert;
|
||||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
||||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||||
|
|
||||||
@ -59,10 +60,9 @@ public class InsertWithPkTestStarter {
|
|||||||
Account account = new Account();
|
Account account = new Account();
|
||||||
account.setId(1L);
|
account.setId(1L);
|
||||||
account.setUserName("michael1");
|
account.setUserName("michael1");
|
||||||
accountMapper.insertWithPk(account);
|
int rows = accountMapper.insertWithPk(account);
|
||||||
|
Assert.assertEquals(1, rows);
|
||||||
accountMapper.selectAll().forEach(System.out::println);
|
accountMapper.selectAll().forEach(System.out::println);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user