joinWrapper增强

This commit is contained in:
张松 2025-02-20 17:21:09 +08:00
parent d15f1a00c4
commit dadaa401c2
2 changed files with 77 additions and 2 deletions

View File

@ -1,21 +1,26 @@
package com.czg.utils;
import cn.hutool.core.util.StrUtil;
import com.mybatisflex.core.constant.SqlConnector;
import com.mybatisflex.core.exception.FlexExceptions;
import com.mybatisflex.core.query.*;
import com.mybatisflex.core.table.TableInfo;
import com.mybatisflex.core.table.TableInfoFactory;
import com.mybatisflex.core.util.LambdaGetter;
import com.mybatisflex.core.util.LambdaUtil;
import com.mybatisflex.core.util.MapUtil;
import java.io.Serializable;
import java.lang.invoke.SerializedLambda;
import java.lang.reflect.Method;
import java.util.function.Consumer;
import static com.mybatisflex.core.util.LambdaUtil.getFieldName;
/**
* 关联查询的queryWrapper
*
* @author Administrator
*/
public class JoinQueryWrapper extends QueryWrapper {
@ -55,6 +60,61 @@ public class JoinQueryWrapper extends QueryWrapper {
return tableInfo.getTableName() + "." + StrUtil.toUnderlineCase(getFieldName(column));
}
public static <T> QueryColumn column(LambdaGetter<T> getter) {
SerializedLambda lambda = getSerializedLambda(getter);
Class<?> entityClass = getImplClass0(lambda);
TableInfo tableInfo = TableInfoFactory.ofEntityClass(entityClass);
String fieldName = getFieldName(getter);
QueryColumn columnByProperty = tableInfo.getQueryColumnByProperty(fieldName);
columnByProperty.setName(tableInfo.getTableName() + "." + StrUtil.toUnderlineCase(fieldName));
return columnByProperty;
}
@Override
public JoinQueryWrapper and(Consumer<QueryWrapper> consumer, boolean condition) {
if (!condition) {
return this;
}
JoinQueryWrapper newWrapper = new JoinQueryWrapper();
consumer.accept(newWrapper);
QueryCondition whereQueryCondition = newWrapper.whereQueryCondition;
if (whereQueryCondition != null) {
and(new Brackets(whereQueryCondition));
}
return this;
}
@Override
public JoinQueryWrapper or(Consumer<QueryWrapper> consumer, boolean condition) {
if (!condition) {
return this;
}
JoinQueryWrapper newWrapper = new JoinQueryWrapper();
consumer.accept(newWrapper);
QueryCondition whereQueryCondition = newWrapper.whereQueryCondition;
if (whereQueryCondition != null) {
or(new Brackets(whereQueryCondition));
}
return this;
}
@Override
public JoinQueryWrapper or(Consumer<QueryWrapper> consumer) {
return or(consumer, true);
}
@Override
public JoinQueryWrapper and(Consumer<QueryWrapper> consumer) {
return and(consumer, true);
}
@Override
public QueryWrapper and(QueryCondition queryCondition) {
return addWhereQueryCondition(queryCondition, SqlConnector.AND);
}
@Override
public <T> JoinQueryWrapper eq(LambdaGetter<T> column, Object value) {
eq(getColum(column), value);
@ -90,4 +150,12 @@ public class JoinQueryWrapper extends QueryWrapper {
orderBy(getColum(column), asc);
return this;
}
@Override
public <T> JoinQueryWrapper like(LambdaGetter<T> column, Object value) {
like(getColum(column), value);
return this;
}
}

View File

@ -18,6 +18,7 @@ import com.czg.sa.StpKit;
import com.czg.service.RedisService;
import com.czg.service.account.mapper.ShopUserMapper;
import com.czg.utils.AssertUtil;
import com.czg.utils.JoinQueryWrapper;
import com.czg.utils.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
@ -56,9 +57,15 @@ public class ShopUserServiceImpl extends ServiceImpl<ShopUserMapper, ShopUser> i
@Override
public Page<ShopUser> getPage(String key, Integer isVip) {
QueryWrapper queryWrapper = new QueryWrapper().eq(ShopUser::getShopId, StpKit.USER.getShopId());
JoinQueryWrapper queryWrapper = new JoinQueryWrapper().eq(ShopUser::getShopId, StpKit.USER.getShopId());
if (StrUtil.isNotBlank(key)) {
queryWrapper.and(column(UserInfo::getNickName).like(key).or(column(UserInfo::getPhone).like(key)));
queryWrapper.and(q -> {
q.like(UserInfo::getNickName, key).or(r -> {
r.like(UserInfo::getPhone, key);
});
});
// queryWrapper.and(JoinQueryWrapper.column(UserInfo::getNickName).like(key).or(JoinQueryWrapper.column(UserInfo::getPhone).like(key)));
}
if (isVip != null) {