异常信息修改

This commit is contained in:
yulichang 2023-03-30 17:43:58 +08:00
parent 1bdb91325a
commit 38bcaee07e
15 changed files with 92 additions and 20 deletions

View File

@ -94,6 +94,7 @@ public class MPJSqlInjector extends DefaultSqlInjector {
}
private List<AbstractMethod> methodFilter(List<AbstractMethod> list) {
String packageStr = SelectList.class.getPackage().getName();
List<String> methodList = Arrays.asList(
"Update",
"Delete",
@ -104,9 +105,10 @@ public class MPJSqlInjector extends DefaultSqlInjector {
"SelectObjs",
"SelectList",
"SelectPage");
list.removeIf(i -> methodList.contains(i.getClass().getSimpleName()));
list.addAll(getWrapperMethod());
list.addAll(getJoinMethod());
list.removeIf(i -> methodList.contains(i.getClass().getSimpleName()) &&
Objects.equals(packageStr, i.getClass().getPackage().getName()));
addAll(list,getWrapperMethod());
addAll(list,getJoinMethod());
return list;
}
@ -146,6 +148,14 @@ public class MPJSqlInjector extends DefaultSqlInjector {
return list;
}
private void addAll(List<AbstractMethod> source, List<AbstractMethod> addList) {
for (AbstractMethod method : addList) {
if (source.stream().noneMatch(m -> m.getClass().getSimpleName().equals(method.getClass().getSimpleName()))) {
source.add(method);
}
}
}
@Override
public void inspectInject(MapperBuilderAssistant builderAssistant, Class<?> mapperClass) {
Class<?> modelClass = getSuperClassGenericType(mapperClass, Mapper.class, 0);

View File

@ -10,6 +10,7 @@ import com.baomidou.mybatisplus.core.toolkit.*;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.github.yulichang.config.ConfigProperties;
import com.github.yulichang.query.interfaces.StringJoin;
import com.github.yulichang.toolkit.Asserts;
import com.github.yulichang.toolkit.TableHelper;
import java.util.*;
@ -155,7 +156,7 @@ public class MPJLambdaQueryWrapper<T> extends AbstractLambdaWrapper<T, MPJLambda
@Override
public MPJLambdaQueryWrapper<T> select(Class<T> entityClass, Predicate<TableFieldInfo> predicate) {
TableInfo info = TableHelper.get(entityClass);
Assert.notNull(info, "table not find by class <%s>", entityClass.getSimpleName());
Asserts.hasTable(info, entityClass);
selectColumns.addAll(info.getFieldList().stream().filter(predicate).map(c ->
alias + StringPool.DOT + c.getColumn()).collect(Collectors.toList()));
return typedThis;
@ -180,7 +181,7 @@ public class MPJLambdaQueryWrapper<T> extends AbstractLambdaWrapper<T, MPJLambda
@SuppressWarnings("DuplicatedCode")
public final MPJLambdaQueryWrapper<T> selectAll(Class<?> clazz, String as) {
TableInfo info = TableHelper.get(clazz);
Assert.notNull(info, "table not find by class <%s>", clazz.getSimpleName());
Asserts.hasTable(info, clazz);
if (info.havePK()) {
selectColumns.add(as + StringPool.DOT + info.getKeyColumn());
}

View File

@ -9,6 +9,7 @@ import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.*;
import com.github.yulichang.config.ConfigProperties;
import com.github.yulichang.query.interfaces.StringJoin;
import com.github.yulichang.toolkit.Asserts;
import com.github.yulichang.toolkit.TableHelper;
import com.github.yulichang.wrapper.interfaces.Chain;
@ -153,7 +154,7 @@ public class MPJQueryWrapper<T> extends AbstractWrapper<T, String, MPJQueryWrapp
@Override
public MPJQueryWrapper<T> select(Class<T> entityClass, Predicate<TableFieldInfo> predicate) {
TableInfo info = TableHelper.get(entityClass);
Assert.notNull(info, "table not find by class <%s>", entityClass.getSimpleName());
Asserts.hasTable(info, entityClass);
selectColumns.addAll(info.getFieldList().stream().filter(predicate).map(c ->
alias + StringPool.DOT + c.getSqlSelect()).collect(Collectors.toList()));
return typedThis;
@ -178,7 +179,7 @@ public class MPJQueryWrapper<T> extends AbstractWrapper<T, String, MPJQueryWrapp
@SuppressWarnings({"DuplicatedCode", "UnusedReturnValue"})
public final MPJQueryWrapper<T> selectAll(Class<?> clazz, String as) {
TableInfo info = TableHelper.get(clazz);
Assert.notNull(info, "table not find by class <%s>", clazz);
Asserts.hasTable(info, clazz);
if (ConfigProperties.tableInfoAdapter.mpjHasPK(info)) {
selectColumns.add(as + StringPool.DOT + info.getKeySqlSelect());
}

View File

@ -0,0 +1,19 @@
package com.github.yulichang.toolkit;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
/**
* @author yulichang
* @since 1.4.5
*/
public class Asserts {
public static void hasTable(TableInfo tableInfo, Class<?> entityClass) {
if (tableInfo == null) {
throw new MybatisPlusException(String.format(
"mapper not find by class <%s> , add mapper and extends BaseMapper<T> or MPJBaseMapper<T>",
entityClass.getSimpleName()));
}
}
}

View File

@ -45,7 +45,7 @@ public class LogicInfoUtils implements Constants {
String logicStr;
TableInfo tableInfo = TableHelper.get(clazz);
Assert.notNull(tableInfo, "table not find by class <%s>", clazz.getSimpleName());
Asserts.hasTable(tableInfo, clazz);
TableFieldInfo logicField = ConfigProperties.tableInfoAdapter.mpjGetLogicField(tableInfo);
if (ConfigProperties.tableInfoAdapter.mpjHasLogic(tableInfo) && Objects.nonNull(logicField)) {
final String value = logicField.getLogicNotDeleteValue();

View File

@ -1,6 +1,13 @@
package com.github.yulichang.toolkit;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.github.yulichang.mapper.MPJTableMapperHelper;
import org.apache.ibatis.session.SqlSession;
import java.util.Objects;
/**
* spring容器工具类
*
@ -19,6 +26,18 @@ public class SpringContentUtils {
return SpringContentUtils.springContext.getBean(clazz);
}
@SuppressWarnings("unchecked")
public static <T> T getMapper(Class<?> clazz) {
if (Objects.isNull(springContext)) {
SqlSession session = SqlHelper.sqlSession(clazz);
Assert.notNull(session, "mapper not find by class <%s>", clazz.getSimpleName());
return (T) SqlHelper.getMapper(clazz, session);
}
Class<?> mapper = MPJTableMapperHelper.getMapper(clazz);
Assert.notNull(mapper, "mapper not find by class <%s>", clazz.getSimpleName());
return (T)getBean(mapper);
}
public interface SpringContext {
<T> T getBean(Class<T> clazz);

View File

@ -17,18 +17,14 @@ public class SqlHelper {
public static <R, T> R exec(Class<T> entityClass, Function<BaseMapper<T>, R> function) {
Assert.notNull(entityClass,"请使用 new MPJLambdaWrapper(主表.class) 或 JoinWrappers.lambda(主表.class) 构造方法");
Class<?> mapperClass = MPJTableMapperHelper.getMapper(entityClass);
Assert.notNull(mapperClass, "mapper not find by class <%s>", entityClass.getSimpleName());
Object mapper = SpringContentUtils.getBean(mapperClass);
Object mapper = SpringContentUtils.getMapper(entityClass);
Assert.notNull(mapper, "mapper not init <%s>", entityClass.getSimpleName());
return function.apply((BaseMapper<T>) mapper);
}
public static <R, T> R execJoin(Class<T> entityClass, Function<MPJBaseMapper<T>, R> function) {
Assert.notNull(entityClass,"请使用 new MPJLambdaWrapper(主表.class) 或 JoinWrappers.lambda(主表.class) 构造方法");
Class<?> mapperClass = MPJTableMapperHelper.getMapper(entityClass);
Assert.notNull(mapperClass, "mapper not find by class <%s>", entityClass.getSimpleName());
Object mapper = SpringContentUtils.getBean(mapperClass);
Object mapper = SpringContentUtils.getMapper(entityClass);
Assert.notNull(mapper, "mapper not init <%s>", entityClass.getSimpleName());
Assert.isTrue(mapper instanceof MPJBaseMapper, "mapper <%s> not extends MPJBaseMapper ", entityClass.getSimpleName());
return function.apply((MPJBaseMapper<T>) mapper);

View File

@ -1,8 +1,8 @@
package com.github.yulichang.toolkit.support;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.github.yulichang.config.ConfigProperties;
import com.github.yulichang.toolkit.Asserts;
import com.github.yulichang.toolkit.TableHelper;
import com.github.yulichang.wrapper.segments.SelectCache;
@ -28,7 +28,7 @@ public class ColumnCache {
public static List<SelectCache> getListField(Class<?> clazz) {
return LIST_CACHE.computeIfAbsent(clazz, c -> {
TableInfo tableInfo = TableHelper.get(clazz);
Assert.notNull(tableInfo, "table not find by class <%s>", c.getSimpleName());
Asserts.hasTable(tableInfo, c);
List<SelectCache> list = new ArrayList<>();
if (ConfigProperties.tableInfoAdapter.mpjHasPK(tableInfo)) {
list.add(new SelectCache(clazz, true, tableInfo.getKeyColumn(), tableInfo.getKeyType(), tableInfo.getKeyProperty(), null));

View File

@ -409,7 +409,7 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
Integer oldIndex = this.getIndex();
int newIndex = tableIndex;
TableInfo info = TableHelper.get(clazz);
Assert.notNull(info, "table not find by class <%s>", clazz.getSimpleName());
Asserts.hasTable(info, clazz);
MPJLambdaWrapper<T> instance = instance(newIndex, keyWord, clazz, info.getTableName());
instance.isNo = true;
instance.isMain = false;

View File

@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.github.yulichang.toolkit.Asserts;
import com.github.yulichang.toolkit.LambdaUtils;
import com.github.yulichang.toolkit.MPJReflectionKit;
import com.github.yulichang.toolkit.TableHelper;
@ -55,7 +56,7 @@ public interface Query<Children> extends Serializable {
*/
default <E> Children select(Class<E> entityClass, Predicate<TableFieldInfo> predicate) {
TableInfo info = TableHelper.get(entityClass);
Assert.notNull(info, "table not find by class <%s>", entityClass.getSimpleName());
Asserts.hasTable(info, entityClass);
Map<String, SelectCache> cacheMap = ColumnCache.getMapField(entityClass);
info.getFieldList().stream().filter(predicate).collect(Collectors.toList()).forEach(
i -> getSelectColum().add(new SelectNormal(cacheMap.get(i.getProperty()), getIndex(), isHasAlias(), getAlias())));

View File

@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.github.yulichang.toolkit.Asserts;
import com.github.yulichang.toolkit.LambdaUtils;
import com.github.yulichang.toolkit.MPJReflectionKit;
import com.github.yulichang.toolkit.TableHelper;
@ -238,7 +239,7 @@ public class MybatisLabel<E, T> implements Label<T> {
private void autoBuild(boolean auto, Class<E> entityClass, Class<T> tagClass) {
TableInfo tableInfo = TableHelper.get(entityClass);
Assert.notNull(tableInfo, "table not find by class <%s>", entityClass.getSimpleName());
Asserts.hasTable(tableInfo, entityClass);
Map<String, FieldCache> tagMap = MPJReflectionKit.getFieldMap(tagClass);
if (auto && !tagMap.isEmpty()) {
List<SelectCache> listField = ColumnCache.getListField(entityClass);

View File

@ -5,7 +5,9 @@ import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.yulichang.config.ConfigProperties;
import com.github.yulichang.toolkit.Asserts;
import com.github.yulichang.toolkit.TableHelper;
import jdk.nashorn.internal.AssertsEnabled;
import lombok.Getter;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.type.TypeHandler;
@ -56,7 +58,7 @@ public class SelectCache {
this.hasTypeHandle = this.tableFieldInfo.getTypeHandler() != null && tableFieldInfo.getTypeHandler() != UnknownTypeHandler.class;
if (this.hasTypeHandle) {
TableInfo info = TableHelper.get(clazz);
Assert.notNull(info, "table not find by class <%s>", clazz.getSimpleName());
Asserts.hasTable(info, clazz);
this.typeHandler = getTypeHandler(ConfigProperties.tableInfoAdapter.mpjGetConfiguration(info), tableFieldInfo);
} else {
this.typeHandler = null;

View File

@ -136,4 +136,18 @@ class CollectionTest {
assert dtos2.get(0).getB().getC().getD().getE().getId() != null;
assert Objects.equals(dtos2.get(0).getB().getName(), "tableD1");
}
void test(){
MPJLambdaWrapper<TableA> wrapper1 = new MPJLambdaWrapper<TableA>()
.selectAll(TableA.class)
.selectAssociation(TableB.class, TableADTO::getB, b -> b
.collection(TableC.class, TableBDTO::getCList))
.leftJoin(TableB.class, TableB::getAid, TableA::getId)
.leftJoin(TableC.class, TableC::getBid, TableB::getId)
.leftJoin(TableD.class, TableD::getCid, TableC::getId)
.leftJoin(TableE.class, TableE::getDid, TableD::getId);
List<TableADTO> dtos1 = tableAMapper.selectJoinList(TableADTO.class, wrapper1);
}
}

View File

@ -42,6 +42,8 @@ public class UserDTO {
private List<AddressDTO> addressList;
private AddressDTO addressDTO;
private List<Integer> addressIds;
private List<UserDO> children;

View File

@ -684,6 +684,12 @@ class LambdaWrapperTest {
.gt(UserDO::getId, 3)
.lt(UserDO::getId, 8));
assert dos.size() == 4;
ThreadLocalUtils.set("SELECT id,pid,`name`,`json`,sex,head_img,create_time,address_id,address_id2,del,create_by,update_by FROM `user` t WHERE t.del=false AND (t.id > ? AND t.id < ?)");
List<UserDO> dos1 = userMapper.selectList(new MPJLambdaWrapper<UserDO>()
.gt(UserDO::getId, 3)
.lt(UserDO::getId, 8));
assert dos1.size() == 4;
}
/**