This commit is contained in:
yulichang 2022-12-09 12:57:15 +08:00
parent 4a1cd1f211
commit 7b0cfa4ff2
23 changed files with 549 additions and 372 deletions

View File

@ -59,7 +59,7 @@ class test {
List<UserDTO> list = userMapper.selectJoinList(UserDTO.class, wrapper);
//分页查询 (需要启用 mybatis plus 分页插件)
List<UserDTO> listPage = userMapper.selectJoinPage(new Page<>(2, 10), UserDTO.class, wrapper);
Page<UserDTO> listPage = userMapper.selectJoinPage(new Page<>(2, 10), UserDTO.class, wrapper);
}
}
```
@ -165,7 +165,7 @@ class test {
List<UserDTO> list = userMapper.selectJoinList(UserDTO.class, wrapper);
//分页查询 (需要启用 mybatis plus 分页插件)
List<UserDTO> listPage = userMapper.selectJoinPage(new Page<>(1, 10), UserDTO.class, wrapper);
Page<UserDTO> listPage = userMapper.selectJoinPage(new Page<>(1, 10), UserDTO.class, wrapper);
}
}
```

View File

@ -39,7 +39,7 @@ import java.util.List;
* @author yulichang
* @since 1.3.7
*/
@SuppressWarnings("unused")
@SuppressWarnings("ALL")
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({SqlSessionFactory.class, SqlSessionFactoryBean.class})
@ConditionalOnSingleCandidate(DataSource.class)
@ -83,6 +83,7 @@ public class MybatisPlusJoinAutoConfiguration {
@Order(Ordered.HIGHEST_PRECEDENCE)
@ConditionalOnMissingBean({DefaultSqlInjector.class, AbstractSqlInjector.class, ISqlInjector.class})
public MPJSqlInjector mpjSqlInjector() {
logger.info("MPJSqlInjector init");
return new MPJSqlInjector();
}
@ -91,7 +92,6 @@ public class MybatisPlusJoinAutoConfiguration {
*/
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
@SuppressWarnings("InstantiationOfUtilityClass")
public SpringContentUtils springContentUtils(SpringContext springContext) {
return new SpringContentUtils(springContext);
}
@ -119,7 +119,6 @@ public class MybatisPlusJoinAutoConfiguration {
}
@Override
@SuppressWarnings("NullableProblems")
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}

View File

@ -61,12 +61,6 @@
<version>1.18.24</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<scope>provided</scope>
<version>2.0.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>

View File

@ -240,9 +240,9 @@ public class MPJInterceptor implements Interceptor {
Field field = ofTypeField.get(r.getProperty());
if (columnSet.contains(columnName)) {
columnName = getColumn(columnSet, columnName);
label = new SelectLabel(r.getSelectNormal(), mybatisLabel.getOfType(), field, columnName);
label = new SelectLabel(r.getSelectNormal(), mybatisLabel.getIndex(), mybatisLabel.getOfType(), field, columnName);
} else {
label = new SelectLabel(r.getSelectNormal(), mybatisLabel.getOfType(), field);
label = new SelectLabel(r.getSelectNormal(), mybatisLabel.getIndex(), mybatisLabel.getOfType(), field);
}
columnList.add(label);
ResultMapping.Builder builder = new ResultMapping.Builder(ms.getConfiguration(), r.getProperty(), columnName, r.getJavaType());

View File

@ -18,15 +18,20 @@ import java.util.concurrent.ConcurrentHashMap;
*/
public class LogicInfoUtils implements Constants {
private static final Map<Class<?>, String> LOGIC_CACHE = new ConcurrentHashMap<>();
private static final Map<Class<?>, Map<String, String>> LOGIC_CACHE = new ConcurrentHashMap<>();
@SuppressWarnings("ConstantConditions")
public static String getLogicInfo(int tableIndex, Class<?> clazz) {
String logicStr = LOGIC_CACHE.get(clazz);
if (Objects.nonNull(logicStr)) {
return logicStr;
public static String getLogicInfo(String tableIndex, Class<?> clazz) {
Map<String, String> absent = LOGIC_CACHE.get(clazz);
if (absent == null) {
absent = new ConcurrentHashMap<>();
LOGIC_CACHE.put(clazz, absent);
}
return absent.computeIfAbsent(tableIndex, key -> getLogicStr(key, clazz));
}
private static String getLogicStr(String tableIndex, Class<?> clazz) {
String logicStr;
TableInfo tableInfo = TableInfoHelper.getTableInfo(clazz);
Assert.notNull(tableInfo, "%s 不是数据库实体或没有注册到mybatis plus中", clazz.getName());
if (tableInfo.isWithLogicDelete() && Objects.nonNull(tableInfo.getLogicDeleteFieldInfo())) {
@ -39,7 +44,6 @@ public class LogicInfoUtils implements Constants {
} else {
logicStr = StringPool.EMPTY;
}
LOGIC_CACHE.put(clazz, logicStr);
return logicStr;
}
}

View File

@ -3,7 +3,7 @@ package com.github.yulichang.toolkit.support;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.github.yulichang.wrapper.segments.SelectNormal;
import com.github.yulichang.wrapper.segments.SelectCache;
import java.util.ArrayList;
import java.util.List;
@ -20,24 +20,24 @@ import java.util.stream.Collectors;
*/
public class ColumnCache {
private static final Map<Class<?>, List<SelectNormal>> LIST_CACHE = new ConcurrentHashMap<>();
private static final Map<Class<?>, List<SelectCache>> LIST_CACHE = new ConcurrentHashMap<>();
private static final Map<Class<?>, Map<String, SelectNormal>> MAP_CACHE = new ConcurrentHashMap<>();
private static final Map<Class<?>, Map<String, SelectCache>> MAP_CACHE = new ConcurrentHashMap<>();
public static List<SelectNormal> getListField(Class<?> clazz) {
public static List<SelectCache> getListField(Class<?> clazz) {
return LIST_CACHE.computeIfAbsent(clazz, c -> {
TableInfo tableInfo = TableInfoHelper.getTableInfo(clazz);
Assert.notNull(tableInfo, "table not find by class <%s>", c.getSimpleName());
List<SelectNormal> list = new ArrayList<>();
List<SelectCache> list = new ArrayList<>();
if (tableInfo.havePK()) {
list.add(new SelectNormal(clazz, true, tableInfo.getKeyColumn(), tableInfo.getKeyType(), tableInfo.getKeyProperty(), null));
list.add(new SelectCache(clazz, true, tableInfo.getKeyColumn(), tableInfo.getKeyType(), tableInfo.getKeyProperty(), null));
}
list.addAll(tableInfo.getFieldList().stream().map(f -> new SelectNormal(clazz, false, f.getColumn(), f.getPropertyType(), f.getProperty(), f)).collect(Collectors.toList()));
list.addAll(tableInfo.getFieldList().stream().map(f -> new SelectCache(clazz, false, f.getColumn(), f.getPropertyType(), f.getProperty(), f)).collect(Collectors.toList()));
return list;
});
}
public static Map<String, SelectNormal> getMapField(Class<?> clazz) {
return MAP_CACHE.computeIfAbsent(clazz, c -> getListField(c).stream().collect(Collectors.toMap(SelectNormal::getColumProperty, Function.identity(), (i, j) -> j)));
public static Map<String, SelectCache> getMapField(Class<?> clazz) {
return MAP_CACHE.computeIfAbsent(clazz, c -> getListField(c).stream().collect(Collectors.toMap(SelectCache::getColumProperty, Function.identity(), (i, j) -> j)));
}
}

View File

@ -20,7 +20,6 @@ import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.github.yulichang.toolkit.ClassUtils;
import com.github.yulichang.toolkit.ReflectionKit;
import lombok.extern.slf4j.Slf4j;
import java.lang.invoke.SerializedLambda;
import java.lang.reflect.Field;
@ -28,7 +27,6 @@ import java.lang.reflect.Field;
/**
* Created by hcl at 2021/5/14
*/
@Slf4j
public class ReflectLambdaMeta implements LambdaMeta {
private static final Field FIELD_CAPTURING_CLASS;
@ -39,7 +37,6 @@ public class ReflectLambdaMeta implements LambdaMeta {
fieldCapturingClass = ReflectionKit.setAccessible(aClass.getDeclaredField("capturingClass"));
} catch (Throwable e) {
// 解决高版本 jdk 的问题 gitee: https://gitee.com/baomidou/mybatis-plus/issues/I4A7I5
log.warn(e.getMessage());
fieldCapturingClass = null;
}
FIELD_CAPTURING_CLASS = fieldCapturingClass;

View File

@ -5,12 +5,14 @@ import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.github.yulichang.toolkit.Constant;
import com.github.yulichang.toolkit.LambdaUtils;
import com.github.yulichang.toolkit.support.ColumnCache;
import com.github.yulichang.wrapper.segments.SelectNormal;
import com.github.yulichang.wrapper.segments.SelectCache;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.*;
import java.util.stream.Stream;
import static java.util.stream.Collectors.joining;
@ -25,7 +27,13 @@ public abstract class MPJAbstractLambdaWrapper<T, Children extends MPJAbstractLa
/**
* 关联的表
*/
protected Map<Class<?>, Integer> subTable = new HashMap<>();
protected TableList tableList = new TableList();
/**
* 表别名
*/
@Getter
protected String index;
@Override
protected <X> String columnToString(X column, boolean isJoin) {
@ -44,35 +52,87 @@ public abstract class MPJAbstractLambdaWrapper<T, Children extends MPJAbstractLa
getCache(column).getTagColumn();
}
protected SelectNormal getCache(SFunction<?, ?> fn) {
protected SelectCache getCache(SFunction<?, ?> fn) {
Class<?> aClass = LambdaUtils.getEntityClass(fn);
Map<String, SelectNormal> cacheMap = ColumnCache.getMapField(aClass);
Map<String, SelectCache> cacheMap = ColumnCache.getMapField(aClass);
return cacheMap.get(LambdaUtils.getName(fn));
}
protected String getDefault(Class<?> clazz, boolean isJoin) {
Integer index = subTable.get(clazz);
if (Objects.nonNull(index)) {
Table table = tableList.get(clazz, index);
if (Objects.nonNull(table.getIndex())) {
if (getEntityClass() == null) {
return index.toString();
return table.getIndex();
}
if (isJoin && joinClass == getEntityClass()) {
return StringPool.EMPTY;
}
return index.toString();
return table.getIndex();
}
return StringPool.EMPTY;
}
protected String getDefaultSelect(Class<?> clazz, boolean myself) {
Integer index = subTable.get(clazz);
if (Objects.nonNull(index)) {
Table table = tableList.get(clazz, index);
if (Objects.nonNull(table.getIndex())) {
if (myself) {
return StringPool.EMPTY;
}
return index.toString();
return table.getIndex();
}
return StringPool.EMPTY;
}
public static class TableList {
private static final Table DEFAULT_TABLE = new Table(null, null);
private final List<Table> list = new ArrayList<>();
public void add(Class<?> clazz, String index) {
this.list.add(new Table(clazz, index));
}
private Table get(Class<?> clazz) {
for (Table t : list) {
if (clazz == t.clazz) {
return t;
}
}
return DEFAULT_TABLE;
}
public Table get(Class<?> clazz, String index) {
if (Objects.isNull(index)) {
return get(clazz);
}
for (Table t : list) {
if (clazz == t.clazz && Objects.equals(index, t.getIndex())) {
return t;
}
}
return DEFAULT_TABLE;
}
public Stream<Table> stream() {
return list.stream();
}
public void clear() {
list.clear();
}
public boolean isEmpty() {
return list.isEmpty();
}
}
@Data
@EqualsAndHashCode
@AllArgsConstructor
public static class Table {
private Class<?> clazz;
private String index;
}
}

View File

@ -2,34 +2,32 @@ package com.github.yulichang.wrapper;
import com.baomidou.mybatisplus.core.conditions.SharedString;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.core.toolkit.*;
import com.baomidou.mybatisplus.core.toolkit.ArrayUtils;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.github.yulichang.config.ConfigProperties;
import com.github.yulichang.toolkit.Constant;
import com.github.yulichang.toolkit.LambdaUtils;
import com.github.yulichang.toolkit.*;
import com.github.yulichang.toolkit.LogicInfoUtils;
import com.github.yulichang.toolkit.MPJWrappers;
import com.github.yulichang.toolkit.support.ColumnCache;
import com.github.yulichang.wrapper.enums.BaseFuncEnum;
import com.github.yulichang.wrapper.interfaces.LambdaJoin;
import com.github.yulichang.wrapper.interfaces.Query;
import com.github.yulichang.wrapper.interfaces.QueryJoin;
import com.github.yulichang.wrapper.interfaces.QueryLabel;
import com.github.yulichang.wrapper.interfaces.on.OnFunction;
import com.github.yulichang.wrapper.resultmap.MFunc;
import com.github.yulichang.wrapper.resultmap.MybatisLabel;
import com.github.yulichang.wrapper.segments.Select;
import com.github.yulichang.wrapper.segments.SelectAlias;
import com.github.yulichang.wrapper.segments.SelectFunc;
import com.github.yulichang.wrapper.segments.SelectCache;
import com.github.yulichang.wrapper.segments.SelectNormal;
import lombok.Getter;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
@ -39,9 +37,9 @@ import java.util.stream.Collectors;
* @author yulichang
* @see MPJWrappers
*/
@SuppressWarnings({"unused", "unchecked"})
@SuppressWarnings({"unused"})
public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWrapper<T>>
implements Query<MPJLambdaWrapper<T>>, LambdaJoin<MPJLambdaWrapper<T>, T> {
implements Query<MPJLambdaWrapper<T>>, QueryJoin<MPJLambdaWrapper<T>, T>, QueryLabel<MPJLambdaWrapper<T>> {
/**
* 查询表
@ -66,11 +64,6 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
*/
@Getter
private boolean resultMap = false;
/**
* 是否自定义resultMap 自动构建不算
*/
@Getter
private boolean customResult = false;
/**
* 查询字段 sql
*/
@ -111,7 +104,7 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
MPJLambdaWrapper(T entity, Class<T> entityClass, SharedString sqlSelect, AtomicInteger paramNameSeq,
Map<String, Object> paramNameValuePairs, MergeSegments mergeSegments,
SharedString lastSql, SharedString sqlComment, SharedString sqlFirst,
Map<Class<?>, Integer> subTable, String keyWord, Class<?> joinClass) {
TableList tableList, String keyWord, Class<?> joinClass) {
super.setEntity(entity);
super.setEntityClass(entityClass);
this.paramNameSeq = paramNameSeq;
@ -121,7 +114,7 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
this.lastSql = lastSql;
this.sqlComment = sqlComment;
this.sqlFirst = sqlFirst;
this.subTable = subTable;
this.tableList = tableList;
this.keyWord = keyWord;
this.joinClass = joinClass;
}
@ -136,185 +129,43 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
return typedThis;
}
@Override
public List<Select> getSelectColum() {
return this.selectColumns;
}
@Override
public void addLabel(MybatisLabel<?, ?> label) {
this.resultMap = true;
this.resultMapMybatisLabel.add(label);
}
@Override
public MPJLambdaWrapper<T> getChildren() {
return typedThis;
}
/**
* 设置查询字段
*
* @param columns 字段数组
* @return children
*/
@SafeVarargs
public final <S> MPJLambdaWrapper<T> select(SFunction<S, ?>... columns) {
public final <E> MPJLambdaWrapper<T> select(SFunction<E, ?>... columns) {
if (ArrayUtils.isNotEmpty(columns)) {
for (SFunction<S, ?> s : columns) {
SelectNormal cache = getCache(s);
selectColumns.add(cache);
Class<?> aClass = LambdaUtils.getEntityClass(columns[0]);
Map<String, SelectCache> cacheMap = ColumnCache.getMapField(aClass);
for (SFunction<E, ?> s : columns) {
SelectCache cache = cacheMap.get(LambdaUtils.getName(s));
getSelectColum().add(new SelectNormal(cache, index));
}
}
return typedThis;
}
/**
* 一对多查询 调用此方法发必需要调用对应的 left join / right join ... 连表方法否则会报错
* <p>
* 举例 UserDO AddressDO 为一对多关系 UserDTO 为结果类
* <pre>
* MPJLambdaQueryWrapper<UserDO> wrapper = new MPJLambdaQueryWrapper<UserDO>();
* wrapper.selectAll(UserDO.class)
* .selectCollection(AddressDO.class, UserDTO::getAddressListDTO)
* .leftJoin(AddressDO.class, ...... )
* .eq(...)
* ...
* <pre/>
* 会自动将 AddressDO类中相同属性的字段 以mybatis<collection>的方式映射到UserDTO.addressListDTO属性中
*
* @since 1.3.0
*
* @param child 连表数据库实体类
* @param dtoField 包装类对应的属性
* @param <S> 包装类
* @param <C> 对多数据库实体类
* @param <Z> 包装类集合泛型
* @param <F> 包装类集合字段泛型
*/
public <S, C, Z, F extends java.util.Collection<?>> MPJLambdaWrapper<T> selectCollection(Class<C> child, SFunction<S, F> dtoField) {
String dtoFieldName = LambdaUtils.getName(dtoField);
Class<S> dtoClass = LambdaUtils.getEntityClass(dtoField);
Map<String, Field> fieldMap = MPJReflectionKit.getFieldMap(dtoClass);
Field field = fieldMap.get(dtoFieldName);
this.resultMap = true;
Class<?> genericType = MPJReflectionKit.getGenericType(field);
MybatisLabel.Builder<C, Z> builder;
if (genericType == null || genericType.isAssignableFrom(child)) {
//找不到集合泛型 List List<?> List<Object> 直接查询数据库实体
builder = new MybatisLabel.Builder<>(dtoFieldName, child, field.getType());
} else {
Class<Z> ofType = (Class<Z>) genericType;
builder = new MybatisLabel.Builder<>(dtoFieldName, child, field.getType(), ofType, true);
}
this.resultMapMybatisLabel.add(builder.build());
return typedThis;
}
/**
* 一对多查询 调用此方法发必需要调用对应的 left join / right join ... 连表方法否则会报错
* <p>
* 举例 UserDO AddressDO 为一对多关系 UserDTO 为结果类
* <pre>
* MPJLambdaQueryWrapper<UserDO> wrapper = new MPJLambdaQueryWrapper();
* wrapper.selectAll(UserDO.class)
* .selectCollection(AddressDO.class, UserDTO::getAddressListDTO, map -> map
* .id(AddressDO::getId, AddressDTO::getId) //如果属性名一致 可以传一个
* .result(AddressDO::getUserId) //如果属性名一致 可以传一个
* .result(AddressDO::getAddress, AddressDTO::getAddress))) //如果属性名一致 可以传一个
* .leftJoin(AddressDO.class, ...... )
* .eq(...)
* ...
* <pre/>
*
* 会自动将 AddressDO类中指定的字段 以mybatis<collection>的方式映射到UserDTO.addressListDTO属性中
*
* @since 1.3.0
*
* @param child 连表数据库实体类
* @param dtoField 包装类对应的属性
* @param collection collection标签内容
* @param <S> 包装类
* @param <C> 对多数据库实体类
* @param <Z> 包装类集合泛型
* @param <F> 包装类集合字段泛型
*/
public <S, C, Z, F extends java.util.Collection<Z>> MPJLambdaWrapper<T>
selectCollection(Class<C> child, SFunction<S, F> dtoField, MFunc<MybatisLabel.Builder<C, Z>> collection) {
String dtoFieldName = LambdaUtils.getName(dtoField);
Class<S> dtoClass = LambdaUtils.getEntityClass(dtoField);
Field field = MPJReflectionKit.getFieldMap(dtoClass).get(dtoFieldName);
this.resultMap = true;
//获取集合泛型
Class<?> genericType = MPJReflectionKit.getGenericType(field);
Class<Z> ofType = (Class<Z>) genericType;
MybatisLabel.Builder<C, Z> builder = new MybatisLabel.Builder<>(dtoFieldName, child, field.getType(), ofType, false);
MybatisLabel.Builder<C, Z> czBuilder = collection.apply(builder);
this.customResult = czBuilder.hasCustom();
this.resultMapMybatisLabel.add(czBuilder.build());
return typedThis;
}
/**
* 对一查询 用法参考 selectCollection
*
* @since 1.3.0
*/
public <S, C, F> MPJLambdaWrapper<T> selectAssociation(Class<C> child, SFunction<S, F> dtoField) {
String dtoFieldName = LambdaUtils.getName(dtoField);
Class<S> dtoClass = LambdaUtils.getEntityClass(dtoField);
Map<String, Field> fieldMap = MPJReflectionKit.getFieldMap(dtoClass);
Field field = fieldMap.get(dtoFieldName);
Assert.isFalse(Collection.class.isAssignableFrom(field.getType()), "association 不支持集合类");
this.resultMap = true;
MybatisLabel.Builder<C, F> builder;
builder = new MybatisLabel.Builder<>(dtoFieldName, child, field.getType(), (Class<F>) field.getType(), true);
this.resultMapMybatisLabel.add(builder.build());
return typedThis;
}
/**
* 对一查询 用法参考 selectCollection
*
* @since 1.3.0
*/
public <S, C, F> MPJLambdaWrapper<T> selectAssociation(Class<C> child, SFunction<S, F> dtoField,
MFunc<MybatisLabel.Builder<C, F>> collection) {
String dtoFieldName = LambdaUtils.getName(dtoField);
Class<S> dtoClass = LambdaUtils.getEntityClass(dtoField);
Field field = MPJReflectionKit.getFieldMap(dtoClass).get(dtoFieldName);
this.resultMap = true;
Assert.isFalse(Collection.class.isAssignableFrom(field.getType()), "association 不支持集合类");
MybatisLabel.Builder<C, F> builder = new MybatisLabel.Builder<>(dtoFieldName, child, field.getType(), (Class<F>) child, false);
MybatisLabel.Builder<C, F> cfBuilder = collection.apply(builder);
this.customResult = cfBuilder.hasCustom();
this.resultMapMybatisLabel.add(cfBuilder.build());
return typedThis;
}
@Override
public <E> MPJLambdaWrapper<T> select(Class<E> entityClass, Predicate<TableFieldInfo> predicate) {
TableInfo info = TableInfoHelper.getTableInfo(entityClass);
Map<String, SelectNormal> cacheMap = ColumnCache.getMapField(entityClass);
info.getFieldList().stream().filter(predicate).collect(Collectors.toList()).forEach(
i -> selectColumns.add(cacheMap.get(i.getProperty())));
return typedThis;
}
@Override
public <E> MPJLambdaWrapper<T> selectAsClass(Class<E> source, Class<?> tag) {
List<SelectNormal> normalList = ColumnCache.getListField(source);
Map<String, Field> fieldMap = MPJReflectionKit.getFieldMap(tag);
for (SelectNormal cache : normalList) {
if (fieldMap.containsKey(cache.getColumProperty())) {
selectColumns.add(cache);
}
}
return typedThis;
}
@Override
public <S> MPJLambdaWrapper<T> selectAs(SFunction<S, ?> column, String alias) {
selectColumns.add(new SelectAlias(getCache(column), alias));
return typedThis;
}
public <S> MPJLambdaWrapper<T> selectFunc(BaseFuncEnum funcEnum, SFunction<S, ?> column, String alias) {
selectColumns.add(new SelectFunc(getCache(column), alias, funcEnum));
return typedThis;
}
@Override
public MPJLambdaWrapper<T> selectFunc(BaseFuncEnum funcEnum, Object column, String alias) {
selectColumns.add(new SelectFunc(alias, funcEnum, column.toString()));
return typedThis;
}
public final MPJLambdaWrapper<T> selectAll(Class<?> clazz) {
selectColumns.addAll(ColumnCache.getListField(clazz));
return typedThis;
}
/**
* 查询条件 SQL 片段
*/
@ -341,7 +192,7 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
value.append(wrapper.getKeyWord())
.append(tableName)
.append(Constant.SPACE_TABLE_ALIAS)
.append(subTable.get(wrapper.getJoinClass()))
.append(tableList.get(wrapper.getJoinClass(), wrapper.getIndex()).getIndex())
.append(Constant.ON)
.append(wrapper.getExpression().getNormal().getSqlSegment());
}
@ -371,7 +222,7 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
protected MPJLambdaWrapper<T> instance(String keyWord, Class<?> joinClass) {
return new MPJLambdaWrapper<>(getEntity(), getEntityClass(), null, paramNameSeq, paramNameValuePairs,
new MergeSegments(), SharedString.emptyString(), SharedString.emptyString(), SharedString.emptyString(),
this.subTable, keyWord, joinClass);
this.tableList, keyWord, joinClass);
}
@Override
@ -380,7 +231,7 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
sqlSelect.toNull();
from.toNull();
selectColumns.clear();
subTable.clear();
tableList.clear();
}
/**
@ -418,11 +269,11 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
*/
public String getSubLogicSql() {
if (subLogicSql) {
if (CollectionUtils.isEmpty(subTable)) {
if (tableList.isEmpty()) {
return StringPool.EMPTY;
}
return subTable.entrySet().stream().map(entry -> LogicInfoUtils.getLogicInfo(entry.getValue(),
entry.getKey())).collect(Collectors.joining(StringPool.SPACE));
return tableList.stream().map(t -> LogicInfoUtils.getLogicInfo(t.getIndex(),
t.getClazz())).collect(Collectors.joining(StringPool.SPACE));
}
return StringPool.EMPTY;
}
@ -437,7 +288,7 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
@Override
public <R> MPJLambdaWrapper<T> join(String keyWord, Class<R> clazz, OnFunction<T> function) {
MPJLambdaWrapper<T> apply = function.apply(instance(keyWord, clazz));
subTable.put(clazz, tableIndex);
tableList.add(clazz, String.valueOf(tableIndex));
onWrappers.add(apply);
tableIndex++;
return typedThis;

View File

@ -1,13 +1,22 @@
package com.github.yulichang.wrapper.interfaces;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.github.yulichang.toolkit.LambdaUtils;
import com.github.yulichang.toolkit.MPJReflectionKit;
import com.github.yulichang.toolkit.support.ColumnCache;
import com.github.yulichang.wrapper.enums.BaseFuncEnum;
import com.github.yulichang.wrapper.enums.DefaultFuncEnum;
import com.github.yulichang.wrapper.segments.*;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
* 参考 {@link com.baomidou.mybatisplus.core.conditions.query.Query}
@ -17,14 +26,13 @@ import java.util.function.Predicate;
@SuppressWarnings("unused")
public interface Query<Children> extends Serializable {
/**
* 设置查询字段
*
* @param columns 字段数组
* @return children
*/
@SuppressWarnings("unchecked")
<E> Children select(SFunction<E, ?>... columns);
List<Select> getSelectColum();
Children getChildren();
String getIndex();
/**
* 过滤查询的字段信息(主键除外!)
@ -37,7 +45,17 @@ public interface Query<Children> extends Serializable {
* @param predicate 过滤方式
* @return children
*/
<E> Children select(Class<E> entityClass, Predicate<TableFieldInfo> predicate);
default <E> Children select(Class<E> entityClass, Predicate<TableFieldInfo> predicate) {
TableInfo info = TableInfoHelper.getTableInfo(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())));
return getChildren();
}
@SuppressWarnings("unchecked")
<E> Children select(SFunction<E, ?>... columns);
/**
* 说明
@ -49,7 +67,16 @@ public interface Query<Children> extends Serializable {
* @param tag 目标类
* @return children
*/
<E> Children selectAsClass(Class<E> source, Class<?> tag);
default <E> Children selectAsClass(Class<E> source, Class<?> tag) {
List<SelectCache> normalList = ColumnCache.getListField(source);
Map<String, Field> fieldMap = MPJReflectionKit.getFieldMap(tag);
for (SelectCache cache : normalList) {
if (fieldMap.containsKey(cache.getColumProperty())) {
getSelectColum().add(new SelectNormal(cache, getIndex()));
}
}
return getChildren();
}
/**
* ignore
@ -61,7 +88,12 @@ public interface Query<Children> extends Serializable {
/**
* 别名查询
*/
<S> Children selectAs(SFunction<S, ?> column, String alias);
default <S> Children selectAs(SFunction<S, ?> column, String alias) {
Class<?> aClass = LambdaUtils.getEntityClass(column);
Map<String, SelectCache> cacheMap = ColumnCache.getMapField(aClass);
getSelectColum().add(new SelectAlias(cacheMap.get(LambdaUtils.getName(column)), getIndex(), alias));
return getChildren();
}
/**
* 聚合函数查询
@ -75,9 +107,17 @@ public interface Query<Children> extends Serializable {
* @param column 函数作用的字段
* @param alias 别名
*/
Children selectFunc(BaseFuncEnum funcEnum, Object column, String alias);
default Children selectFunc(BaseFuncEnum funcEnum, Object column, String alias) {
getSelectColum().add(new SelectFunc(alias, getIndex(), funcEnum, column.toString()));
return getChildren();
}
<S> Children selectFunc(BaseFuncEnum funcEnum, SFunction<S, ?> column, String alias);
default <S> Children selectFunc(BaseFuncEnum funcEnum, SFunction<S, ?> column, String alias) {
Class<?> aClass = LambdaUtils.getEntityClass(column);
Map<String, SelectCache> cacheMap = ColumnCache.getMapField(aClass);
getSelectColum().add(new SelectFunc(cacheMap.get(LambdaUtils.getName(column)), getIndex(), alias, funcEnum));
return getChildren();
}
default <S, X> Children selectFunc(BaseFuncEnum funcEnum, SFunction<S, ?> column, SFunction<X, ?> alias) {
return selectFunc(funcEnum, column, LambdaUtils.getName(alias));
@ -94,7 +134,11 @@ public interface Query<Children> extends Serializable {
/**
* 查询实体类全部字段
*/
Children selectAll(Class<?> clazz);
default Children selectAll(Class<?> clazz) {
getSelectColum().addAll(ColumnCache.getListField(clazz).stream().map(i ->
new SelectNormal(i, getIndex())).collect(Collectors.toList()));
return getChildren();
}
/**
* select sql 片段

View File

@ -10,7 +10,7 @@ import com.github.yulichang.wrapper.interfaces.on.OnFunction;
* @author yulichang
*/
@SuppressWarnings("unused")
public interface LambdaJoin<Children, Entity> extends MPJBaseJoin<Entity> {
public interface QueryJoin<Children, Entity> extends MPJBaseJoin<Entity> {
/**
* left join

View File

@ -0,0 +1,138 @@
package com.github.yulichang.wrapper.interfaces;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.github.yulichang.toolkit.LambdaUtils;
import com.github.yulichang.toolkit.MPJReflectionKit;
import com.github.yulichang.wrapper.resultmap.MFunc;
import com.github.yulichang.wrapper.resultmap.MybatisLabel;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Map;
@SuppressWarnings({"unchecked", "unused"})
public interface QueryLabel<Children> {
void addLabel(MybatisLabel<?, ?> label);
Children getChildren();
/**
* 一对多查询 调用此方法发必需要调用对应的 left join / right join ... 连表方法否则会报错
* <p>
* 举例 UserDO AddressDO 为一对多关系 UserDTO 为结果类
* <pre>
* MPJLambdaQueryWrapper<UserDO> wrapper = new MPJLambdaQueryWrapper<UserDO>();
* wrapper.selectAll(UserDO.class)
* .selectCollection(AddressDO.class, UserDTO::getAddressListDTO)
* .leftJoin(AddressDO.class, ...... )
* .eq(...)
* ...
* <pre/>
* 会自动将 AddressDO类中相同属性的字段 以mybatis<collection>的方式映射到UserDTO.addressListDTO属性中
*
* @since 1.3.0
*
* @param child 连表数据库实体类
* @param dtoField 包装类对应的属性
* @param <S> 包装类
* @param <C> 对多数据库实体类
* @param <Z> 包装类集合泛型
* @param <F> 包装类集合字段泛型
*/
default <S, C, Z, F extends java.util.Collection<?>> Children selectCollection(Class<C> child, SFunction<S, F> dtoField) {
String dtoFieldName = LambdaUtils.getName(dtoField);
Class<S> dtoClass = LambdaUtils.getEntityClass(dtoField);
Map<String, Field> fieldMap = MPJReflectionKit.getFieldMap(dtoClass);
Field field = fieldMap.get(dtoFieldName);
Class<?> genericType = MPJReflectionKit.getGenericType(field);
MybatisLabel.Builder<C, Z> builder;
if (genericType == null || genericType.isAssignableFrom(child)) {
//找不到集合泛型 List List<?> List<Object> 直接查询数据库实体
builder = new MybatisLabel.Builder<>(dtoFieldName, child, field.getType());
} else {
Class<Z> ofType = (Class<Z>) genericType;
builder = new MybatisLabel.Builder<>(dtoFieldName, child, field.getType(), ofType, true);
}
addLabel(builder.build());
return getChildren();
}
/**
* 一对多查询 调用此方法发必需要调用对应的 left join / right join ... 连表方法否则会报错
* <p>
* 举例 UserDO AddressDO 为一对多关系 UserDTO 为结果类
* <pre>
* MPJLambdaQueryWrapper<UserDO> wrapper = new MPJLambdaQueryWrapper();
* wrapper.selectAll(UserDO.class)
* .selectCollection(AddressDO.class, UserDTO::getAddressListDTO, map -> map
* .id(AddressDO::getId, AddressDTO::getId) //如果属性名一致 可以传一个
* .result(AddressDO::getUserId) //如果属性名一致 可以传一个
* .result(AddressDO::getAddress, AddressDTO::getAddress))) //如果属性名一致 可以传一个
* .leftJoin(AddressDO.class, ...... )
* .eq(...)
* ...
* <pre/>
*
* 会自动将 AddressDO类中指定的字段 以mybatis<collection>的方式映射到UserDTO.addressListDTO属性中
*
* @since 1.3.0
*
* @param child 连表数据库实体类
* @param dtoField 包装类对应的属性
* @param collection collection标签内容
* @param <S> 包装类
* @param <C> 对多数据库实体类
* @param <Z> 包装类集合泛型
* @param <F> 包装类集合字段泛型
*/
default <S, C, Z, F extends java.util.Collection<Z>> Children selectCollection(Class<C> child,
SFunction<S, F> dtoField,
MFunc<MybatisLabel.Builder<C, Z>> collection) {
String dtoFieldName = LambdaUtils.getName(dtoField);
Class<S> dtoClass = LambdaUtils.getEntityClass(dtoField);
Field field = MPJReflectionKit.getFieldMap(dtoClass).get(dtoFieldName);
//获取集合泛型
Class<?> genericType = MPJReflectionKit.getGenericType(field);
Class<Z> ofType = (Class<Z>) genericType;
MybatisLabel.Builder<C, Z> builder = new MybatisLabel.Builder<>(dtoFieldName, child, field.getType(), ofType, false);
MybatisLabel.Builder<C, Z> czBuilder = collection.apply(builder);
addLabel(czBuilder.build());
return getChildren();
}
/**
* 对一查询 用法参考 selectCollection
*
* @since 1.3.0
*/
default <S, C, F> Children selectAssociation(Class<C> child, SFunction<S, F> dtoField) {
String dtoFieldName = LambdaUtils.getName(dtoField);
Class<S> dtoClass = LambdaUtils.getEntityClass(dtoField);
Map<String, Field> fieldMap = MPJReflectionKit.getFieldMap(dtoClass);
Field field = fieldMap.get(dtoFieldName);
Assert.isFalse(Collection.class.isAssignableFrom(field.getType()), "association 不支持集合类");
MybatisLabel.Builder<C, F> builder;
builder = new MybatisLabel.Builder<>(dtoFieldName, child, field.getType(), (Class<F>) field.getType(), true);
addLabel(builder.build());
return getChildren();
}
/**
* 对一查询 用法参考 selectCollection
*
* @since 1.3.0
*/
default <S, C, F> Children selectAssociation(Class<C> child, SFunction<S, F> dtoField,
MFunc<MybatisLabel.Builder<C, F>> collection) {
String dtoFieldName = LambdaUtils.getName(dtoField);
Class<S> dtoClass = LambdaUtils.getEntityClass(dtoField);
Field field = MPJReflectionKit.getFieldMap(dtoClass).get(dtoFieldName);
Assert.isFalse(Collection.class.isAssignableFrom(field.getType()), "association 不支持集合类");
MybatisLabel.Builder<C, F> builder = new MybatisLabel.Builder<>(dtoFieldName, child, field.getType(), (Class<F>) child, false);
MybatisLabel.Builder<C, F> cfBuilder = collection.apply(builder);
addLabel(cfBuilder.build());
return getChildren();
}
}

View File

@ -8,7 +8,7 @@ import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.github.yulichang.toolkit.LambdaUtils;
import com.github.yulichang.toolkit.MPJReflectionKit;
import com.github.yulichang.toolkit.support.ColumnCache;
import com.github.yulichang.wrapper.segments.SelectNormal;
import com.github.yulichang.wrapper.segments.SelectCache;
import lombok.Getter;
import java.lang.reflect.Field;
@ -24,6 +24,8 @@ import java.util.stream.Collectors;
@Getter
public class MybatisLabel<E, T> {
private final String index = null;
private String property;
private Class<E> entityClass;
@ -175,10 +177,6 @@ public class MybatisLabel<E, T> {
return this;
}
public boolean hasCustom() {
return CollectionUtils.isNotEmpty(mybatisLabel.resultList) || CollectionUtils.isNotEmpty(mybatisLabel.mybatisLabels);
}
public MybatisLabel<E, T> build() {
if (CollectionUtils.isEmpty(mybatisLabel.resultList)) {
autoBuild(true, mybatisLabel.entityClass, mybatisLabel.ofType);
@ -190,7 +188,7 @@ public class MybatisLabel<E, T> {
TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass);
Map<String, Field> tagMap = MPJReflectionKit.getFieldMap(tagClass);
if (auto && !tagMap.isEmpty()) {
List<SelectNormal> listField = ColumnCache.getListField(entityClass);
List<SelectCache> listField = ColumnCache.getListField(entityClass);
if (entityClass.isAssignableFrom(tagClass)) {
mybatisLabel.resultList.addAll(listField.stream().map(i -> {
Result result = new Result();
@ -202,7 +200,7 @@ public class MybatisLabel<E, T> {
return result;
}).collect(Collectors.toList()));
} else {
for (SelectNormal s : listField) {
for (SelectCache s : listField) {
Field field = tagMap.get(s.getColumProperty());
if (Objects.nonNull(field)) {
Result result = new Result();

View File

@ -4,7 +4,7 @@ import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.github.yulichang.toolkit.LambdaUtils;
import com.github.yulichang.toolkit.support.ColumnCache;
import com.github.yulichang.wrapper.segments.SelectNormal;
import com.github.yulichang.wrapper.segments.SelectCache;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
@ -25,7 +25,9 @@ public class Result {
private boolean isId;
private SelectNormal selectNormal;
private String index;
private SelectCache selectNormal;
private String property;
@ -54,9 +56,9 @@ public class Result {
public Builder<E, T> column(SFunction<E, ?> column) {
Class<E> entityClass = LambdaUtils.getEntityClass(column);
Map<String, SelectNormal> normalMap = ColumnCache.getMapField(entityClass);
Map<String, SelectCache> normalMap = ColumnCache.getMapField(entityClass);
String name = LambdaUtils.getName(column);
SelectNormal normal = normalMap.get(name);
SelectCache normal = normalMap.get(name);
result.selectNormal = normal;
if (StringUtils.isBlank(result.property)) {
result.property = normal.getColumProperty();

View File

@ -14,6 +14,8 @@ public interface Select {
Class<?> getClazz();
String getIndex();
boolean isPk();
String getColumn();

View File

@ -15,14 +15,17 @@ import org.apache.ibatis.type.TypeHandler;
@Getter
public class SelectAlias implements Select {
private final SelectNormal selectNormal;
private final SelectCache cache;
private final String index;
private final boolean hasAlias;
private final String alias;
public SelectAlias(SelectNormal selectNormal, String alias) {
this.selectNormal = selectNormal;
public SelectAlias(SelectCache cache, String index, String alias) {
this.cache = cache;
this.index = index;
this.hasAlias = true;
this.alias = alias;
}
@ -30,47 +33,48 @@ public class SelectAlias implements Select {
@Override
public Class<?> getClazz() {
return selectNormal.getClazz();
return cache.getClazz();
}
@Override
public boolean isPk() {
return selectNormal.isPk();
return cache.isPk();
}
@Override
public String getColumn() {
return selectNormal.getColumn();
return cache.getColumn();
}
@Override
public Class<?> getColumnType() {
return selectNormal.getColumnType();
return cache.getColumnType();
}
@Override
public String getTagColumn() {
return selectNormal.getTagColumn();
return cache.getTagColumn();
}
@Override
public String getColumProperty() {
return selectNormal.getColumProperty();
return cache.getColumProperty();
}
@Override
public boolean hasTypeHandle() {
return selectNormal.hasTypeHandle();
return cache.isHasTypeHandle();
}
@Override
public TypeHandler<?> getTypeHandle() {
return selectNormal.getTypeHandle();
return cache.getTypeHandler();
}
@Override
public TableFieldInfo getTableFieldInfo() {
return selectNormal.getTableFieldInfo();
return cache.getTableFieldInfo();
}
@Override

View File

@ -0,0 +1,75 @@
package com.github.yulichang.wrapper.segments;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import lombok.Getter;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.type.TypeHandler;
import org.apache.ibatis.type.TypeHandlerRegistry;
import org.apache.ibatis.type.UnknownTypeHandler;
import java.util.Objects;
/**
* 缓存列, 普通列
*
* @author yulichang
* @since 1.3.10
*/
@Getter
public class SelectCache {
private final Class<?> clazz;
private final boolean isPk;
private final String column;
private final Class<?> columnType;
private final String tagColumn;
private final String columProperty;
private final TableFieldInfo tableFieldInfo;
private final boolean hasTypeHandle;
private final TypeHandler<?> typeHandler;
public SelectCache(Class<?> clazz, boolean isPk, String column, Class<?> columnType, String columProperty, TableFieldInfo tableFieldInfo) {
this.clazz = clazz;
this.isPk = isPk;
this.column = column;
this.columnType = columnType;
this.columProperty = columProperty;
this.tagColumn = StringUtils.getTargetColumn(column);
this.tableFieldInfo = tableFieldInfo;
if (Objects.isNull(tableFieldInfo)) {
this.hasTypeHandle = false;
this.typeHandler = null;
} else {
this.hasTypeHandle = this.tableFieldInfo.getTypeHandler() != null && tableFieldInfo.getTypeHandler() != UnknownTypeHandler.class;
if (this.hasTypeHandle) {
TableInfo info = TableInfoHelper.getTableInfo(clazz);
Assert.notNull(info, "table not find by class <%s>", clazz.getSimpleName());
this.typeHandler = getTypeHandler(info.getConfiguration(), tableFieldInfo);
} else {
this.typeHandler = null;
}
}
}
private TypeHandler<?> getTypeHandler(Configuration configuration, TableFieldInfo info) {
TypeHandlerRegistry registry = configuration.getTypeHandlerRegistry();
TypeHandler<?> typeHandler = registry.getMappingTypeHandler(info.getTypeHandler());
if (typeHandler == null) {
typeHandler = registry.getInstance(info.getPropertyType(), info.getTypeHandler());
}
return typeHandler;
}
}

View File

@ -17,10 +17,11 @@ import java.util.Objects;
@Getter
public class SelectFunc implements Select {
private final String index;
private final SelectNormal selectNormal;
private final SelectCache cache;
private final String coloum;
private final String column;
private final boolean hasAlias;
@ -31,18 +32,20 @@ public class SelectFunc implements Select {
private final BaseFuncEnum func;
public SelectFunc(SelectNormal selectNormal, String alias, BaseFuncEnum func) {
this.selectNormal = selectNormal;
this.coloum = selectNormal.getColumn();
public SelectFunc(SelectCache cache, String index, String alias, BaseFuncEnum func) {
this.index = index;
this.cache = cache;
this.column = cache.getColumn();
this.hasAlias = true;
this.alias = alias;
this.isFunc = true;
this.func = func;
}
public SelectFunc(String alias, BaseFuncEnum func, String column) {
this.coloum = column;
this.selectNormal = null;
public SelectFunc(String alias, String index, BaseFuncEnum func, String column) {
this.index = index;
this.column = column;
this.cache = null;
this.hasAlias = true;
this.alias = alias;
this.isFunc = true;
@ -51,47 +54,44 @@ public class SelectFunc implements Select {
@Override
public Class<?> getClazz() {
return selectNormal.getClazz();
return Objects.isNull(cache) ? null : cache.getClazz();
}
@Override
public boolean isPk() {
return false;
}
@Override
public String getColumn() {
return coloum;
}
@Override
public Class<?> getColumnType() {
return Objects.isNull(selectNormal) ? null : selectNormal.getColumnType();
return Objects.isNull(cache) ? null : cache.getColumnType();
}
@Override
public String getTagColumn() {
return Objects.isNull(selectNormal) ? null : selectNormal.getTagColumn();
return Objects.isNull(cache) ? null : cache.getTagColumn();
}
@Override
public String getColumProperty() {
return Objects.isNull(selectNormal) ? null : selectNormal.getColumProperty();
return Objects.isNull(cache) ? null : cache.getColumProperty();
}
@Override
public boolean hasTypeHandle() {
return !Objects.isNull(selectNormal) && selectNormal.isHasTypeHandle();
return !Objects.isNull(cache) && cache.isHasTypeHandle();
}
@Override
public TypeHandler<?> getTypeHandle() {
return Objects.isNull(selectNormal) ? null : selectNormal.getTypeHandle();
return Objects.isNull(cache) ? null : cache.getTypeHandler();
}
@Override
public TableFieldInfo getTableFieldInfo() {
return Objects.isNull(selectNormal) ? null : selectNormal.getTableFieldInfo();
return Objects.isNull(cache) ? null : cache.getTableFieldInfo();
}
@Override

View File

@ -17,7 +17,9 @@ import java.lang.reflect.Field;
@Getter
public class SelectLabel implements Select {
private final SelectNormal selectNormal;
private final String index;
private final SelectCache cache;
private final Class<?> tagClass;
@ -27,16 +29,18 @@ public class SelectLabel implements Select {
private final String alias;
public SelectLabel(SelectNormal selectNormal, Class<?> tagClass, Field tagField) {
this.selectNormal = selectNormal;
public SelectLabel(SelectCache cache, String index, Class<?> tagClass, Field tagField) {
this.cache = cache;
this.index = index;
this.tagClass = tagClass;
this.tagField = tagField;
this.hasAlias = false;
this.alias = null;
}
public SelectLabel(SelectNormal selectNormal, Class<?> tagClass, Field tagField, String column) {
this.selectNormal = selectNormal;
public SelectLabel(SelectCache cache, String index, Class<?> tagClass, Field tagField, String column) {
this.cache = cache;
this.index = index;
this.tagClass = tagClass;
this.tagField = tagField;
this.hasAlias = true;
@ -45,42 +49,42 @@ public class SelectLabel implements Select {
@Override
public Class<?> getClazz() {
return selectNormal.getClazz();
return cache.getClazz();
}
@Override
public boolean isPk() {
return selectNormal.isPk();
return cache.isPk();
}
@Override
public String getColumn() {
return selectNormal.getColumn();
return cache.getColumn();
}
@Override
public Class<?> getColumnType() {
return selectNormal.getColumnType();
return cache.getColumnType();
}
@Override
public String getTagColumn() {
return selectNormal.getTagColumn();
return cache.getTagColumn();
}
@Override
public String getColumProperty() {
return selectNormal.getColumProperty();
return cache.getColumProperty();
}
@Override
public boolean hasTypeHandle() {
return selectNormal.isHasTypeHandle();
return cache.isHasTypeHandle();
}
@Override
public TypeHandler<?> getTypeHandle() {
return selectNormal.getTypeHandle();
return cache.getTypeHandler();
}
@Override
@ -95,7 +99,7 @@ public class SelectLabel implements Select {
@Override
public TableFieldInfo getTableFieldInfo() {
return selectNormal.getTableFieldInfo();
return cache.getTableFieldInfo();
}
@Override

View File

@ -1,21 +1,12 @@
package com.github.yulichang.wrapper.segments;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.yulichang.wrapper.enums.BaseFuncEnum;
import lombok.Getter;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.type.TypeHandler;
import org.apache.ibatis.type.TypeHandlerRegistry;
import org.apache.ibatis.type.UnknownTypeHandler;
import java.util.Objects;
/**
* 缓存列, 普通列
* 缓存列
*
* @author yulichang
* @since 1.3.10
@ -23,55 +14,54 @@ import java.util.Objects;
@Getter
public class SelectNormal implements Select {
private final Class<?> clazz;
private final String index;
private final boolean isPk;
private final SelectCache cache;
private final String column;
private final Class<?> columnType;
private final String tagColumn;
private final String columProperty;
private final TableFieldInfo tableFieldInfo;
private final boolean hasTypeHandle;
private final TypeHandler<?> typeHandler;
public SelectNormal(Class<?> clazz, boolean isPk, String column, Class<?> columnType, String columProperty, TableFieldInfo tableFieldInfo) {
this.clazz = clazz;
this.isPk = isPk;
this.column = column;
this.columnType = columnType;
this.columProperty = columProperty;
this.tagColumn = StringUtils.getTargetColumn(column);
this.tableFieldInfo = tableFieldInfo;
if (Objects.isNull(tableFieldInfo)) {
this.hasTypeHandle = false;
this.typeHandler = null;
} else {
this.hasTypeHandle = this.tableFieldInfo.getTypeHandler() != null && tableFieldInfo.getTypeHandler() != UnknownTypeHandler.class;
if (this.hasTypeHandle) {
TableInfo info = TableInfoHelper.getTableInfo(clazz);
Assert.notNull(info, "table not find by class <%s>", clazz.getSimpleName());
this.typeHandler = getTypeHandler(info.getConfiguration(), tableFieldInfo);
} else {
this.typeHandler = null;
public SelectNormal(SelectCache cache, String index) {
this.cache = cache;
this.index = index;
}
@Override
public Class<?> getClazz() {
return cache.getClazz();
}
@Override
public boolean isPk() {
return cache.isPk();
}
@Override
public String getColumn() {
return cache.getColumn();
}
@Override
public Class<?> getColumnType() {
return cache.getColumnType();
}
@Override
public String getTagColumn() {
return cache.getTagColumn();
}
@Override
public String getColumProperty() {
return cache.getColumProperty();
}
@Override
public boolean hasTypeHandle() {
return hasTypeHandle;
return cache.isHasTypeHandle();
}
@Override
public TypeHandler<?> getTypeHandle() {
return typeHandler;
return cache.getTypeHandler();
}
@Override
@ -84,6 +74,11 @@ public class SelectNormal implements Select {
return null;
}
@Override
public TableFieldInfo getTableFieldInfo() {
return cache.getTableFieldInfo();
}
@Override
public boolean isFunc() {
return false;
@ -98,14 +93,4 @@ public class SelectNormal implements Select {
public boolean isLabel() {
return false;
}
private TypeHandler<?> getTypeHandler(Configuration configuration, TableFieldInfo info) {
TypeHandlerRegistry registry = configuration.getTypeHandlerRegistry();
TypeHandler<?> typeHandler = registry.getMappingTypeHandler(info.getTypeHandler());
if (typeHandler == null) {
typeHandler = registry.getInstance(info.getPropertyType(), info.getTypeHandler());
}
return typeHandler;
}
}

View File

@ -1,5 +1,6 @@
package com.github.yulichang.test.join.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
@ -17,7 +18,7 @@ public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor paginationInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
PaginationInnerInterceptor page = new PaginationInnerInterceptor();
PaginationInnerInterceptor page = new PaginationInnerInterceptor(DbType.H2);
page.setOptimizeJoin(false);
interceptor.addInnerInterceptor(page);
return interceptor;

View File

@ -35,7 +35,8 @@ public class UserDO {
private Sex sex;
private String headImg;
@TableField("head_img")
private String img;
private LocalDateTime createTime;

View File

@ -132,6 +132,24 @@ class LambdaWrapperTest {
}
/**
* 别名测试
*/
@Test
void testAlias() {
MPJLambdaWrapper<UserDO> wrapper = new MPJLambdaWrapper<UserDO>()
// .disableSubLogicDel()//关闭副表逻辑删除
// .disableLogicDel()//关闭主表逻辑删除
.selectAll(UserDO.class)
.selectCollection(UserDO.class, UserDO::getChildren)
.leftJoin(UserDO.class, UserDO::getPid, UserDO::getId);
List<UserDO> list = userMapper.selectJoinList(UserDO.class, wrapper);
assert list.get(0).getName() != null && list.get(0).getChildren().get(0).getName() != null;
assert list.get(0).getImg() != null && list.get(0).getChildren().get(0).getImg() != null;
System.out.println(list);
}
/**
* 简单的分页关联查询 lambda
*/
@ -161,7 +179,7 @@ class LambdaWrapperTest {
.eq(UserDO::getId, AddressDO::getUserId)
.eq(UserDO::getId, AddressDO::getUserId))
.eq(UserDO::getId, 1)
.and(i -> i.eq(UserDO::getHeadImg, "er")
.and(i -> i.eq(UserDO::getImg, "er")
.or()
.eq(AddressDO::getUserId, 1))
.eq(UserDO::getId, 1));